[Resolved] Touch logic

Hello Friends

I am developing a game in which the object flies untill button is press and on release it moves down. so i wanted to know how can we go for that i mean :

As long as player continue his touch object flies and as soon as he release the button it again fall down.

Can anyone tell me how can i do that?

Regards
Varun [import]uid: 130269 topic_id: 30700 reply_id: 330700[/import]

Thats actually pretty easy and something that Corona SDK.

You will use a “touch” handler on a graphic that represents your button. The touch handler generates several events for each touch. The two you are interested in are the “began” event and the “ended” event. You get a began event when the touch starts and the ended event when they let up.

  
local function buttonHandler(event)  
 if event,phase == "begain" then  
 -- your code to start flying  
 elseif event.phase == "ended" then  
 -- your code to stop flying  
 end  
 return true -- very important to have this here  
end  
  
button = display.newImageRect("yourbuttongraphic.png",128,64) -- or whatever size your graphic is  
button:addEventListener("touch",buttonHandler)  

Of course you have to position your button, add it to any groups, etc. but that’s the gist of it. [import]uid: 19626 topic_id: 30700 reply_id: 123000[/import]

PS: attention to the line:

2-[lua]if event,phase == “begain” then[/lua]

it should be:

2-[lua]if event,phase == “began” then[/lua]
Only a small typo, nothing else.

[import]uid: 89165 topic_id: 30700 reply_id: 123040[/import]

1000 apologizes… Its what I get for responding before I’m awake… Thanks for the read-behind @RSCdev! [import]uid: 19626 topic_id: 30700 reply_id: 123047[/import]

Haha, you`re more than welcome and am pleased to do that when it is possible @Rob.

BTW, there is other typo that I saw just now! , look below (tip: the comma!)
2-[lua]if event,phase == “begain” then [/lua] --<
it should be:

2-[lua]if event.phase == “began” then [/lua] --<Sorry for that. :slight_smile:
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30700 reply_id: 123056[/import]

Thats actually pretty easy and something that Corona SDK.

You will use a “touch” handler on a graphic that represents your button. The touch handler generates several events for each touch. The two you are interested in are the “began” event and the “ended” event. You get a began event when the touch starts and the ended event when they let up.

  
local function buttonHandler(event)  
 if event,phase == "begain" then  
 -- your code to start flying  
 elseif event.phase == "ended" then  
 -- your code to stop flying  
 end  
 return true -- very important to have this here  
end  
  
button = display.newImageRect("yourbuttongraphic.png",128,64) -- or whatever size your graphic is  
button:addEventListener("touch",buttonHandler)  

Of course you have to position your button, add it to any groups, etc. but that’s the gist of it. [import]uid: 19626 topic_id: 30700 reply_id: 123000[/import]

PS: attention to the line:

2-[lua]if event,phase == “begain” then[/lua]

it should be:

2-[lua]if event,phase == “began” then[/lua]
Only a small typo, nothing else.

[import]uid: 89165 topic_id: 30700 reply_id: 123040[/import]

1000 apologizes… Its what I get for responding before I’m awake… Thanks for the read-behind @RSCdev! [import]uid: 19626 topic_id: 30700 reply_id: 123047[/import]

Haha, you`re more than welcome and am pleased to do that when it is possible @Rob.

BTW, there is other typo that I saw just now! , look below (tip: the comma!)
2-[lua]if event,phase == “begain” then [/lua] --<
it should be:

2-[lua]if event.phase == “began” then [/lua] --<Sorry for that. :slight_smile:
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30700 reply_id: 123056[/import]