How to reverse spritesheet animation direction?

Hi all, I have the code below which animates four images contained in a spritesheet when the image is touched, starting with image 1 and stopping on image 4.

What I really would like to do would be to start with image 4 showing on the screen and, when it's tapped, start the animation backwards so that it ends with image 1.

Thanks in advance for any advice,

Laddsa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  require("sprite")
  display.setStatusBar(display.HiddenStatusBar)
 
  function touchimage (event) 
    if (event.phase =="began") then
        event.target:prepare("forward")
        event.target:play()
    end
  end
   
  local data = require("images").getSpriteSheetData()
  local sheet = sprite.newSpriteSheetFromData("images.png",data)
                
  local myimage = sprite.newSpriteSet(sheet,1,4)
  sprite.add( myimage, "forward", 1 ,4, 350, 1) 
              
  local card = sprite.newSprite(myimage)
  card.x = 60
  card.y = 100
                        
  myimage:addEventListener( "touch", touchimage)

I believe sprite.add( myimage, "forward", 4 ,-4, 350, 1) should do the trick. I remember seeing something about it in the docs but it's a pain in the ass to check on a smartphone.

Hi many thanks for the code - I tried it, but unfortunately got the following error message: 'sequence frames must be inside the sheet'.

I also checked the API refernce area of the website just now and it gives the same line of code too, so am surprised that it didn't work.

Might be a problem with my spritesheet, I'll recreate it and try again!

Best wishes,

Laddsa

Actually use your code "sprite.add( myimage, "forward", 1 ,4, 350, 1)" but change the code to "sprite.add( myimage, "forward", 1 ,4, 350, -1)" for one time back and forth or "sprite.add( myimage, "forward", 1 ,4, 350, -2)" for continuous back and forth.

Thanks tomchristman24447,

That's close to what I would like as it plays the direction backwards when it gets to the last image frame, but ideally what I require is just the animation starting at image 4 and going backwards when it's tapped. Do you think this is possible?

Best wishes,

Laddsa

It might be easier to modify the sprite sheet to include the images backward at the end.

Ah yes, good idea, I think that's the best way to proceed.

Many thanks,

Laddsa

So at the moment, there is no way of reversing the order of the animation through code?

views:1636 update:2011/10/15 21:01:16
corona forums © 2003-2011