Sprite suquence doesn't play

Hi, my sprite sequence doesn’t play, where should I position the play function? Thank you for your help.

local walk2 = { width=106, height=138, numFrames=11, sheetContentWidth=212, sheetContentHeight=828 } local myImage = graphics.newImageSheet( "images/hoja.png", walk2) local sequenceWalk = { { name = "walkLeft", frames={7,8,9,10,11}, time=800 }, { name = "walkRight", frames={1,2,3,4,5} }, } local walk = display.newSprite( myImage, sequenceWalk ) walk.x = width/2 - 150 walk.y = height/2 --ANIMACIJA local dx = 4 local function move() walk.x = walk.x + dx if(dx\>0) then walk:setSequence("walkRight") walk:play() else walk:setSequence("walkLeft") walk:play() end if ((walk.x \< 47) or (walk.x \> width - 47)) then dx = dx \* -1 end end

Hi @tomaz4,

At first look, it appears that the “move()” function is a Runtime function that constantly updates the object’s position. If so, you’re constantly resetting the sprite sequence back to the first frame, so it will appear that it’s not animating. You need to pull the “play()” call outside of this Runtime routine and trigger it just once.

Hope this helps,

Brent Sorrentino

Hi, thank you for the effort. I did try that, but no effect. What I’m trying to do, is to move a sprite and as soon as it reaches the screen edge I’ll use another sequence. 

Is there another way to do this?

Can I increment in the animation function the current frame?

Hi @tomaz4,

Can you post the current code you have for this routine?

Brent

I did solve it in a way and it is working. I was using the isVisible method and switching between the two sprites. But now I cannot apply physics to object “player” … :frowning:

--HODI local walkRight = { width=106, height=138, numFrames=11, sheetContentWidth=212, sheetContentHeight=828 } local myImage = graphics.newImageSheet( "images/hoja.png", walkRight) local sequenceRight = { { name = "right", start=1, count=5, time=400 }, } local sequenceLeft = { { name = "left", start=7, count=5, time=400 }, } local right = display.newSprite( myImage, sequenceRight ) right.x = width/2 + 150 right.y = height-75 right.isVisible = false right:play() local left = display.newSprite( myImage, sequenceLeft ) left.x = width/2 left.y = height-75 left.isVisible = false left:play() --definiram class, da lahko na tega potem apliciram fiziko local player = {} player.left = left player.right = right player.switch = { 1, 2 } playerShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 } player.change = function() if player.switch == 1 then player.left.isVisible = true player.right.isVisible = false else player.left.isVisible = false player.right.isVisible = true end end physics.addBody(player, "dynamic", { density=3.0, friction=0.5, bounce=0.3, shape=playerShape } ) local dx = 4 local function turn(self) if ((self.x \< 47) or (self.x \> width - 47)) then dx = dx \* -1 end end local function animation() left.x = right.x right.x = right.x + dx if (dx\>0) then player.switch = 2 else player.switch = 1 end player.change() turn(right) end Runtime:addEventListener( "enterFrame", animation )

Hi @tomaz4,

What is the specific error here? Is the physics body even created? Can you print some details on what’s not working?

Thanks,

Brent

It says:

main.lua:81 bad argument #-1 to ‘addBody’ (/Proxy expected, got nil). This line is causing this:

physics.addBody(player, "dynamic", { density=3.0, friction=0.5, bounce=0.3, shape=playerShape } )

Hi @tomaz4,

In general, I see various “odd” coding methods here. You should be consolidating this to one sprite object, not two. You’re setting a new sprite object for “left” and “right”, but these should just be different sequences of the same sprite. The “change” function should change the sequence, not the actual sprite object.

This is probably causing the physics addBody() to fail too. You’ve defined “player” as a table, but it’s not a display object… so you’re trying to apply a physics body to a Lua table which isn’t a display object.

I’d suggest you rework this code a bit, since the basic method you’re aiming for seems simple enough. Once you do that, the rest (physics) should flow naturally into place. :slight_smile:

Brent

I did it before like this, just changing the sequence. But then I had the problem, that the sprite wasn’t changing the frames. It was stuck at the first frame of the sequence. :frowning:

then I have tryed to solve this with the workaround. :slight_smile: Which does look odd. But I did not have any other idea how to solve my original problem.

Hi @tomaz4,

At first look, it appears that the “move()” function is a Runtime function that constantly updates the object’s position. If so, you’re constantly resetting the sprite sequence back to the first frame, so it will appear that it’s not animating. You need to pull the “play()” call outside of this Runtime routine and trigger it just once.

Hope this helps,

Brent Sorrentino

Hi, thank you for the effort. I did try that, but no effect. What I’m trying to do, is to move a sprite and as soon as it reaches the screen edge I’ll use another sequence. 

Is there another way to do this?

Can I increment in the animation function the current frame?

Hi @tomaz4,

Can you post the current code you have for this routine?

Brent

I did solve it in a way and it is working. I was using the isVisible method and switching between the two sprites. But now I cannot apply physics to object “player” … :frowning:

--HODI local walkRight = { width=106, height=138, numFrames=11, sheetContentWidth=212, sheetContentHeight=828 } local myImage = graphics.newImageSheet( "images/hoja.png", walkRight) local sequenceRight = { { name = "right", start=1, count=5, time=400 }, } local sequenceLeft = { { name = "left", start=7, count=5, time=400 }, } local right = display.newSprite( myImage, sequenceRight ) right.x = width/2 + 150 right.y = height-75 right.isVisible = false right:play() local left = display.newSprite( myImage, sequenceLeft ) left.x = width/2 left.y = height-75 left.isVisible = false left:play() --definiram class, da lahko na tega potem apliciram fiziko local player = {} player.left = left player.right = right player.switch = { 1, 2 } playerShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 } player.change = function() if player.switch == 1 then player.left.isVisible = true player.right.isVisible = false else player.left.isVisible = false player.right.isVisible = true end end physics.addBody(player, "dynamic", { density=3.0, friction=0.5, bounce=0.3, shape=playerShape } ) local dx = 4 local function turn(self) if ((self.x \< 47) or (self.x \> width - 47)) then dx = dx \* -1 end end local function animation() left.x = right.x right.x = right.x + dx if (dx\>0) then player.switch = 2 else player.switch = 1 end player.change() turn(right) end Runtime:addEventListener( "enterFrame", animation )

Hi @tomaz4,

What is the specific error here? Is the physics body even created? Can you print some details on what’s not working?

Thanks,

Brent

It says:

main.lua:81 bad argument #-1 to ‘addBody’ (/Proxy expected, got nil). This line is causing this:

physics.addBody(player, "dynamic", { density=3.0, friction=0.5, bounce=0.3, shape=playerShape } )

Hi @tomaz4,

In general, I see various “odd” coding methods here. You should be consolidating this to one sprite object, not two. You’re setting a new sprite object for “left” and “right”, but these should just be different sequences of the same sprite. The “change” function should change the sequence, not the actual sprite object.

This is probably causing the physics addBody() to fail too. You’ve defined “player” as a table, but it’s not a display object… so you’re trying to apply a physics body to a Lua table which isn’t a display object.

I’d suggest you rework this code a bit, since the basic method you’re aiming for seems simple enough. Once you do that, the rest (physics) should flow naturally into place. :slight_smile:

Brent

I did it before like this, just changing the sequence. But then I had the problem, that the sprite wasn’t changing the frames. It was stuck at the first frame of the sequence. :frowning:

then I have tryed to solve this with the workaround. :slight_smile: Which does look odd. But I did not have any other idea how to solve my original problem.