Drag Event to Sprite Animation

Ok,

I have my drag event so when I drag my finger across a physics enabled area left and right a character will move left and right. I have now replaced the static character with a sprite animation. He only “runs” right because that is all it is showing even though the code has phycially moving left. I know this is because I don’t have it calling the sequence of him running left and only right.

In short how can I tell which direction the finger is swiping so I can call the appropriate sprite sequence?

swipe left load the run left, swipe right load the run right…

Matt

[code]
local grass = display.newImage(“grass.png”)
grass.x = 160; grass.y = 440
physics.addBody( grass, “static”)

sheetData = {width=96, height=96,numFrames=24,sheetContentWidth=512, sheetContentHeight=512,loopCount=1}
sprites = graphics.newImageSheet(“coinsprite.png”,sheetData)
sequenceData =
{
{name=“runright”,frames={9,10,11,12,13,14,15,16}},
{name=“runleft”,frames={17,18,19,20,21,22,23,24}},
{name=“dieright”,frames={6,7,8}},
{name=“dieleft”,frames={3,4,5}},
}
adventurer = display.newSprite(sprites,sequenceData)
adventurer.x = screenW/2; adventurer.y = screenH - 105
adventurer.myName = “adventureman”
local adventurerShape = {-18,-18,18,-18,18,18,-18,18}
physics.addBody(adventurer, “static”,{isSensor=true, shape=adventurerShape})
adventurer.x = math.random(10,screenW-10)
– drag event
local function startDrag( event )

if “began” == event.phase then
adventurer.isFocus = true
adventurer.x0 = event.x - adventurer.x
elseif adventurer.isFocus then
if “moved” == event.phase then
adventurer.x = event.x - adventurer.x0
adventurer:setSequence(“runright”)
adventurer:play()
if adventurer.x >= screenW then
adventurer.x = screenW - 20
end
if adventurer.x < 0 then
adventurer.x = 0 + 20
end
elseif “ended” == phase or “cancelled” == phase then
adventurer.isFocus = false
end
end
return true
end

grass:addEventListener(“touch”, startDrag)
[/code] [import]uid: 18783 topic_id: 35535 reply_id: 335535[/import]

Your touch event table that gets passed to you should have the xStart and yStart of the event. If event.xStart > event.x then you’re moving right to left. If event.xStart < event.x then you’re moving left to right.
[import]uid: 199310 topic_id: 35535 reply_id: 141275[/import]

The start position of the character is determined by this line…

adventurer.x = math.random(10,screenW-10)

so adventurer.x0 seems like the line you are referring too in order to start the moving logic? [import]uid: 18783 topic_id: 35535 reply_id: 141282[/import]

I guess I was a bit confusing. It seems to me that you need to know if the person is swiping left or right, so that you can then determine which sprite to play.

The suggestion I made above should tell you which way the finger is moving on the device and you can use that information to drive your sprite.

Make sense? [import]uid: 199310 topic_id: 35535 reply_id: 141283[/import]

it does but didn’t quite work for the dragging event. Also when dragging it seems that the animation wont animate until the finger is lifted… so if it is a swipe it kinda works but if you drag your finger to move the character it holds the current sprite frame until you release your finger.

Any examples I can look at somewhere for this type of action?

[import]uid: 18783 topic_id: 35535 reply_id: 141531[/import]

Rob… after messing with it some more… it was just the > < in your code that were backwards for me. I fixed that and now if you drag right he runs right and left he runs left… any idea how to get the sprite to go while he is running and not after the finger is released?

Matt [import]uid: 18783 topic_id: 35535 reply_id: 141541[/import]

I don’t have a lot of experience with sprites, but however you play the sequence, put it in the event.phase == “began” part of the IF statement and stop playing when you get an “ended” phase. [import]uid: 199310 topic_id: 35535 reply_id: 141612[/import]

Your touch event table that gets passed to you should have the xStart and yStart of the event. If event.xStart > event.x then you’re moving right to left. If event.xStart < event.x then you’re moving left to right.
[import]uid: 199310 topic_id: 35535 reply_id: 141275[/import]

The start position of the character is determined by this line…

adventurer.x = math.random(10,screenW-10)

so adventurer.x0 seems like the line you are referring too in order to start the moving logic? [import]uid: 18783 topic_id: 35535 reply_id: 141282[/import]

I guess I was a bit confusing. It seems to me that you need to know if the person is swiping left or right, so that you can then determine which sprite to play.

The suggestion I made above should tell you which way the finger is moving on the device and you can use that information to drive your sprite.

Make sense? [import]uid: 199310 topic_id: 35535 reply_id: 141283[/import]

it does but didn’t quite work for the dragging event. Also when dragging it seems that the animation wont animate until the finger is lifted… so if it is a swipe it kinda works but if you drag your finger to move the character it holds the current sprite frame until you release your finger.

Any examples I can look at somewhere for this type of action?

[import]uid: 18783 topic_id: 35535 reply_id: 141531[/import]

Rob… after messing with it some more… it was just the > < in your code that were backwards for me. I fixed that and now if you drag right he runs right and left he runs left… any idea how to get the sprite to go while he is running and not after the finger is released?

Matt [import]uid: 18783 topic_id: 35535 reply_id: 141541[/import]

I don’t have a lot of experience with sprites, but however you play the sequence, put it in the event.phase == “began” part of the IF statement and stop playing when you get an “ended” phase. [import]uid: 199310 topic_id: 35535 reply_id: 141612[/import]