ActionScript 多对象实例化 hitTestPoint
我遇到的问题基本上是,我在舞台上实例化“目标”对象五次。这种情况正在发生,但只有一个对象能够对其执行 hitTestPoint。谁能帮忙解释为什么会发生这种情况?
这是代码:
更新
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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的目标添加到数组中以进行跟踪:
现在您可以引用每个单独的目标:
Add your targets to an array to keep track of then:
now you can refer to each individual target: