AS2: How to spawn movieclips within drawn bounds?

3 posts

Flag Post

I need help in my “project I’ve just started” using AS2.

I’m trying to make a “minigame” where clickable markers generates within an area
and the player just needs to click it before it goes away to score, It’s like OSU!Though it doesn’t involve music and rythme.

My problem is: Markers are spawning all over the place “when what i just want to happen is to spawn them within the shaded area/movieclip, like I’m giving them a boundary”. I can’t proceed with other stuffs for this game due to this being the most important part of it.

I think all I have to do is modify these lines, but I don’t know the right thing to input/change:

//field is the shaded box movieclip for containing spawned markers, giving them bounds.
_root.field.attachMovie("crosshair_event","en"+runningtime,runningtime);
_root.field["en"+runningtime]._x = int(Math.random()*field._height);
_root.field["en"+runningtime]._y = int(Math.random()*field._width);

Thanks in advance for any help.

 
Flag Post

field is the shaded area where you want the things to spawn.
thing is the thing you want to spawn.
The + field.x is only if the field isn’t at (0,0)

thing.x = field.x + (Math.random()*field.width)
thing.y = field.y + (Math.random()*field.height)

EDIT: I found that you mixed up width and height. Swap them around.

 
Flag Post

You could spawn the marker, and then check if it is within the specified area. If it is, you leave it as it is, if it is not, reposition it and check again. (Use a while loop for this). This especially useful if the boundary is not a rectangle.