Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
i don't see where you're doing any check for wall.
in general, using hittestobject works well with vertical and horizontal rectangles. however, if one or more object is not rectangular in shape, you'll probably want to use a shape-based hittest using the bitmapdata's hittest.
Copy link to clipboard
Copied
Thanks for the reply. I had left out the hit testing since it wasn't working. I've since simplified the code --hit test included-- maybe this should help:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class crossScreen extends MovieClip {
var target: Target = new Target();
var wallArray: Array = new Array();
var seeker: Seeker = new Seeker();
public function crossScreen() {
startGame();
stage.addEventListener(Event.ENTER_FRAME, seek);
}
public function startGame(): void {
for (var w: int = 0; w < 3; w++) {
var wall: Wall = new Wall();
wall.x = 150 + (36 * w);
wall.y = 125 + (50 * w);
wall.rotation = w * 90
addChild(wall)
wallArray.push(wall)
}
target.x = 200;
target.y = 100;
addChild(target)
seeker.x = 20;
seeker.y = 150;
addChild(seeker)
}
public function seek(e: Event): void {
if (seeker.hitTestObject(wallArray[0]) || seeker.hitTestObject(wallArray[1]) || seeker.hitTestObject(wallArray[2])) {
seeker.y += 2;
} else {
if (seeker.x > target.x) {
seeker.x -= 2;
}
if (seeker.y > target.y) {
seeker.y -= 2;
}
if (seeker.x < target.x) {
seeker.x += 2;
}
if (seeker.y < target.y) {
seeker.y += 2;
}
}
}
}
}
Basically whats happening is that the seeker object seeks out the target fine, and when it hits the first wall (wallArray[0]) it tracks downward till it clear it then it goes back on to seeking the target. The problem arises in that wallArray[1] is horizontal and underneath the target object. So when the seeker object encounters it, and its x position is the same as that of the target, it just continually hits it, bounces back, then hits it again.
here is the link to a projector of the file so you can see what I mean:
https://dl.dropboxusercontent.com/u/62536946/CrossScreen.exe
-TRE
Copy link to clipboard
Copied
a few things:
1. store the previous seeker position. when you hit a wall/obstacle, return to that previous position and then make an obstacle evading position update.
2. you need to use some intelligence in evading obstacles. simply incrementing a seeker's y by 2 isn't going to work.
3. with a vertical obstacle, you should be able to increment/decrement the y property (after returning to the previous position).
4. with a horizontal obstacle, you should be able to increment/decrement the seeker x property (after returning to the previous position).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now