Drawing multiple of the same sprites?
-
I need to know how to draw multiple of the same sprites on screen at the same time in different locations and I dont know what to do?
-
I'm assuming that you want to treat all these sprites as individual things to move/be collided with etc.
What you'll need is an array which contains the sprites, then you can assign properties to each of those sprites and manage them using a loop. A set up like this should get you started:
// We'll begin by loading an image file for your sprite enemiesImg = loadImage("Untied Games/Creeper") // Let's imagine you want to use these as enemies in a game maxEnemies = 20 // variable to store the maximum number of enemies array enemies[maxEnemies] // declare the array - this one has 20 elements since we use the maxEnemies variable // now loop over the array and put a sprite in each element for i = 0 to maxEnemies loop enemies[i] = createSprite() // once you've done this, you can now edit the automatic properties of each sprite, or even add your own custom ones setSpriteImage(enemies[i], enemiesImg) enemies[i].x = random( gwidth() ) // give each sprite a random position on the x axis enemies[i].y = random( gheight() ) // give each sprite a random position on the y axis enemies[i].x_speed = random( 40 ) - 20 // give each enemy a random movement speed on the x axis in the rage of -20 (moving left) to 20 (moving right) enemies[i].y_speed = random( 40 ) - 20 // give each enemy a random movement speed on the y axis in the rage of -20 (moving up) to 20 (moving down) repeat // With that done, we now have 20 enemy sprites, each one with the Creeper enemy image, each with a randomly chosen position on the x and y axis // Now, in your main loop loop clear() updateSprites() // you will need this function in your loop if you want to apply things like x_speed and y_speed, or animation. // you can manipulate the sprite values here, for example: for i = 0 to maxEnemies loop if enemies[i].x > gwidth() or enemies[i].x < 0 then enemies[i].x_speed = -enemies[i].x_speed endif // if a sprite hits the left or right side of the screen, reverse the x direction if enemies[i].y > gheight() or enemies[i].y < 0 then enemies[i].y_speed = -enemies[i].y_speed endif // if a sprite hits the top or bottom side of the screen, reverse the y direction repeat drawSprites() update() repeat
Of course this is all using randomly chosen positions. If you want something like your enemies to spawn over time, you'll need a couple of extra bits and bobs. Let me know what you're after and I'll see if I can help some more!
Hope this is useful to you :)
-
I've been looping watching over the tutorials for basic arrays, for loops and the 2 structure vids for the last 48 hours trying to decide how I should start with this also, only with mine I just want blocks (top down map just like an empty screen but with blocks to make it harder to catch the enemy without being a closed in Maze.) I need collision only else I would have used box.
Thanks DavePs
I assume if I want to set each block position manually which I do that I replace the [I] in the for loop with [*one number between 0 and 19] x 20 times -
@Maxwello Sounds like building a map in the map editor and using collision boxes would be the way to go for you! Then you just do
collideMap()
and some x and y speed cancellation and bob should be your uncle.Take a look at "Super FUZE Platformer" and the way that Will is doing the collision with the level. The exact same will apply only in top down fashion.
-
@Dave I did build a map with the editor but it was hard to not be wonky (for me) but I got one to try then I erm.. Couldn't work out how to load it in lmao.
I'll have another look ty -
@Maxwello At the start of your program call
loadMap("mapName")
, then before yourupdate()
line in the main loop, calldrawMap()
That will draw the entire map, all layers etc. If you want to achieve something like parallax scrolling (scrolling different layers at different speeds), you'll need to draw the layers individually. For a top down game, this shouldn't be a concern though!
-
@Dave thanks Dave I did that and it looked like a blank screen so perhaps it was a layering issue, now you have Confirmed that's the way I can keep trying knowing I'll succeed eventually thanks again
I may have had drawmap and update map back to front. (puts on boxing mitt.. Downward thrust) -
@Maxwello You might have issues with your sprite camera if that's the case. I would recommend taking a good look at the FUZE Platformer program. I would direct you to a tutorial for this, but currently there isn't one. This is being worked on however! You will have a better resource for this in the future.
-
@Dave oh it's cool I think one of these threads had some 2d camera advice.
On the subject of tutorials some seem to be greyed out throughout have you noticed this? -
@Maxwello Which tutorials do you mean dude? I'm not sure what you mean by grayed out - do you mean tutorials on here in FUZE Arena or within Fuze itself?
-
@Dave they have probably always been this way and I've just had a massive brain malfunction
Edit
OK sussed it omg as soon as I saw the photo then glanced back at the switch it clicked its cos I have a bunch of code greyed out in my code with the /* */Edit2
Sorry if this is common sense I guess I just hadn't been in this position before
I just didn't know sorry and I've been straining my eyes with it too lol
-
@Maxwello Hmmm - this is strange. Initially I thoght "Ah! you've found an issue, I'll fix that" - but I took a look at this tutorial on Fuze and this grayed out effect is not on mine. We will look into this. Out of interest - what happens when you copy and paste that block of code into a Fuze program? (Move the cursor using the d-pad to that code block so it is highlighted with the white border, then press the Y button to copy the code. "Copied code to clipboard!" should appear as a message at the bottom of the screen. Then, go to an empty project and paste the code from your clipboard in there.
-
@Dave it pastes in fine.. Seems that it occurs only if you have any of the commented out woth the /*. */
Even if it's just like
/*hello
Within an entire project if you then open help.
I guess it sees the help as an extension of the project?
-
Oh, that's definitely a bug! Thank you.
-
Great info - thanks @Maxwello !