Change :setSequence Spriteanimation

I have a sprite animation with 3 different sequence data like this:

....... local sequenceData = { {name = "one", frames={1}, time = 1000,loopCount = 1}, {name = "two", frames={2,3,4,5,6}, time = 2000,loopCount = 1}, {name = "three", start = 7,count = 25,time = 3000,loopCount = 1}, } .......

Here is the situation:

1- mysprite:setSequence(“one”)

2- some transition.to happen  , after that i do:

local myListener = function ( self, event ) if( event.phase == "ended" ) then self:setSequence("one") end return true end myspritei:setSequence("two") mysprite.sprite = myListener mysprite:addEventListener( "sprite" ) mysprite:play() mysprite = nil

3- on a button click i want third animation to happen

mysprite:setSequence(“three”)

but program crash this time

P.s:everything works fine until i implement second step

Do you have any idea how to help me out solve this please?

Hi @blablu1212,

This seems like a potential scope issue. Have you studied about Lua scope and its importance? It’s one of the most crucial aspects when learning Lua, so I recommend that you become very comfortable with the concept.

Here is a video to begin with:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Take care,

Brent

Hi Brent,

I saw the entire video, thank you :slight_smile:

Yes i am aware of scope concept and i declare mysprite as an upvalue variable.

So i am using same var but i am changing setSequence different times.

Well, I notice you have a syntax error and specify the variable as:

myspritei

and not the correct way:

mysprite

Sorry @Brent,

i have miss typed it here, but there is no such extra “i” in my code

Why are you setting “mysprite” to nil?

Because i want to change its sequence back and forth

Changing the sequence is just doing that action on the same sprite, and thus the same object/variable.

Brent

i tried removing mysprite = nil

and it works fine, step nr.2 is fixed , thanks Brent :slight_smile:

but its not behaving good for step nr.3

the code for step nr.3 works alone, but not after implementing this logic.

When i press the button setSequence(“three”) experience run normally but after its finished it calls sequence(“one”)

and there is no code that calls it

Hi @blablu1212,

Actually, there is code which calls “one” again. :slight_smile: Basically this is happening:

  1. You have set up a sprite event listener, and that refers to the function “myListener()” (this is fine).

  2. Within “myListener()”, you listen for the “ended” phase. (this is fine).

  3. The “ended” phase is triggered when any sequence finishes playing. So, when you play sequence “three”, it completes, and then the “myListener()” function plays “one” again.

Brent

Hi @Brent,

I really appreciate your help on this  :slight_smile:

 

Its true what you’re pointing out on nr (3)

So how i solved the issue is by setting what i want to achieve under “next” phase

if event.phase == "next" then self:removeEventListener( "sprite" ) self:setSequence("one") end 

This gets next frame = 2 , do you know how can i ask if frame=5 is ended ,to do all the logic?

 

I was looking at this link about event phases, tried to ask for next many times, but didnt work

http://docs.coronalabs.com/api/event/sprite/phase.html

Hi @blablu1212,

I assume you’ve seen the sprite guide, but in case you haven’t:

http://docs.coronalabs.com/guide/media/spriteAnimation/index.html

In any case, you’ll get much better control of everything if you use both the “event.phase” and “.sequence” of the sprite.

http://docs.coronalabs.com/api/type/SpriteObject/sequence.html

In this way, you can ask (for example), “If this sprite is currently on sequence three (sprite.sequence == 3) , and that sequence finished playing (ended phase), then (switch to another sequence, or stop, or restart the sequence, etc.)”.

Brent

Yeah i was looking around for something like obj.sequence ,i didn’t know its existence

i think this will work, need to try it :slight_smile:

Thanks so much @Brent!

Hi @blablu1212,

This seems like a potential scope issue. Have you studied about Lua scope and its importance? It’s one of the most crucial aspects when learning Lua, so I recommend that you become very comfortable with the concept.

Here is a video to begin with:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Take care,

Brent

Hi Brent,

I saw the entire video, thank you :slight_smile:

Yes i am aware of scope concept and i declare mysprite as an upvalue variable.

So i am using same var but i am changing setSequence different times.

Well, I notice you have a syntax error and specify the variable as:

myspritei

and not the correct way:

mysprite

Sorry @Brent,

i have miss typed it here, but there is no such extra “i” in my code

Why are you setting “mysprite” to nil?

Because i want to change its sequence back and forth

Changing the sequence is just doing that action on the same sprite, and thus the same object/variable.

Brent