Spawning obstacles
-
I am making a simple avoiding obstacles game. Currently, the player can be moved with the left joystick. I am now starting to add obstacles. My idea is to have an array that contains a structure for every obstacle. This structure has an X and Y comment which is responsible for holding the position of the obstacle. However, I am struggling to add these structures to the array. Is there a better way to do this? What is the best way to spawn obstacles at random positions on the screen? I am a beginner so baby talk, please.
The shared game code is TWC3MNDHX
Thank you in advance -
I’m thinking that instead of adding structures to your array, you can add sprites directly. Sprites already have a .x and .y automatically. Also, sprites are special because you can add new custom parameters on the fly, like this:
s = createSprite() s.myParam = 42 s.anotherParam = “smurf“
-
I was assuming that you are working with a 2D game, otherwise using sprites makes less sense (although it can still be useful).
-
And to answer your question as to how you add them to an array, one can for instance do this:
var myArray = [] for u = 0 to 10 loop s = createSprite() // Do more sprite init here myArray[len(myArray)] = s repeat
-
@holly8760 hey, the download code is 1 character short can't download :/
-
@Holly8760 Also I can't see your program awaiting approval (for a download code to work you must submit the program so that we can approve it for community sharing, otherwise only your switch friends will be able to download it).
If you've not submitted it you can do so by going to the Share section from the home menu, pressing R twice to get to My Shared Programs, then pressing A on the program you want to submit and selecting Submit. :)
-
@kat, Ah I did not realize I needed to do that. Thank you, I'll do that from now on.
-
@vinicity, Wow thanks for the help! I've never used sprites before but I'm excited to try.
-
@holly8760 Check out the sprite tutorials if you haven't already:
-
One very useful function that they do not mention in these tutorials is,
detectspritecollision(sprite_A, sprite_B)
This will detect a collision and return true if the specified sprites are colliding.
If you have trouble with the collision boxes then use this function,
setspritecollisionshape()