Help animating player

I am currently trying to animate the playable character in a game. I have the animation running but it is separate from the playable character that shows up.

local sprite = require “sprite”
local sheet1 = sprite.newSpriteSheet( “richClimb1.png”, 512, 256 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 6)
sprite.add( spriteSet1, “rich”, 1, 6, 1000, 0 ) – play 6 frames every 1000 ms
local instance1 = sprite.newSprite( spriteSet1 )
instance1:prepare(“rich”)
instance1:play()

this is the code I have. It works, but how do I connect it to the player? Also i am trying to animate this on a line. [import]uid: 94237 topic_id: 16941 reply_id: 316941[/import]

What do you mean connect it? If the sprite works, and it represents the playable character, just make it a physics body and use the sprite as the player’s character.

local instance1 = sprite.newSprite( spriteSet1 )
physics.addBody (instance1)

Now the sprite can run into walls and stuff. [import]uid: 89724 topic_id: 16941 reply_id: 63515[/import]

function spawnPlayerObject(xPlayerSpawn,yPlayerSpawn,richTurn) --spawns Rich where we need him.
– riches sprite sheet and all his inital spirites. We need to adjust this so that animations 3-6 are the only ones that animate when moving horizontally
local richPlayer = sprite.newSpriteSet(richSheet1,1,6)
sprite.add(richPlayer, “rich”, 1,6,500,1)
rich = sprite.newSprite(richPlayer)
rich.x = xPlayerSpawn
rich.y = yPlayerSpawn
rich:scale(_W*0.0009, _W*0.0009) – scale is used to adjust the size of the object.
richDir = richTurn
rich.rotation = richDir
physics.addBody( rich, “static”, { friction=1, radius = 15 } ) – needs a better physics body for collision detection.
end

ok this is what i have now. And what i mean is that the player draws a line and then the character follows it after completion and animates the sprite set on the line. here is what we have for that.

function movePlayerOnLine(event)
–for the original image replace all rich with player.
if posCount ~= linePart then
richDir = math.atan2(ractgangle_hit[posCount].y-rich.y,ractgangle_hit[posCount].x-rich.x)*180/math.pi
playerSpeed = 5
rich.x = rich.x + math.cos(richDir*math.pi/180)*playerSpeed
rich.y = rich.y + math.sin(richDir*math.pi/180)*playerSpeed
if rich.x < ractgangle_hit[posCount].x+playerSpeed*10 and rich.x > ractgangle_hit[posCount].x-playerSpeed*10 then
if rich.y < ractgangle_hit[posCount].y+playerSpeed*10 and rich.y > ractgangle_hit[posCount].y-playerSpeed*10 then
posCount = posCount + 1
end
end

right now it works but it is not animating. [import]uid: 94237 topic_id: 16941 reply_id: 63646[/import]

It’s not animating because your missing the prepare and play commands by the looks of it? [import]uid: 84637 topic_id: 16941 reply_id: 63661[/import]

I put the prepare and play commands in.

instance1:prepare(“rich”)
instance1:play()

but it is still doing the same thing and not animating [import]uid: 94237 topic_id: 16941 reply_id: 63662[/import]

Exactly because instance1 is nothing now with your changes.

This is more correct :

rich:prepare("rich") rich:play() [import]uid: 84637 topic_id: 16941 reply_id: 63664[/import]

I see what you’re saying. I made the changes but it still is not animating. hmmm. [import]uid: 94237 topic_id: 16941 reply_id: 63666[/import]

Where are you calling those two functions?

If your calling them in a enterframe or loop (ie constantly) then it wont work. [import]uid: 84637 topic_id: 16941 reply_id: 63667[/import]

I put them at the end right after rich.rotation [import]uid: 94237 topic_id: 16941 reply_id: 63672[/import]

he seems to be doing the animation at the beginning once and not on the line. [import]uid: 94237 topic_id: 16941 reply_id: 63683[/import]

the goal is for it to animate the sprite set on the drawn line. Right now though it is animate at the beginning and end of the line but not while moving. [import]uid: 94237 topic_id: 16941 reply_id: 63857[/import]

as far as i can tell, nothing has changed but i am getting an error on this line:
richDir = math.atan2(ractgangle_hit[posCount].y-rich.y,ractgangle_hit[posCount].x-rich.x)*180/math.pi

of attempt to upvalue “rich” a nil value. [import]uid: 94237 topic_id: 16941 reply_id: 63861[/import]