Looping through sprites 10-15

Hello, I have the code below which animates a sprite in my app. My questions is, once the sprite has played through once, is there a way to just animate sprites 10-15?

In this case, the dog is preparing to run in sprites 1-9 and in 10-15 the dog is running. After sprites 1-9 play through once, I want 10-15 to play over and over again.

1
2
3
4
5
6
7
8
9
10
11
12
local sheet1 = sprite.newSpriteSheet( "images/dog.png", 85, 92 )
         
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 15)
sprite.add( spriteSet1, "dog", 1, 15, 1500, 0 ) 
         
local dog= sprite.newSprite( spriteSet1 )
dog.x = 140
dog.y = 275     
physics.addBody( dog, "dynamic", { friction=0.5, bounce=0 } ) 
         
dog:prepare("dog")
dog:play()      

Yes, add to line 5:

sprite.add(spriteSet1,"running",10,15,500,0)

and when you want to 'shift gears' sort to speak, just do:

dog:prepare("running")
dog:play()

and he will cycle those frames.

Hi Paul!!
one more question about that.
How can I show automaticly the sprite "b" after the sprite "a" has excecuted?

the code is similar:

1
2
3
4
5
6
7
8
9
10
11
12
13
local sheet1 = sprite.newSpriteSheet( "image1.jpg", 250, 400 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 10)
sprite.add( spriteSet1, "a", 1, 6, 170, 5)
sprite.add( spriteSet1, "b", 7, 10, 500, 1)
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth / 4 + 16
instance1.y = baseline - 75
instance1.xScale = .5
instance1.yScale = .5
instance1:prepare("a")
instance1:play()
--instance1:prepare("b") ????
--instance1:play()  ????

I believe you are looking for spriteInstance:addEventListener(). Listen for end of animation then switch over.

Hi TheRealTonyK, thanks for response.

I think the code below has what you talk about, but it doesn't work, what I'm missing?.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local sheet1 = sprite.newSpriteSheet( "image1.jpg", 250, 400 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 10)
sprite.add( spriteSet1, "a", 1, 6, 170, 5)
sprite.add( spriteSet1, "b", 7, 10, 3000, 1)
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth / 4 + 16
instance1.y = baseline - 75
instance1.xScale = .5
instance1.yScale = .5
instance1:prepare("a")
instance1:play()
instance1:addEventListener( "end", nextSprite)
 
local function nextSprite(event)
    --instanc1:removeEventListener("sprite")
        instance1:prepare("b")
        instance1:play()
    return true
end

I'll test soon, but please check:

local sheet1 = sprite.newSpriteSheet( "image1.jpg", 250, 400 )

this is stating that your FRAME width = 250 and height = 400.
Is this really what you want or did you put the width and height of the complete sprite sheet image?

Also should be
instance1:addEventListener("sprite", nextSprite)

These numbers are ok, I think are pixels, because the sprite works perfectly. The sheet's measures are 500x2000

Also must check event.phase

local function nextSprite(event)
if (event.phase == "end") then
instance1:prepare("b")
instance1:play()
end

return true
end

that should do it. works on my end.

forgot to mention the nextSprite function needs to be moved up:

here is my quick n dirty code, (replaced with a sprite sheet i had laying around)

local sprite = require("sprite")

local instance1 = nil

local function nextSprite(event)
if (event.phase == "end") then
instance1:prepare("b")
instance1:play()
end

return true
end

local sheet1 = sprite.newSpriteSheet( "Wizard3Blue_anim.png", 35, 45 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 35)
sprite.add( spriteSet1, "a", 1, 8, 370, 5)
sprite.add( spriteSet1, "b", 12, 15, 300, 1)
instance1 = sprite.newSprite( spriteSet1 )
instance1.x = 300 -- display.contentWidth / 4 + 16
instance1.y = 300 --baseline - 75
--instance1.xScale = .5
--instance1.yScale = .5
instance1:prepare("a")
instance1:play()
instance1:addEventListener( "sprite", nextSprite)

Perfect!!!!! great solution!!
Now I can run a sprite, then other sprite.
I tried to go on with the same "tecnique", but I couldn't.
The final idea is run an sprite, at the end of the first one runs another sprite, and at the end of the second sprite display an image.
This is the code, just the part wich shows the last image doesn't work.
Could you see what I'm missing please?.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local sprite = require("sprite")
local instance1 = nil
local instance2 = nil
 
local function displayimage(event)
        if (event.phase == "end") then
                instance2:removeEventListener( "sprite", nextSprite)
                --************Last image***********
                -- ***FASE 3*******
                local bkg = display.newImage( "icon.png", true )
                bkg.x = display.contentWidth / 4 + 100
                bkg.y = baseline - 75
                --*********************************
        end
        return true
end
 
local function nextSprite(event)
        if (event.phase == "end") then
                instance1:removeEventListener( "sprite", nextSprite)
-- ***FASE 2*******
                local sheet2 = sprite.newSpriteSheet( "fase2.jpg", 125, 200 ) 
                local spriteSet2 = sprite.newSpriteSet(sheet2, 1, 8)
                sprite.add( spriteSet2, "b", 3, 6, 2300, 1)
                instance2 = sprite.newSprite( spriteSet2 )
                instance2.x = display.contentWidth / 4 + 16
                instance2.y = baseline - 75
                instance2:prepare("b")
                instance2:play()
                instance2:addEventListener( "sprite", displayimage)
        end
        return true
end
 
--*******FASE1********************************
local sheet1 = sprite.newSpriteSheet( "fase1.jpg", 125, 200 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 8)
sprite.add( spriteSet1, "a", 1, 8, 170, 3)
instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth / 4 + 16
instance1.y = baseline - 75
instance1:prepare("a")
instance1:play()
instance1:addEventListener( "sprite", nextSprite)
--********************************************

I'll try to test something in a bit, but I do not think removing an event listener inside the event is a good idea.

Here ya go.

local sprite = require("sprite")

local instance1 = nil

local function playSpriteMovie()
local instance1 = nil

local function nextSprite(event)
if (event.phase == "end") then
if(event.sprite.sequence == "a") then
print("switching to b")
--instance1:pause()
instance1:prepare("b")
instance1:play()
elseif(event.sprite.sequence == "b") then
instance1:pause()
local bkg = display.newImage("Icon.png", true)
bkg.x = 300
bkg.y = 300
timer.performWithDelay(200, instance1:removeEventListener("sprite", nextSprite))
end
end

return true
end

local sheet1 = sprite.newSpriteSheet( "Wizard3Blue_anim.png", 35, 45 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 35)
sprite.add( spriteSet1, "a", 1, 8, 370, 5)
sprite.add( spriteSet1, "b", 12, 15, 200, 1)
instance1 = sprite.newSprite( spriteSet1 )
instance1.x = 300 -- display.contentWidth / 4 + 16
instance1.y = 300 --baseline - 75
--instance1.xScale = .5
--instance1.yScale = .5
instance1:prepare("a")
instance1:play()
instance1:addEventListener( "sprite", nextSprite)
end
playSpriteMovie()

remember to dispose and cleanup...didn't have time to add that code...:)

Amazing work!
Thanks so much TheRealTonyK.

Now I can use all the aprite I have. To improve this, this could be a very simply question, but is very important. How can I manage the images in terms of order? (front image, back image, etc).
While I'm showing the sprites, I want to show an image, that has to be in front of all other images all the time.

How can I manage these properties?

One thing to add with your sprite work, you may want to look into sprite.newSpriteMultiSet.

As far as layering goes (z-order), it is something I wish was better implemented but the tip i recieved on these forums was proper grouping. It is stage one of effecting z order, but hard to implement during run-time.

1
2
3
4
5
6
7
8
levelGroup = display.newGroup()
backgroundGroup = display.newGroup()
--add some objects into backgroundGroup
levelGroup:insert(backgroundGroup)
 
topGroup = display.newGroup()
--add someobjects into topGroup
levelGroup:insert(topGroup)
views:2116 update:2011/10/4 17:12:07
corona forums © 2003-2011