instantiate box runtime
-
@SteveZX81 unfortunately on google I find little about it programming with fuze
-
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
-
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 -
@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
-
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
-
@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
-
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
-
@Retrocade_media I did such a thing
//pseudo codeloop 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 -
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
andcreateImage
), 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 callclear()
, that way you do not need to keep track of the boxes at all. They just get drawn to theframebuffer
(or an image you created, if you also need to draw things that do need to be cleared). -
@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.
-
@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
-
@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.