Touch Sensor

I have to click on the sensor twice before it shows. I pulled out the following code that is not working as I expected. I’m sure its something simple I am doing wrong. Please help thanks

–> Define our touch event listener (my diagnostic cod is included (the function does not execute first time I —> click. It shows it was hit twice the second time I click.
function TrustSensor(event)

Touched = Touched + 1.0

yImage = display.newImage( “buttonBlue.png”, 0, 420, true )
msg = display.newText(“Touc1”, 10, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

if Fuel < 1 then
Trust = 0
Fuel = 0
else
Trust = Trust + 30
if Trust > 100 then
Trust = 100
end
yImage = display.newImage( “buttonBlue.png”, 0, 320, true )
msg = display.newText(“Touch”, 10, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

–> Display Trust Data
yImage = display.newImage( “buttonBlue.png”, 0, 120, true )
msg = display.newText(“Trust”, 10, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Trust, “%d”)
msg = display.newText( testtext2, 120, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

end

end
–> Add trustcontrol
local trustcontrol = display.newImage(“TrustControl.png”)
trustcontrol.x = display.contentWidth -20
trustcontrol.y = display.contentHeight - 900

–> Turn Trust Control into physics body
physics.addBody(trustcontrol, { density=1, friction=10, bounce=.01 })

trustcontrol:addEventListener(“touch”, TrustSensor)

[import]uid: 12141 topic_id: 4702 reply_id: 304702[/import]

You have an if statement

if Fuel \< 1 then  

but I don’t see anywhere in your code where Fuel is set to more than 1. I’m not able to follow your code exactly right now, but I’m guessing it has something to do with the initial value of Fuel. [import]uid: 12108 topic_id: 4702 reply_id: 14901[/import]

I just pulled out the affected code. The fuel equation is outside the function. However the display function above and below is outside the “if” and does not display until it’s been clicked 2 times. [import]uid: 12141 topic_id: 4702 reply_id: 14925[/import]

oh wait I just noticed your note that TrustSensor() doesn’t even run the first time. Maybe return true at the end of the function to end the touch event? Another thing to consider is that there are separate events triggered for the “began” and “end” phase of a touch, but really I’m not sure what the problem is here.

If you could distill this into code we can run that shows the problem that might help; I didn’t realize this was an excerpt that isn’t complete by itself. [import]uid: 12108 topic_id: 4702 reply_id: 14935[/import]

i made the code easier to read
[blockcode]function TrustSensor(event)

Touched = Touched + 1.0

yImage = display.newImage( “buttonBlue.png”, 0, 420, true )
msg = display.newText(“Touc1”, 10, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

if Fuel < 1 then
Trust = 0
Fuel = 0
else
Trust = Trust + 30
if Trust > 100 then
Trust = 100
end
yImage = display.newImage( “buttonBlue.png”, 0, 320, true )
msg = display.newText(“Touch”, 10, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

–> Display Trust Data
yImage = display.newImage( “buttonBlue.png”, 0, 120, true )
msg = display.newText(“Trust”, 10, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Trust, “%d”)
msg = display.newText( testtext2, 120, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

end

end

–> Add trustcontrol
local trustcontrol = display.newImage(“TrustControl.png”)
trustcontrol.x = display.contentWidth -20
trustcontrol.y = display.contentHeight - 900

–> Turn Trust Control into physics body
physics.addBody(trustcontrol, { density=1, friction=10, bounce=.01 })

trustcontrol:addEventListener(“touch”, TrustSensor) [import]uid: 7911 topic_id: 4702 reply_id: 14942[/import]

this wont help your problem but will optimize a little
i removed the if statement for max trust
[blockcode]
function TrustSensor(event)

Touched = Touched + 1.0

yImage = display.newImage( “buttonBlue.png”, 0, 420, true )
msg = display.newText(“Touc1”, 10, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 425, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

if Fuel < 1 then
Trust = 0
Fuel = 0
else
Trust = Trust + 30
Trust = math.min(100, Trust)

yImage = display.newImage( “buttonBlue.png”, 0, 320, true )
msg = display.newText(“Touch”, 10, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Touched, “%d”)
msg = display.newText( testtext2, 120, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

–> Display Trust Data
yImage = display.newImage( “buttonBlue.png”, 0, 120, true )
msg = display.newText(“Trust”, 10, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (Trust, “%d”)
msg = display.newText( testtext2, 120, 125, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )

end

end

–> Add trustcontrol
local trustcontrol = display.newImage(“TrustControl.png”)
trustcontrol.x = display.contentWidth -20
trustcontrol.y = display.contentHeight - 900

–> Turn Trust Control into physics body
physics.addBody(trustcontrol, { density=1, friction=10, bounce=.01 })

trustcontrol:addEventListener(“touch”, TrustSensor)
[import]uid: 7911 topic_id: 4702 reply_id: 14943[/import]

Thank You, sorry about the lack of indents in the code I posted.
I need to further investigate your comment:
“Another thing to consider is that there are separate events triggered for the “began” and “end” phase of a touch, but really I’m not sure what the problem is here.”
I did not realize there was a sensor trigger for the beginning and the end.

[import]uid: 12141 topic_id: 4702 reply_id: 14955[/import]

try adding some print statements
put print(“Trust Sensor”) just above Touched = Touched + 1
put print(“fuel:”…fuel…" , trust:"…trust) just above the if statement

then run the corona terminal app that way when you touch button you will know if it is going to the function and the value of the variables

also dont think it should matter but have you tried giving unique names to the display images and text
EDITED TYPOS [import]uid: 7911 topic_id: 4702 reply_id: 14957[/import]

you could also change
trustcontrol:addEventListener(“touch”, TrustSensor)
to
trustcontrol:addEventListener(“tap”, TrustSensor)

then you wont have to worry about the event.phase for just a simple tap of a button [import]uid: 7911 topic_id: 4702 reply_id: 14958[/import]

Thank You for your patience and your explanation. I took this code from the corona site since it creates the same results as my code.
The following is a code example I found on the site with my display addition:

–> Code example from http://developer.anscamobile.com/reference/index/eventxstart

function drawLine( event )
if(event.phase == “ended”) then
line = display.newLine(event.xStart, event.yStart, event.x, event.y)
line:setColor(255,0,0)
line.width = 5
end
–> Begin My Add
–> Display Touch event.xStart
yImage = display.newImage( “buttonBlue.png”, 0, 320, true )
msg = display.newText(“Touch”, 10, 325, native.systemFont, 20 )
msg:setTextColor( 255,255,255 )
testtext2 = tostring (event.xStart, “%d”)
msg = display.newText( testtext2, 120, 325, native.systemFont, 30 )
msg:setTextColor( 255,255,255 )
–> End My Add

end
Runtime:addEventListener(“touch”, drawLine)

With or without my display addition it ignores the first touch. I’m missing why it is not picking up the first touch on the screen. I need to know where they touched on the screen. I’m good after the first touch. I am using the Corona Iphone & Ipad simulator for the testing.

PS: Do you know how I keep from displaying my email address as my user name. [import]uid: 12141 topic_id: 4702 reply_id: 14961[/import]