ActionScript 3 - 数组问题中对象的碰撞检测

时间:2015-10-08 04:52:57

标签: actionscript-3 flash collision-detection

我正在开发一款小型Flash游戏,目前正在尝试为多达4名玩家实施多人游戏。虽然1个玩家的碰撞检测效果很好,但当添加更多玩家时只有最后一个玩家可以与其他对象发生碰撞

使用跟踪语句我发现从主类调用有问题的玩家的x和y坐标会返回初始的x和y位置,而不是当前的坐标(trace(players[0].x + "/" + players[0].y);),从播放器类(trace(this.x + "/" + this.y);)内调用它们时,总是给出正确的值。

最后一位玩家将始终在两个类中返回正确的坐标。下面是主要类的骨架。

public class main extends MovieClip {

    public var collisionObject: Array = new Array(14);
    public var players: Array = new Array();
    private var noOfPlayers = 2;

    public function main() {
        for (var i = 0; i < noOfPlayers; i++) {
            setupPlayer(i);
            stage.addChild(players[i]);
        }
        setupCollisionObject();
        stage.addEventListener(Event.ENTER_FRAME, checkForCollision);
    }

    private function setupCollisionObject() {
      /* Determines positions of and spawns objects */ 
    }

    private function setupPlayer(playerNo) {
        switch (playerNo) {
            case 3:
                players[3] = new player(1000, 576, 180, 104, 100, 102);
            case 2:
                players[2] = new player(24, 576, 0, 73, 74, 76);
            case 1:
                players[1] = new player(1000, 384, 180, 38, 37, 39);
            case 0:
                players[0] = new player(24, 384, 0, 87, 65, 68);
        }
    }

    public function checkForCollision(e: Event) {

        trace("x: "+players[0].x+" y: "+players[0].y);
        trace("x: "+players[1].x+" y: "+players[1].y);

        for (var i in players) {
            for (var j in collisionObject) {
                if (players[i].hitTestObject(collisionObject[j])) {
                    //damage player
                }
            }
        }
    }
}

我不知道为什么会这样。

1 个答案:

答案 0 :(得分:0)

break;中你的交换机案例中缺少setupPlayer(),这会导致所有玩家在最后一次重新初始化setupPlayers(i)期间重新初始化。

private function setupPlayer(playerNo) {
    switch (playerNo) {
        case 3:
            players[3] = new player(1000, 576, 180, 104, 100, 102);
            break;
        case 2:
            players[2] = new player(24, 576, 0, 73, 74, 76);
            break;
        case 1:
            players[1] = new player(1000, 384, 180, 38, 37, 39);
            break;
        case 0:
            players[0] = new player(24, 384, 0, 87, 65, 68);
            break;
    }
}