Help with touch event

Hello everybody,

I new in Corona and I´m trying my first app.
I have a object that move by 4 buttons (up/donw/left/right) with applyforce.

While the button are press I want apply force in the object, so, for that Im using this code for each button:

function btUp( event )  
 if event.phase == "began" then  
 impUp = true  
 elseif event.phase == "ended" or event.phase == "cancelled" or event.phase == "moved" then  
 impUp = false  
 nave:stopAtFrame(1)   
 end  
end  
  
function impulseUp()   
 if (impUp and combustivel \> 0) then  
 nave:applyForce( 0, -0.5, nave.x, nave.y )  
 nave:stopAtFrame(3)   
 combustivel = combustivel - 2  
 health = combustivel  
 moveHealth()  
 end  
end  
upimg:addEventListener("touch", btUp)  
Runtime:addEventListener("enterFrame", impulseUp)  

But the up command sometimes freeze after I get out my finger. Like I was still pressing.

I tried using too this another code:

function btUp( event )  
 if event.phase == "began" then  
 function impulseUp()   
 if(combustivel \> 0) then  
 nave:applyForce( 0, -0.5, nave.x, 0 )  
 nave:play{ startFrame=2, endFrame=3, loop=0 }  
 combustivel = combustivel - 2  
 health = combustivel  
 moveHealth()   
 end  
 end  
 Runtime:addEventListener("enterFrame", impulseUp)  
 elseif "ended" == phase or "cancelled" == phase or "moved" == phase then   
 Runtime:removeEventListener("enterFrame", impulseUp);  
 nave:stopAtFrame(1)   
 end  
end  
upimg:addEventListener("touch", btUp)  

But had the some problem.

Any ideia?

[import]uid: 57791 topic_id: 14585 reply_id: 314585[/import]

In second code, the correct line 13 is

elseif event.phase == "ended" or event.phase == "cancelled" or event.phase == "moved" then [import]uid: 57791 topic_id: 14585 reply_id: 53949[/import]

Why not remove the check for “moved”…that may fix the issue.

Also, for the 2nd code example…I think you want to check for event.phase…not just the “phase” variable. I don’t see variable defined.

[import]uid: 12700 topic_id: 14585 reply_id: 53968[/import]

Hello indiegamepod,

Thx for your reply.

You right about the second code, I did this correction in first comment.

The “moved” event ocurr when you move a finger over a object with “touch” listener?
If this is correct, in those algoritm when “moved” occur, was like I took off the finger from the button, but my problem is that the button answer like I was pressing all time.

But I´m new in Corona and can be talking smething nonsense. I try without “moved” [import]uid: 57791 topic_id: 14585 reply_id: 53973[/import]

I remove “moved” but nothing…

I forgot something essencial… This only happen in Iphone Device… In simulator everything work fine. i dont try in Android

Can I see the “prints” for debug when run in iphone device? [import]uid: 57791 topic_id: 14585 reply_id: 53993[/import]

Have you seen this tutorial by Peach Pellen?

http://techority.com/2011/02/14/controlling-a-character-with-a-d-pad/ [import]uid: 67839 topic_id: 14585 reply_id: 54005[/import]

Hello elbowroomapps,

Very nice tutorial, I think this approach will resolve my problem.

local function stop (event)   
 if event.phase =="ended" then  
 motionx = 0  
 motiony = 0  
 end  
end  
  
Runtime:addEventListener("touch", stop )  

Thx a lot. [import]uid: 57791 topic_id: 14585 reply_id: 54008[/import]

I’m glad it helped.

The thanks should really go to Peach Pellen. [import]uid: 67839 topic_id: 14585 reply_id: 54012[/import]

I love it when I come into a thread, ready to help - and see that the problem has already been solved.

Admittedly, I love it a little more when it’s been solved using one of my tutorials :wink:

Thanks guys! [import]uid: 52491 topic_id: 14585 reply_id: 54047[/import]

Hello Peach,

Sry but the problem wasnt resolve… :frowning:

I will clear all code and leave only the problem part, test in iPhone, and, if persist, put here.

This is a Multitouch app, so, in simulator run fine (I cannt press 2 button at same time, course). But, is possible I see a terminal for debug when run in iPhone? Or log file?

Thx for help. [import]uid: 57791 topic_id: 14585 reply_id: 54062[/import]

yeah… I cleaned all code and put only the essential to reproduce the problem. The problem doesn´t happen all time, but happens.

system.activate( "multitouch" )  
local physics = require( "physics" )  
physics.start()  
physics.setGravity(0, 2.5)   
  
local esq  
local dir  
local freio  
local nave  
  
------ PREPARACAO DO AMBIENTE  
function setUpBotoes()  
 freio = display.newImage( "botaoredondo.png");   
 freio.x = 45  
 freio.y = 295  
 esq = display.newImage( "setaesq.png");   
 esq.x = 375  
 esq.y = 295  
 dir = display.newImage( "setadir.png");   
 dir.x = 450  
 dir.y = 295  
end  
  
function setUpGround()  
 local ground = display.newImage( "ground.png" )  
 ground.x = display.contentWidth / 2  
 ground.y = 270   
 physics.addBody( ground, "static", { friction=1, bounce=0, filter = {categoryBits = 4, maskBits = 2} })  
end  
  
------------ JOGABILIDADE  
function btFreio( event )  
 if event.phase == "began" then  
 function impulsoFreio()   
 nave:applyForce( 0, -0.5, nave.x, nave.y )  
 end  
 Runtime:addEventListener("enterFrame", impulsoFreio)  
 else  
 Runtime:removeEventListener("enterFrame", impulsoFreio)  
 end  
end  
  
function btDir( event )  
 if event.phase == "began" then  
 function impulsoDir()  
 nave:applyForce( -0.15, 0, nave.x, nave.y )  
 end   
 Runtime:addEventListener("enterFrame", impulsoDir)  
 else  
 Runtime:removeEventListener("enterFrame", impulsoDir)  
 end  
end  
function btEsq( event )  
 if event.phase == "began" then  
 function impulsoEsq()  
 nave:applyForce( 0.15, 0, nave.x, nave.y )  
 end  
 Runtime:addEventListener("enterFrame", impulsoEsq)  
 else  
 Runtime:removeEventListener("enterFrame", impulsoEsq)  
 end  
end  
  
function setUpNave()  
 nave = display.newImage("nave25.png")  
 nave.x = 200  
 nave.y = 30  
 nave.linearDamping = 5  
 naveshape01 = {-5,5, -5,-6, -1,-12, 0,-16, 6,-7, 6,5, 6,11, 4,14, -3,14, -5,10 }  
 naveshape02 = {-12,8, -10,4, -6,5, -6,10, -8,16, -11,16, -12,11}  
 naveshape03 = {7,6, 10,4, 12,8, 12,15, 10,16, 8,15, 7,11}  
  
 physics.addBody(nave, "dynamic",   
 { density = 0.1, friction=0.01, shape = naveshape01, filter = {categoryBits = 2, maskBits = 12}},  
 { density = 0.1, friction=0.01, shape = naveshape02, filter = {categoryBits = 2, maskBits = 12}},  
 { density = 0.1, friction=0.01, shape = naveshape03, filter = {categoryBits = 2, maskBits = 12}}   
 )   
 nave.isFixedRotation = true  
end  
function main()  
 setUpNave()  
 setUpBotoes()  
 setUpGround()  
 esq:addEventListener("touch", btEsq)  
 dir:addEventListener("touch", btDir)  
 freio:addEventListener("touch", btFreio)  
end  
  
main()  
  

[import]uid: 57791 topic_id: 14585 reply_id: 54103[/import]

If you want to see the log files on the iPhone, I use Xcode. Open the organizer on Xcode and connect your iPhone to it. Then drag the file of your game to the applications folder. Run and teat you game on the iPhone and the console and log files are present on Xcode!
I don’t know if I explained it good, hope I helped…
And I also believe there are many ways but that is just the way I do it. [import]uid: 24708 topic_id: 14585 reply_id: 54113[/import]

Hello brian,

This is like I do to test my app in iPhone, but I tried before and nothing (no one print(“something”)) went to log or console in xcode (my Iphone still puggled).

Need some diretive or command in main.lua to send print() to console or log in xcode? [import]uid: 57791 topic_id: 14585 reply_id: 54120[/import]

After many test, many prints, many differents codes to do the same thing…

my conclusion is: sometimes (1 in 20) the ended/moved/canceled phase in touch event isn’t detect. :frowning: Maybe the time between the press and the lift finger. Maybe some problem with applyforce use…

If somebody want check this, pls ask me about the procedure.
thx Claudio [import]uid: 57791 topic_id: 14585 reply_id: 54136[/import]

For me at least, it seems like a group of code comes every min or so.

For one of my last games i had a perform with delay timer and every second i printed something. It wouldn’t come in every second, but a group would come in every min or so( maybe a little longer).

and just try

print(“something”)

And also since it is not working you should look at the code made by peach pellen(link is at the start of the forum). Maybe there is something you are doing wrong, and most likely your code has similar characteristics. [import]uid: 24708 topic_id: 14585 reply_id: 54150[/import]

Hey again, sorry to hear that.

May I ask, is there a reason you are using apply force (which stacks) rather than applying linear velocity?

I am sure we can figure out a solution, I’m just wondering if it might be an option to use that instead - if so that could potentially be the way to go.

Let us know :slight_smile:

Peach [import]uid: 52491 topic_id: 14585 reply_id: 54151[/import]

Hey Peach,

It is a spacecraft in a low gravity environment, then the “applyforce” represented well the impulse jet stabilizers. This weekend I will try to use the linear velocity, but for a good representation, while the button is pressed, the speed increase and should stop when the finger is removed, then I fear that the same problem occurs.

[import]uid: 57791 topic_id: 14585 reply_id: 54248[/import]

I understand.

Can you try the print statements that Brian suggested, to help troubleshoot? :slight_smile: [import]uid: 52491 topic_id: 14585 reply_id: 54260[/import]

Hi Peach,

Note that their algorithm uses a constant speed in an environment monotouch and no gravity. I will adapt it to applyforce and multitouch. I need the multitouch because the user has to act against gravity almost all the time, even when going left or right. I will put here when functional. [import]uid: 57791 topic_id: 14585 reply_id: 54285[/import]

Hey Peach!

I was adapting your code, but I had problems early on with the routine below:

local function stop (event)  
if event.phase =="ended" then  
motionx = 0  
motiony = 0  
end  
end  
Runtime:addEventListener("touch", stop )  

I had to look way to detect which button had been released so I read with more details on event propagation and setfocus and decided to do some more tests on old code.

I added Setfocus to each button, but now there was always the problem when two buttons were played simultaneously, there seems to be a conflict event, then the variable changed event internal to each function (I thing this is a bug) and everything worked perfectly.

The final code:

system.activate( "multitouch" )  
local physics = require( "physics" )  
physics.start()  
physics.setGravity(0, 2.5)   
  
local leftdir  
local rightdir  
local breaker  
local ship  
local ground  
  
breaker = display.newImage( "botaoredondo.png");   
breaker.x = 45  
breaker.y = 290  
leftdir = display.newImage( "setaesq.png");   
leftdir.x = 375  
leftdir.y = 290  
  
rightdir = display.newImage( "setadir.png");   
rightdir.x = 450  
rightdir.y = 290  
  
ground = display.newImage( "ground.png" )  
ground.x = display.contentWidth / 2  
ground.y = 250  
physics.addBody( ground, "static", { friction=1, bounce=0, filter = {categoryBits = 4, maskBits = 2} })  
  
ship = display.newImage("nave25.png")  
ship.x = 200  
ship.y = 30  
-- nave.linearDamping = 5  
shipshape01 = {-5,5, -5,-6, -1,-12, 0,-16, 6,-7, 6,5, 6,11, 4,14, -3,14, -5,10 }  
shipshape02 = {-12,8, -10,4, -6,5, -6,10, -8,16, -11,16, -12,11}  
shipshape03 = {7,6, 10,4, 12,8, 12,15, 10,16, 8,15, 7,11}  
  
physics.addBody(ship, "dynamic",   
{ density = 0.1, friction=0.01, shape = shipshape01, filter = {categoryBits = 2, maskBits = 12}},  
{ density = 0.1, friction=0.01, shape = shipshape02, filter = {categoryBits = 2, maskBits = 12}},  
{ density = 0.1, friction=0.01, shape = shipshape03, filter = {categoryBits = 2, maskBits = 12}}   
)   
ship.isFixedRotation = true  
  
function btBreak( eventb )  
 if eventb.phase == "began" then  
 function impulseBreak()   
 ship:applyForce( 0, -0.5, ship.x, ship.y )  
 end  
 display.getCurrentStage():setFocus(breaker, eventb.id)  
 Runtime:addEventListener("enterFrame", impulseBreak)  
 else  
 display.getCurrentStage():setFocus(breaker, nil)  
 Runtime:removeEventListener("enterFrame", impulseBreak)  
 end  
end  
  
function btRight( eventr )  
 if eventr.phase == "began" then  
 function impulseRight()  
 ship:applyForce( -0.15, 0, ship.x, ship.y )  
 end   
 display.getCurrentStage():setFocus(rightdir, eventr.id)  
 Runtime:addEventListener("enterFrame", impulseRight)  
 else  
 display.getCurrentStage():setFocus(rightdir, nil)  
 Runtime:removeEventListener("enterFrame", impulseRight)  
 end  
end  
  
function btLeft( eventl )  
 if eventl.phase == "began" then  
 function impulseLeft()  
 ship:applyForce( 0.15, 0, ship.x, ship.y )  
 end  
 display.getCurrentStage():setFocus(leftdir, eventl.id)  
 Runtime:addEventListener("enterFrame", impulseLeft)  
 else  
 display.getCurrentStage():setFocus(leftdir, nil)  
 Runtime:removeEventListener("enterFrame", impulseLeft)  
 end  
end  
  
breaker:addEventListener("touch", btBreak)  
leftdir:addEventListener("touch", btLeft)  
rightdir:addEventListener("touch", btRight)  

[import]uid: 57791 topic_id: 14585 reply_id: 54294[/import]