Hi! I need to be able to drop many sprites as my character runs along and then have each time itself like a bomb, change as it goes and perform an action when it times out.
Im not sure how to create this instanced object with behaviour in blocks… any advice?
There are a few ways to do something like this.
Option 1: Use the arcade-sprite-data extension
The arcade-sprite-data extension lets you store arbitrary data on a sprite; you can add it by clicking the Extensions category in the toolbox (the new blocks will appear in the bottom of the sprites category).
Using this extension is the most flexible way you can keep track of state, but it’s also the most complicated code-wise.
Here is some sample code:
Option 2: use the “lifespan” property
Each sprite has a “lifespan” property you can access using the “set x to ___” block in the sprites category. This property takes in a number of milliseconds and will automatically destroy the sprite when that amount of time has passed.
This is the simplest code-wise, but it doesn’t work very well if you have multiple states you want your sprites to go through. For example, if you want the sprite to show an explosion at the end then you need to create another sprite inside an “on sprite destroyed” event.
Here is the same example using lifespan instead:
Option 3: use the timers extension
The timers extension lets you schedule bits of code to run in the future. You can call it multiple times to schedule different events to happen one after the other.
This approach works well, but you need to be careful NOT to use any global variables. I recommend you do everything inside a function that takes the sprite as a parameter to avoid any of the common gotchas.
Here is the same example using timers:
1 Like