ActionScript多对象实例化hitTestPoint

时间:2011-05-20 06:42:11

标签: flash actionscript flash-cs5

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

}

}

1 个答案:

答案 0 :(得分:1)

将目标添加到数组中以跟踪:

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();
            }
        }
    }
相关问题