ForumsProgramming ForumNeed help whit Enemies!

2 3983
SeoMX
offline
SeoMX
89 posts
Nomad

Does anyone knows how to make enemies appear randomly??? (like in "Storm the House&quot

  • 2 Replies
nonkodesign
offline
nonkodesign
1 posts
Nomad

Are you working with Actionscript? If yes, you can set the _x and _y properties of any object on creation, or after being created with a number of different commands based on the way you have chosen to program the game. I won't go into syntax but to generate a random x coordinate, set your x value of the object (enemy) to a random value. Most 'lay areas' will have bounds within the actual viewing area and you need to take this into account. For instance if your play area starts at an x coordinate of 25 pixels and it ends at 600 pixels from 0, then your play area is 575 pixels wide and starts at 25. so your random x coordinate would be _x = 25 + random(575). This will likely cause unwanted clipping at the minimum and maximum coordinates so you may need to add and subtract the width of the object as well to make it look good. These exact same details apply to the y coordinate as well. Hope that helps a bit.

minibeast198
offline
minibeast198
38 posts
Nomad

Make your enemy a movie clip, and set the actions for the movie clip as the following:

onClipEvent(enterFrame) {
randomX = Math.random() * (highX - lowX)) + lowX;
randomY = Math.random() * (highY - lowY)) + lowY;
this._x = randomX
this._y = randomY
}


Replace "highX" with the x-value of the farthest right you'd want an enemy appear. Likewise, replace "lowX" with the x-value of the farthest to the left you'd want an enemy appear. Do the same with "highY" (lowest on the stage you'd want enemy to appear) and "lowY" (highest on the stage you'd want enemy to appear.) Hope this works, I'm not 100% sure this is correct.

Showing 1-2 of 2