So in a fighting game project, I have code that kind of looks like this for a type of projectile:
scene.onHitWall(SpriteKind.Projectile, function (sprite, location) {
animation.stopAnimation(animation.AnimationTypes.All, sprite)
sprite.ay == 0
sprite.vy == 0
sprite.vx == 0
sprite.setVelocity(0, 0)
pause(500) //or really any amount of time
sprite.setImage(assets.image`yoshieggbreak`)
}
})
What’s supposed to happen is when the projectile hits a wall, it stops, switches to a different image (e.g. exploding or broken), pauses a bit to show the image, then after time has passed destroy itself.
What ends up happening though is the projectile hits a wall, stops, the entire game lags for a bit, then the projectile is destroyed without ever displaying the image.
Can you help me figure this out and find a solution?
1 Like
When you pause inside a function such as onHitWall, it pauses everything. This is because the function is not internally running inside a control.runInParallel function. This is the function that allows other code to run while the pause function is running. To fix this, you can run the code inside the control.runInParallel function, but this will cause issues as this function will be set off every frame that your sprite is hitting the wall. Instead, you can set the image and then set the sprite’s “lifespan” variable. This will automatically destroy the sprite after the specified number of milliseconds. This way there is no pausing needed inside the onHitWall function at all!
1 Like
use the timer extension use the after 500 do and put that at the top like this
scene.onHitWall(SpriteKind.Projectile, function (sprite, location) {
timer.after(500, function () {
sprite.setImage(assets.image`yoshieggbreak`)
})
animation.stopAnimation(animation.AnimationTypes.All, sprite)
sprite.ay == 0
sprite.vy == 0
sprite.vx == 0
sprite.setVelocity(0, 0)
}
})