Navigation

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

    Bouncing Sprites From Array Around the Screen

    Beginners
    4
    7
    431
    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.
    • N
      NashiyamaHP last edited by NashiyamaHP

      Hello everyone!

      I’m working on a game where I have some randomly generated enemy sprites in an array. They were created using a for loop similar to how the sprites were created in the tutorial sprite game.

      During my main game loop, when my main player sprite collides with an item, I start spawning the enemies in order using a simple if statement and a counter.

      N=0
      Spawn the sprite enemy[n].spr
      N += 1
      When a new collision is detected, spawn the next one from the array. Etc...

      Everything is working fine and spawning the sprites from the array and they are moving in the assigned velocities, but once they are getting to the edge of the screen they just keep going. Now I know I need to use something like “enemySpeed = -enemySpeed” in order to reverse the direction or At least this how it worked in the tutorials. Should I be using a for loop in the main game loop section? Or separate if statements outside of a for loop? I’m a little confused about how to proceed since not all of the sprite’s locations and speeds have been set at one time like they were in the sprite game tutorial. I’m only setting them once each item is collected. I’m not at home now so I can’t get a post copy of my code just yet, sorry.

      Thanks..

      N 1 Reply Last reply Reply Quote 0
      • N
        Nisse5 F @NashiyamaHP last edited by

        @NashiyamaHP To me, it sounds like you should use the main loop and scan/loop through all the enemies with a "for" loop, and change direction on the enemies that needs to be changed.

        N 1 Reply Last reply Reply Quote 0
        • N
          NashiyamaHP @Nisse5 last edited by

          @Nisse5

          Thanks for the reply.

          Using a for loop to scan the enemies on the screen is kinda of what I suspected I needed and actually wrote some code doing just this but for some reason nothing changed when I ran it and the enemy sprites just continued right off the screen hehe.

          It didn’t spit out any errors either so maybe I need to try again. Will a “for” loop work in this case even if all of the sprites in the array haven’t had their locations and speeds set yet using the setSpriteLocation() and setSpriteSpeed() functions?

          pianofire 1 Reply Last reply Reply Quote 0
          • pianofire
            pianofire Fuze Team @NashiyamaHP last edited by pianofire

            @NashiyamaHP Can you post a screen shot of your code? You can share this via Twitter or save it to an SD card.

            Did you see the example of handling sprites that I posted at the weekend?

            https://fuzearena.com/forum/topic/437/fun-with-sprites

            N 2 Replies Last reply Reply Quote 0
            • N
              NashiyamaHP @pianofire last edited by

              @pianofire

              I’m gonna play with my code a bit tonight and if I can’t get it to work I’ll take a screenshot for a follow up post. Thanks.

              1 Reply Last reply Reply Quote 1
              • N
                NashiyamaHP @pianofire last edited by

                EB497B38-4A6B-4E90-A96E-0B8C3B9CDB37.jpeg
                F9D449DE-28EF-4589-9EBF-F96E771173D1.jpeg @pianofire

                Ok. So I played around a bit last night with no luck. Please excuse the quality of the screens. I’m at work and its the best I could do for now.

                The two photos show the section of code for 1) populating the enemy array depending on the sprite image, and 2) the section of code that sets the sprite location and speed of the next enemy in the array when the target item is collected. (think of it like the the little food pellet in the classic snake game. Eat the food, new enemy spawns.)

                I’ve tried several things to make the enemies bounce around the screen but to no avail. One enemy type should bounce horizontally, one vertically, and one on both axes. I understand the concept of reversing the velocity because of the tutorial but I’m having a hard time getting my head around it in this particular application. Thanks.

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

                  Hello there! From what I can see there it looks like you're doing a createSprite() and setSpriteImage() in the main game loop. This is causing the sprites to be overwritten and recreated each frame. We only want to do those two things once, outside of the loop in our setup (or, if you are generating enemies as the game continues, you will need this to be in a function which is called at certain intervals in time.

                  To get a bunch of rocks floating around the screen and bouncing off the walls, see below (although there are a bunch of things you might want to change here):

                  // load image, set scale and size global variables (very useful throughout)
                  img = loadImage("Untied Games/Asteroid A")
                  scale = 2
                  rsize = tileSize(img, 0) * scale
                  
                  // create the array to store our rocks, each one is a structure comprising of a sprite handle, a position vector and a direction vector
                  array rocks[20] = [
                      .spr = -1, // temporary value to be overwritten with createSprite()
                      .pos = {},
                      .dir = {},
                      .active = true
                  ]
                  
                  // array of direction vectors, makes it easy to choose a random one - this could be much more varied,
                  // or you could use random() to calculate a really random direction
                  dirs = [
                      {1, 1},
                      {-1, -1},
                      {-1, 1},
                      {1, -1}
                  ]
                  
                  // for loop which populates the rocks array with data
                  for i = 0 to len(rocks) loop
                      rocks[i].spr = createSprite() // set a blank sprite in the .spr property (the sprite handle) for each rock
                      setSpriteImage(rocks[i].spr, img) // set the sprite image to the sprite handle
                      setSpriteAnimation(rocks[i].spr, 0, 39, 10 ) // start tile of 0, end tile of 39, 10 fps
                      rocks[i].pos =  random(gwidth() - rsize.x / 2) + rsize.x, random(gheight() - rsize.y * 2) + rsize.y} // choose a start position at a random point on screen with a border to stop rocks being slightly off-screen
                      rocks[i].dir = dirs[random(len(dirs))] // choose a random direction from our dirs array
                  repeat
                  
                  // main loop - we only want to draw the sprites and apply movement calculation etc here. 
                  // now the setup is done, we don't want to do any more sprite creation.
                  
                  loop
                      clear()
                      updateSprites()
                  
                      // for loop to count over each rock
                      for i = 0 to len(rocks) loop
                          if rocks[i].active then // only do these things if a rock is active
                              setSpriteScale(rocks[i].spr, {1, 1} * scale)
                              rocks[i].pos += rocks[i].dir * 0.8 // this multiplier will speed up/slow down the rock movement
                  
                              // now our wall collision if statements, first the x axis:
                              if rocks[i].pos.x + rsize.x / 2 > gwidth() or
                                 rocks[i].pos.x - rsize.x / 2< 0 
                              then
                                  rocks[i].dir.x = -rocks[i].dir.x // reverse the x direction for the rock
                              endif
                  
                              // then the y:
                              if rocks[i].pos.y + rsize.y / 2 > gheight() or
                                 rocks[i].pos.y - rsize.y / 2< 0 
                              then
                                  rocks[i].dir.y = -rocks[i].dir.y // reverse the y direction for the rock
                              endif
                  
                              // finally, apply movement
                              setSpriteLocation(rocks[i].spr, rocks[i].pos)   
                          endif
                      repeat
                  
                      drawSprites()
                      update()
                  repeat
                  

                  This should give you 20 animated rocks flying around the screen, albeit with very fixed directions. You could certainly improve that very easily! Let me know if I've been unclear.

                  I think the problem is primarily with having the setup stuff in the main loop.

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