How to use IsPlaying to set the value of a variable?

I want to be able to set the value of a variable (BallsbeingDest) depending on whether an animation is playing or not. This is what I have come up with but it’s not working. Any help is appreciated.

local sheetInfofruitblaster = require("Animations\_FruitBlaster") local animation\_fruitblaster = graphics.newImageSheet("images/Animations\_FruitBlaster.png", sheetInfofruitblaster:getSheet()) local sequences\_fruitblaster = { -- consecutive frames sequence { name = "default", start = 1, count = 5, time = 1100, loopCount = 1, loopDirection = "forward" } } local function playAnimationFruitBlaster(params) BallsbeingDest=1 local fruitblaster = display.newSprite(layers.matchanimations, animation\_fruitblaster,sequences\_fruitblaster) fruitblaster.x = params.x fruitblaster.y = params.y fruitblaster:play() function fruitblasterlistener(event) if (event.phase=="ended") then ---I can't use 'event.phase == began' because other stuffhappening elsewhere --- in the code might change BallsbeingDest ---which I don't want while the animation is playing-------- BallsbeingDest = 0 fruitblaster:removeSelf() fruitblaster = nil end end function fruitblasterwhatever() if sequences\_fruitblaster.isPlaying=="true" then print("bale") ---I am using this to see if it is working--- BallsbeingDest=1 end end Runtime:addEventListener("enterFrame", fruitblasterwhatever) fruitblaster:addEventListener("sprite", fruitblasterlistener) end

Hi @priyankamajumdar1981,

You’re very close… the “.isPlaying” property relates to the actual sprite, not the sequence. So instead of using this…

[lua]

sequences_fruitblaster.isPlaying==“true”

[/lua]

Use this…

[lua]

fruitblaster.isPlaying == true

[/lua]

Also note that .isPlaying is a boolean true, not the string “true”… so, no quotes around it.

Best regards,

Brent

Thank you so much for your quick reply.

 I tried your method but got an error.

''attempt to index upvalue ‘fruitblaster’(nil value)".

So I must have done something wrong but can’t figure out what. :unsure:

This is just a scope issue. Will there be more than one “fruitblaster” sprite on the screen at once? If there will only be one, then you should create it outside of the function, then refer to it inside the function.

If you are not familiar with scope, it’s an extremely important topic to understand in Lua. Here is a video which should help:

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

Take care,

Brent

Number of fruitblaster on screen at any time is random. There could be 10 or 5 or none at all.

I think the problem is that Runtime:addEventListener(“enterFrame”, fruitblasterwhatever) keeps looking for fruitblaster even after it has been removed. Hence the error. So I came up with the following code to be added at the end. Now I am not getting any error but since I am a total newbie, I am not sure if I am doing it right.

function fruitblasterwhatever() if fruitblaster==nil then do return end elseif fruitblaster.isPlaying==true then print("bale") ---I am using this to see if it is working--- BallsbeingDest=1 end end Runtime:addEventListener("enterFrame", fruitblasterwhatever) fruitblaster:addEventListener("sprite", fruitblasterlistener) end

Hi @priyankamajumdar1981,

Yes, this code needs to be fixed a little. In fact, you shouldn’t be applying a sprite listener in a Runtime (every frame) process… just add the listener to each sprite after you create it, and make sure that it references a function in the proper scope, somewhere above and outside of the function where you create the sprite. This will ensure that each “fruitblaster” (no matter how many) will trigger the various sprite animation phase events like “ended”.

Best regards,

Brent

I have tried not using the Runtime process but ran into another problem. Other stuff happening in the code elsewhere changes the Ballsbeingdest variable to 0 and that leads to stuff happening that I don’t want when fruitblaster is playing. The only solution I have come up with is to use the “every frame” when the fruitblaster is playing to prevent Ballsbeingdest from becoming 0.

But I have changed the code a little. Is this any better?

function fruitblasterchecker() if fruitblaster==nil then Runtime:removeEventListener("enterFrame", fruitblasterchecker) elseif fruitblaster.isPlaying==true then BallsbeingDest=1 print(BallsbeingDest) end end Runtime:addEventListener("enterFrame", fruitblasterchecker)

Hi again,

Well, I still think if you have multiple “fruitblasters”, you should not try to use just 1 reference named “fruitblaster”. This could easily cause other errors and problems as your proceed to more complex code. Each item should be regarded and logged, so Lua has no confusion on what exists and what doesn’t.

Take care,

Brent

The fruitblaster is basically an explosion that takes place every time a successful collision is made by the player.The player could be making one collision or a dozen or none at any time. I don’t have any control over how many of it will on onscreen at any time. :unsure: So I am not sure how to have more than one reference to multiple copies. I am just using the playAnimationFruitBlaster function to play the animation at the position of a successful collision. Maybe I am missing something here but I am completely stumped. Any ideas will be deeply appreciated.

If you’re just making any number of these sprites, then don’t worry about keeping track of them. Just generate them locally within the collision handler and, using a sprite “onComplete” listener, you can have each instance destroy itself when the animation is finished. I did exactly this in a game I made once and it works perfectly.

Brent

That’s a great idea. I’ll give it a try.

Thank you so much for sticking with my problem for all this while. I really appreciate it.

Hi @priyankamajumdar1981,

You’re very close… the “.isPlaying” property relates to the actual sprite, not the sequence. So instead of using this…

[lua]

sequences_fruitblaster.isPlaying==“true”

[/lua]

Use this…

[lua]

fruitblaster.isPlaying == true

[/lua]

Also note that .isPlaying is a boolean true, not the string “true”… so, no quotes around it.

Best regards,

Brent

Thank you so much for your quick reply.

 I tried your method but got an error.

''attempt to index upvalue ‘fruitblaster’(nil value)".

So I must have done something wrong but can’t figure out what. :unsure:

This is just a scope issue. Will there be more than one “fruitblaster” sprite on the screen at once? If there will only be one, then you should create it outside of the function, then refer to it inside the function.

If you are not familiar with scope, it’s an extremely important topic to understand in Lua. Here is a video which should help:

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

Take care,

Brent

Number of fruitblaster on screen at any time is random. There could be 10 or 5 or none at all.

I think the problem is that Runtime:addEventListener(“enterFrame”, fruitblasterwhatever) keeps looking for fruitblaster even after it has been removed. Hence the error. So I came up with the following code to be added at the end. Now I am not getting any error but since I am a total newbie, I am not sure if I am doing it right.

function fruitblasterwhatever() if fruitblaster==nil then do return end elseif fruitblaster.isPlaying==true then print("bale") ---I am using this to see if it is working--- BallsbeingDest=1 end end Runtime:addEventListener("enterFrame", fruitblasterwhatever) fruitblaster:addEventListener("sprite", fruitblasterlistener) end

Hi @priyankamajumdar1981,

Yes, this code needs to be fixed a little. In fact, you shouldn’t be applying a sprite listener in a Runtime (every frame) process… just add the listener to each sprite after you create it, and make sure that it references a function in the proper scope, somewhere above and outside of the function where you create the sprite. This will ensure that each “fruitblaster” (no matter how many) will trigger the various sprite animation phase events like “ended”.

Best regards,

Brent

I have tried not using the Runtime process but ran into another problem. Other stuff happening in the code elsewhere changes the Ballsbeingdest variable to 0 and that leads to stuff happening that I don’t want when fruitblaster is playing. The only solution I have come up with is to use the “every frame” when the fruitblaster is playing to prevent Ballsbeingdest from becoming 0.

But I have changed the code a little. Is this any better?

function fruitblasterchecker() if fruitblaster==nil then Runtime:removeEventListener("enterFrame", fruitblasterchecker) elseif fruitblaster.isPlaying==true then BallsbeingDest=1 print(BallsbeingDest) end end Runtime:addEventListener("enterFrame", fruitblasterchecker)

Hi again,

Well, I still think if you have multiple “fruitblasters”, you should not try to use just 1 reference named “fruitblaster”. This could easily cause other errors and problems as your proceed to more complex code. Each item should be regarded and logged, so Lua has no confusion on what exists and what doesn’t.

Take care,

Brent

The fruitblaster is basically an explosion that takes place every time a successful collision is made by the player.The player could be making one collision or a dozen or none at any time. I don’t have any control over how many of it will on onscreen at any time. :unsure: So I am not sure how to have more than one reference to multiple copies. I am just using the playAnimationFruitBlaster function to play the animation at the position of a successful collision. Maybe I am missing something here but I am completely stumped. Any ideas will be deeply appreciated.

If you’re just making any number of these sprites, then don’t worry about keeping track of them. Just generate them locally within the collision handler and, using a sprite “onComplete” listener, you can have each instance destroy itself when the animation is finished. I did exactly this in a game I made once and it works perfectly.

Brent