The problem I am having is basically, I am instantiating the ‘Target’ object five times on the stage. This is happening, but only one object is able to have hitTestPoint performed on it. Can anyone help as to why this is happening?
Here is the code:
UPDATED
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.text.*;
public class TheGameItself extends Sprite {
public var main_class:TheGame;
private var crosshair:Crosshair = new Crosshair;
private var targets:Array = new Array();
public function TheGameItself(passed_class:TheGame) {
main_class = passed_class;
addTargets(4);
Mouse.hide();
addChild(crosshair);
crosshair.x = mouseX;
crosshair.y = mouseY;
addEventListener(MouseEvent.MOUSE_MOVE, moveCrosshair);
}
private function addTargets(numOfTargets:int):void {
for (var i:int = 0; i < numOfTargets; ++i) {
targets[i] = new Target;
targets[i].x = 100 * i + 70;
targets[i].y = 100;
targets[i].scaleX = targets[i].scaleY = .7;
addChild(targets[i]);
addEventListener(MouseEvent.CLICK, collisionDetection);
}
}
private function collisionDetection(e:MouseEvent):void {
targets.forEach(detectCollision);
}
private function detectCollision(element:*, index:int, targets:Array):void {
if (element.hitTestPoint(mouseX, mouseY, true) && targets.length > 0) {
trace("["+index+"]"+" "+element);
collisionNotification.text = "Yes";
} else {
collisionNotification.text = "No";
}
}
public function moveCrosshair(e:MouseEvent):void {
crosshair.x = mouseX;
crosshair.y = mouseY;
}
}
}
Add your targets to an array to keep track of then:
now you can refer to each individual target: