Navigation

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

    instantiate box runtime

    Beginners
    6
    16
    375
    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.
    • Dave
      Dave Fuze Team last edited by Dave

      Why not use an array variable to store the properties for all of your boxes, then use the box() function inside a for loop to draw them? This way you can manipulate each box.

      // initialise an array of structures - create properties for the boxes, initalise values at 0 as we'll set them to random in a for loop below
      array boxes[10] = [
          .x = 0,
          .y = 0,
          .width = 0,
          .height = 0,
          .colour = 0
      ]
      
      // give the properties a random value for each 
      for i = 0 to len( boxes ) loop
          boxes[i].x = random( gwidth() )
          boxes[i].y = random( gheight() )
          boxes[i].width = random( 100 )
          boxes[i].height = random( 100 )
          boxes[i].colour = {random(1.0), random(1.0), random(1.0), 1} // an RGBA vector with random red, green and blue channels and "1" for alpha
      repeat
      
      
      // main loop
      loop
          clear()
          
          for i = 0 to len( boxes ) loop
              boxes[i].y += 1 // this will move all boxes down the screen, for instance
              box( boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height, boxes[i].colour, false )
          repeat
      
          update()
      repeat
      
      C 1 Reply Last reply Reply Quote 1
      • Dave
        Dave Fuze Team last edited by Dave

        Another method is to use the Fuze shape functions. This will allow you to store a shape in a variable and use various functions to manipulate it.

        Check out these help pages:

        https://fuzearena.com/help/view/createBox
        https://fuzearena.com/help/view/setShapeLocation
        https://fuzearena.com/help/view/drawShape

        1 Reply Last reply Reply Quote 0
        • C
          chucky831 @Dave last edited by chucky831

          @Dave to manipulate them I had created a data structure. All I need is to instantiate the box every few seconds in update without sleep() function

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

            So you're looking to create boxes every few seconds rather than begin the program with all of them? Sounds like a function to populate a field in that array of structs would be good for it.

            Maybe add a .active property to the structure too to keep track of the boxes which are active:

            array boxes[10] = [
                .x = 0,
                .y = 0,
                .width = 0,
                .height = 0,
                .colour = 0,
                .active = false
            ]
            

            A function to make one could look like this:

            function makeBox()
                for i = 0 to len( boxes ) loop
                    if !boxes[i].active then
                        boxes[i].x = where you want the box on the x axis
                        boxes[i].y = where you want the box on the y axis
                        boxes[i].width = whatever you want the width as
                        boxes[i].height = whatever you want the height as
                        boxes[i].colour = {random(1.0), random(1.0), random(1.0), 1}
                        boxes[i].active = true // don't forget to set the active flag so it isn't overwritten
                    endif
                repeat
            return void
            

            You could call this function every few seconds using a % in the main loop.

            frame = 0
            
            loop
                clear()
                
               // make a box every 3 seconds
                if frame % 240 == 0 then
                    makeBox()
                endif
            
                for i = 0 to len( boxes ) loop
                    box( boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height, boxes[i].colour, false )
                repeat
            
                update()
                frame += 1
            repeat
            
            spikey 1 Reply Last reply Reply Quote 1
            • Retrocade_media
              Retrocade_media F @chucky831 last edited by

              @chucky831 make a self made timer?

              //This will be every one second (not counting delta time) assuming we stay at 60 frames per second
              BoxTimer = 60
              
              Loop
                  If BoxTimer > 0 then
                      BoxTimer -= 1
                  Endif
              
                  If BoxTimer <= 0 then
                      BoxTimer = 60
                      //Do your other stuff here
                  Endif
              Repeat
              
              1 Reply Last reply Reply Quote 1
              • Retrocade_media
                Retrocade_media F last edited by

                I should note, Daves method is gonna be better in the long run. However, my method works from the frame you decide to launch it, if you add a variable to stop and start it

                1 Reply Last reply Reply Quote 0
                • C
                  chucky831 last edited by chucky831

                  @Retrocade_media I did such a thing
                  //pseudo code

                  loop
                  
                  for i=0 to 10 loop
                  if(time > delay) then
                  box(....)
                  endif
                  
                  repeat
                  update()
                  repeat
                  

                  doing so rightly the falling pits get stuck. This is what I don't understand how to deal with fuze.
                  I should generate boxes indefinitely only that I don't find a way to do it with fuze
                  Thanks for your help

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

                    I might have misunderstood the question, but here is my attempt of answering it anyway:

                    If you draw the box on an image (using setDrawTarget and createImage), then create a sprite that has the created image as the image. And set the properties of the sprite to implement the behavior you want (for example falling down).

                    This way you could keep creating sprite, but you'd still need to keep track of the sprites you've created, to also remove them when you're done with it.

                    I know @pianofire had implemented a linked-list solution somewhere. That might be a useful data structure for this scenario, to remove sprites from the linked list after the sprite has been removed from screen with removeSprite

                    EDIT:
                    if you mean to draw static boxes, than you could choose not to call clear(), that way you do not need to keep track of the boxes at all. They just get drawn to the framebuffer (or an image you created, if you also need to draw things that do need to be cleared).

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

                      @Dave I just recently wondered if creating a box shape before the main loop, storing it in a variable and then just moving is better or if its ok to call box() in every loop iteration. I was worried that calling box() in every mainloop iteration would allocate new memory that is never released again. But seeing your example I am confident to use the box() function more often, it could save me some lines.

                      1 Reply Last reply Reply Quote 2
                      • C
                        chucky831 last edited by chucky831

                        @PB____ I can add an object to an array like:

                        boxes = obj
                        

                        it does not give me an error but it gives me the length of the array equal to 0

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

                          @chucky831 you can do boxes[len(boxes)] = obj, means you need to give the next item in the array as the index. Here if there are 3 items already in boxes as an example, boxes[0], boxes[1] and boxes[2], then len(boxes) is equal to 3. and boxes[3] = obj will add the 4th item. or you use the len() function directly as I did at the beginning of my comment.

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