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]

