Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Sprite animation loops constantly? Need it to stop!

    Help
    6
    13
    498
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SwitchedOn
      SwitchedOn last edited by

      Heya guys,

      In my "Invaders" project, i have added a small explosion animation when an alien gets hit. But it stays on the screen looping over and over and I cant work out how to remove it.

      All the animation and display stuff works perfectly, I guess its just the logic of how to remove it and when. So I want it to loop once, then remove the sprite.

      I tried checking the current animation frame and doing something like (pseudo code);

      If (getSpriteAnimFrame(theSprite)==64) then
      Remove Sprite
      endIf

      But it never triggers. I've tried replacing the "64" with another getCall (of the max frames of the animation) but again nothing.

      I can't think of what else to do? Must be easy solution as its a common thing in games!

      Thanks for any help!

      Paul

      1 Reply Last reply Reply Quote 2
      • Martin
        Martin Fuze Team last edited by Martin

        What are you setting your sprite animation sequence to? I've not used sprite animations yet, but consider this...

         // 0 to 50 at 60fps
        setSpriteAnimation( enemy, 0, 50, 60 )
        if getSpriteAnimation(sprite) == 64 then
            // Will never trigger
            removesprite(sprite)
        endif
        

        Sorry, if it's obvious, but just check that your animation can actually get to 64 and that you haven't told it otherwise

        Beyond that i don't see anything desperately wrong with what you're doing in principal but like I say, I've not actually used sprite animations myself yet. I cheat, all of my enemies just rotate :)

        1 Reply Last reply Reply Quote 0
        • Martin
          Martin Fuze Team last edited by

          So, the other thing is that once you've removed your sprite I'm pretty sure you're quickly going to find that things crash because you can't loop over a non existent entity. I'm sure there are other ways round this but the simplest is to create a sprite that you never use, like so:

          nullSpr = createSprite()
          // Do whatever you need to do
          // In your loop you can now say
          for n = 0 to len(enemies) loop
              if enemies[n] != nullSpr then
                  // do all your stuff, including when you're done with it...
                  removeSprite(enemies[n])
                  enemies[n] = nullSpr
              endif
          repeat
          

          Hopefully I've got that the right way round. Important is to always remove your sprite before you assign it to null sprite, or indeed assign it to a newly created one. Otherwise you will end up breaking Fuze sooner or later (things get messy internally right now in that scenario but will get fixed!). The other nice thing about actually removing the sprite rather than just moving it offscreen or anything like that is that you then no longer need to care about it in terms of collision detection or anything like that and I tend to create functions like createPlayer(), createEnemy() so when I need to reset the level it's easy because I know I've removed all the old sprites so I just call those functions to create new ones and those functions can come with all the default properties for the player / enemy, like so...

          function createPlayer()
              p = createSprite()
              p.image = img    // image loaded previously
              setSpriteLocation(p, gWidth() / 2, gHeight / 2)
              // and all your other usual sprite creation commands
              p.lives = 3
              p.speed = 10
              // whatever other custom properties your player object might want
          return p
          
          waldron 1 Reply Last reply Reply Quote 2
          • waldron
            waldron F @Martin last edited by

            @Martin yeah i need to start doing this delete sprite its just taking the plunge

            1 Reply Last reply Reply Quote 0
            • M
              MikeDX last edited by MikeDX

              Just a side note

              The following code "should" do exactly the same and not use a sprite slot

              // Do whatever you need to do
              // In your loop you can now say
              for n = 0 to len(enemies) loop
                  if enemies[n] != false then
                      // do all your stuff, including when you're done with it...
                      removeSprite(enemies[n])
                      enemies[n] = false
                  endif
              repeat
              

              I just realised why this doesn't work and I am going to try to get it addressed in a future patch as to me it is very odd behaviour and 100% cannot be "by design"

              You cannot compare a sprite object to anything other than another sprite object, so setting it to false , null, void, 1, "nothing" or anything else will not work... hence the requirement for an empty sprite

              1 Reply Last reply Reply Quote 1
              • SwitchedOn
                SwitchedOn last edited by

                Thanks for all the replies, I'll check them out today!

                Just had a thought laying here after just waking up, and that is;

                // Do whatever you need to do
                // In your loop you can now say
                for n = 0 to len(enemies) loop
                if enemies[n].sprite != visible then
                If explosionSprite[n] frame count>60
                removeSprite(enemies[n])
                Endif
                endif
                repeat

                As only destroyed aliens will ever be exploding?...

                Anyways, thaanjs again. Will update how it's gone :)

                spikey 1 Reply Last reply Reply Quote 2
                • SwitchedOn
                  SwitchedOn last edited by

                  the above worked fine by the way, thanks for all the pointers @Martin and @MikeDX

                  1 Reply Last reply Reply Quote 0
                  • spikey
                    spikey F @SwitchedOn last edited by

                    @SwitchedOn sorry I did not look up the docs, but what is the variable visible? Is this pseudo code?

                    SwitchedOn 1 Reply Last reply Reply Quote 0
                    • D
                      doumdoum F last edited by

                      @SwitchedOn
                      As soon as i hit an enemy, i put it outside the screen area and make it static.

                      sprite.y = -10000// shoud be enough to avoid any collision with other sprites
                      setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
                      

                      I don't delete the sprite,

                      M 1 Reply Last reply Reply Quote 1
                      • SwitchedOn
                        SwitchedOn @spikey last edited by

                        @spikey said in Sprite animation loops constantly? Need it to stop!:

                        @SwitchedOn sorry I did not look up the docs, but what is the variable visible? Is this pseudo code?

                        Yea it is, but its a 'get'

                        getSpriteVisibility(spriteName)

                        Can also use a 'set'

                        setSpriteVisibility(sprintName, Boolean)

                        1 Reply Last reply Reply Quote 1
                        • M
                          MikeDX @doumdoum last edited by

                          @doumdoum said in Sprite animation loops constantly? Need it to stop!:

                          @SwitchedOn
                          As soon as i hit an enemy, i put it outside the screen area and make it static.

                          sprite.y = -10000// shoud be enough to avoid any collision with other sprites
                          setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
                          

                          I don't delete the sprite,

                          Whilst this works, I wouldn't approve of this as a best practice way of doing things.

                          SwitchedOn 1 Reply Last reply Reply Quote 0
                          • SwitchedOn
                            SwitchedOn @MikeDX last edited by

                            @MikeDX said in Sprite animation loops constantly? Need it to stop!:

                            @doumdoum said in Sprite animation loops constantly? Need it to stop!:

                            @SwitchedOn
                            As soon as i hit an enemy, i put it outside the screen area and make it static.

                            sprite.y = -10000// shoud be enough to avoid any collision with other sprites
                            setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
                            

                            I don't delete the sprite,

                            Whilst this works, I wouldn't approve of this as a best practice way of doing things.

                            Yep, could end up being a bit of a memory hog - plus as they are still active to a degree, could cause issues in detections etc.

                            1 Reply Last reply Reply Quote 0
                            • D
                              doumdoum F last edited by

                              In practice, in a space invader/galaxian game, i have a constant number of enemies (50 max).
                              I recycle the sprites on the next stage, only by changing properties.(ecolo coding)
                              Since the sprites are static and far away, they don't collide with bullets, Ships etc.
                              I do the same with bullets, explosions.
                              I admit it's a rough method, but it works well.
                              Give me stack and list, and i'll make it better.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post