Find and Seek with Collision Detection
Hello AS3 Community,
a long time ago I used to play around in Flash, and recently have been trying to get back in to . So far the basics have come back to me just fine. I am tinkering with a little simulation right now and am having the hardest time with something and thought maybe someone out there could lend me a hand:
I am attempting to make a simulation wherein a selection of objects (seekers) automatically move towards another designated point (finder). So far i've got the moment down pretty well, just comparing the x's and y's and using IF statements to make them move until they match. The second part of the simulation though involves the use of a third object (walls) to impede the seekers. I just cant seem to get it to work. Here is the code im using. Forgive the fact that it might not be as simple as it could be:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class main extends MovieClip {
var home: Home = new Home();
var seekerArray: Array = new Array();
var wall: Wall = new Wall();
var seekerTimer: Timer = new Timer(1000)
public function main() {
startGame();
stage.addEventListener(Event.ENTER_FRAME, seek);
seekerTimer.addEventListener(TimerEvent.TIMER, addSeekers);
seekerTimer.start();
}
public function startGame(): void {
wall.x = stage.stageWidth * .5;
wall.y = stage.stageHeight * .5 * .5;
addChild(wall);
home.x = stage.stageWidth * .5;
home.y = stage.stageHeight * .5;
addChild(home);
}
public function addSeekers(e: TimerEvent): void {
var seeker: Seeker = new Seeker();
flyer.x = Math.floor(Math.random()* (550-50)+30);
flyer.y = Math.floor(Math.random()* (550-50)+30);
addChild(seeker);
flyer.hitvar = 1;
seekerArray.push(flyer);
}
public function seek(e: Event): void {
for (var s: int = 0; s < seekerArray.length; s++) {
if (seekerArray
.x > home.x) {seekerArray
.x -= 2;}
if (seekerArray
.y > home.y) {seekerArray
.y -= 2;}
if (seekerArray
.x < home.x) {seekerArray
.x += 2;}
if (seekerArray
.y < home.y) {seekerArray
.y += 2;}
}
}
}
}
I have tried putting in all the sorts of hittest stuff I can remember but none of it works, or it works too well and the seekers just stop in their tracks. Ultimately I would love for them to, when they hit the wall, then forgo trying to get to their original destination and just focus on getting around the Wall then resuming their search.
Thank you in advanced
-TRE
