ActionScript 多对象实例化 hitTestPoint

发布于 2024-11-08 03:38:16 字数 1736 浏览 0 评论 0原文

我遇到的问题基本上是,我在舞台上实例化“目标”对象五次。这种情况正在发生,但只有一个对象能够对其执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-11-15 03:38:16

将您的目标添加到数组中以进行跟踪:

private function target:Array = new Array();

private function newTarget(numOfTargets:uint):void {
        for (var i:int = 0; i < numOfTargets; i++) {
            target[i] = new Target;
            target[i].x = 100 * i + 70;
            target[i].y = 100;
            target[i].scaleX = target[i].scaleY = .7;
            addChild(target[i]);
            trace(target[i]); // Adds 5 objects to stage

            //**updated**
            target[i].addEventListener(MouseEvent.CLICK, collisionDetection); 
        }
}

现在您可以引用每个单独的目标:

private function collisionDetection(e:MouseEvent):void {

var target:MovieClip = MovieClip(e.target); //**updated**

        if (target.hitTestPoint(mouseX, mouseY, true)) {
            collisionNotification.text = "Collision";
            removeTarget();
            currentScore.text = String(int(score));
        } else {
            collisionNotification.text = "No collision";
            decreaseScore();
            currentScore.text = String(int(score));
            // Check if score is equal to -200 and if it is, show game over screen
            if (score == -500) {
                Mouse.show();
                main_class.showGameOver();
            }
        }
    }

Add your targets to an array to keep track of then:

private function target:Array = new Array();

private function newTarget(numOfTargets:uint):void {
        for (var i:int = 0; i < numOfTargets; i++) {
            target[i] = new Target;
            target[i].x = 100 * i + 70;
            target[i].y = 100;
            target[i].scaleX = target[i].scaleY = .7;
            addChild(target[i]);
            trace(target[i]); // Adds 5 objects to stage

            //**updated**
            target[i].addEventListener(MouseEvent.CLICK, collisionDetection); 
        }
}

now you can refer to each individual target:

private function collisionDetection(e:MouseEvent):void {

var target:MovieClip = MovieClip(e.target); //**updated**

        if (target.hitTestPoint(mouseX, mouseY, true)) {
            collisionNotification.text = "Collision";
            removeTarget();
            currentScore.text = String(int(score));
        } else {
            collisionNotification.text = "No collision";
            decreaseScore();
            currentScore.text = String(int(score));
            // Check if score is equal to -200 and if it is, show game over screen
            if (score == -500) {
                Mouse.show();
                main_class.showGameOver();
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文