[Resolved] Different action for each time an Object is touched

I want to make an object jump right when it is touched first time. When it is touched again, it jumps left, and with the third touch it jumps straight up. How can i do that?

I tried using a counter i.e

if count == 0, call touch event that makes object jump right
increment count.

if count == 1, call touch event that makes object jump left
increment count

this doesn’t work, the object jumps left (the last touch event) everytime.

any help would be appreciated… [import]uid: 175611 topic_id: 31047 reply_id: 331047[/import]

If a counter isn’t working then it means there is a flaw in the logic you are using. Track your counter value throughout your code to find the flaw.

[import]uid: 147305 topic_id: 31047 reply_id: 124140[/import]

Probably the flaw in the logic is that you increment the count after the first condition, which means it will now trigger the second condition, and then you increment the counter after the second condition, which means it will now trigger the third condition.

There are two ways to fix this. You can either use [lua]elseif[/lua]s instead of just [lua]if[/lua]s (which will ensure that only one block of code gets executed), or you can wait to increment the counter until the end.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31047 reply_id: 124145[/import]

Declare count at the top of your file [import]uid: 116842 topic_id: 31047 reply_id: 124153[/import]

[lua]local object

local touchObject = function (event)

local obj = event.target

if obj.state == 1 then
– jump right code
end

if obj.state == 2 then
– jump left code
end

if obj.state == 3 then
– jump straight up code
end

obj.state = obj.state + 1
if obj.state == 4 then obj.state = 1; end
end
object = display.newRect(240, 160, 50, 50)
object.state = 1
object:addEventListener(“touch”,touchObject)[/lua] [import]uid: 93133 topic_id: 31047 reply_id: 124173[/import]

you were right, the problem was with the logic. using elseif works. Many thanks to all those who replied. [import]uid: 175611 topic_id: 31047 reply_id: 124277[/import]

If a counter isn’t working then it means there is a flaw in the logic you are using. Track your counter value throughout your code to find the flaw.

[import]uid: 147305 topic_id: 31047 reply_id: 124140[/import]

Probably the flaw in the logic is that you increment the count after the first condition, which means it will now trigger the second condition, and then you increment the counter after the second condition, which means it will now trigger the third condition.

There are two ways to fix this. You can either use [lua]elseif[/lua]s instead of just [lua]if[/lua]s (which will ensure that only one block of code gets executed), or you can wait to increment the counter until the end.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31047 reply_id: 124145[/import]

Declare count at the top of your file [import]uid: 116842 topic_id: 31047 reply_id: 124153[/import]

[lua]local object

local touchObject = function (event)

local obj = event.target

if obj.state == 1 then
– jump right code
end

if obj.state == 2 then
– jump left code
end

if obj.state == 3 then
– jump straight up code
end

obj.state = obj.state + 1
if obj.state == 4 then obj.state = 1; end
end
object = display.newRect(240, 160, 50, 50)
object.state = 1
object:addEventListener(“touch”,touchObject)[/lua] [import]uid: 93133 topic_id: 31047 reply_id: 124173[/import]

you were right, the problem was with the logic. using elseif works. Many thanks to all those who replied. [import]uid: 175611 topic_id: 31047 reply_id: 124277[/import]