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.
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?
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” …
--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 )
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.
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.
then I have tryed to solve this with the workaround. Which does look odd. But I did not have any other idea how to solve my original problem.
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.
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?
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” …
--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 )
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.
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.
then I have tryed to solve this with the workaround. Which does look odd. But I did not have any other idea how to solve my original problem.