I need a flag!

Hi,

I have a draggable player that has four different run animations depending on whether you are dragging him upwards, rightwards, leftwards, or downwards. The problem is, while you are dragging him in any one direction, my code is constantly telling the system to play the run animation corresponding to that direction, and the animation never has a chance to get past it’s first frame. I need to create a flag that says, for example, “the player is already moving left and the corresponding animation has already kicked in, so don’t keep restarting that animation anymore”.

I’m guessing this is a very simple fix to implement, something like creating a variable with a true-false state that indicates whether a certain animation is already in progress or not, but to a non-coder like myself, getting the syntax right is proving to be beyond my best attempts.

This is my code thus far:
local prevx = 0local prevy = 0-- text to display directionlocal directionTXT = display.newText("direction: ",50,50,nil,25)directionTXT:setTextColor( 255,0,0 )local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 local dx = prevx - event.x local dy = prevy - event.y local distance = dx * dx + dy * dy if distance>400 then local angle = math.atan2(dy,dx) * 57.2957795 local string_dir if angle>=22*-1 and angle<23 then player:play('Player run side view left') string_dir="Left" elseif angle>=23 and angle<68 then player:play('Player run side view left') string_dir="Up Left" elseif angle>=68 and angle<113 then player:play('Player run back view') string_dir="Up" elseif angle>=113 and angle<158 then player:play('Player run side view right') string_dir="Up Right" elseif angle>=135 or angle<157*-1 then player:play('Player run side view right') string_dir="Right" elseif angle>=157*-1 and angle<112*-1 then player:play('Player run side view right') string_dir="Down Right" elseif angle>=112*-1 and angle<67*-1 then player:play('Player run front view') string_dir="Down"; elseif angle>=67*-1 and angle<22*-1 then player:play('Player run side view left') string_dir="Down Left" end prevx=event.x prevy=event.y directionTXT.text = "Direction : "..string_dir end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return trueendplayer.x = 200player.y = 200player:play('Player static back view')player:addEventListener( "touch", onTouch )[/code]Note that there are actually 8 directions contained in the code, but they translate down to only four actual animations (eg. down-left and up-left use the running-left animation).Much thanks,Steven [import]uid: 79394 topic_id: 13233 reply_id: 313233[/import]

Hey there,

For example;

[lua]local dragDirection[/lua]

Then, for the function that plays the drag left sprite, add in an if statement and change the dragDirection, like so;

[lua]if dragDirection ~= “left” then
– play sprite here
dragDirection = “left”
end[/lua] [import]uid: 52491 topic_id: 13233 reply_id: 48564[/import]

I’m sorry, Peach, I don’t know if what you’re suggesting is an alternate way of controlling the animation, or patch-code to be added to my existing code.

If it’s an alternate way of controlling the animation, I tried changing my code to this:
local prevx = 0local prevy = 0local dragDirection-- text to display directionlocal directionTXT = display.newText("direction: ",50,50,nil,25)directionTXT:setTextColor( 255,0,0 )local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 local dx = prevx - event.x local dy = prevy - event.y local distance = dx * dx + dy * dyif dragDirection ~= "left" thenplayer:play('Player run side view left')dragDirection = "left"print "left"end if dragDirection ~= "right" thenplayer:play('Player run side view right')dragDirection = "right"print "right"end if dragDirection ~= "up" thenplayer:play('Player run back view')dragDirection = "up"print "up"if dragDirection ~= "down" thenplayer:play('Player run front view')dragDirection = "down"print "down"end end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return trueendplayer.x = 200player.y = 200player:play('Player static back view')player:addEventListener( "touch", onTouch )[/code]..but it just plays the animation last in line, and the constantly restarting animation problem is still present.If your code is meant to be added to my existing code, I'm not sure where to add it. I've tried inserting the dragDirection definition at line 3, and then wrapping your suggestion around line 42 of my code, but I'm not getting the results I need. Could u elaborate a tad more for me? Like I said, I'm not much of a coder, I'm still just cutting and pasting chunks of code that make sense to me. [import]uid: 79394 topic_id: 13233 reply_id: 48700[/import]

I’m sorry, no - not an alternate way.

You would add it in around your current code. Where you play the animation currently for dragging it left, you’d put in an “if” statement to check whether or not dragDirection (or another variable name if you like) was set to “left” already - if it wasn’t, you’d play the animation and set dragDirection to left, if it was you’d do nothing.

Does that make it clearer? You’d just add in “if” statements to stop it triggering a thousand times :wink:

Peach [import]uid: 52491 topic_id: 13233 reply_id: 48748[/import]

Hi Peach,

Yes, your explanation made things crystal clear. With that in mind, I re-inserted your code suggestion and now my code works great, the problem is completely gone. Thank you soo much!

Sincerely,
Steven [import]uid: 79394 topic_id: 13233 reply_id: 48766[/import]

That is due to the many levels of B.A.M.F Peach has acquired.
[import]uid: 61600 topic_id: 13233 reply_id: 48897[/import]

Hear, hear. [import]uid: 79394 topic_id: 13233 reply_id: 48978[/import]

Booyah! :smiley:

I love it when I can help solve their issues; really happy to hear it is working great.

PS - @Nick/G$, true dat, 'yo. [import]uid: 52491 topic_id: 13233 reply_id: 48985[/import]