1

我想知道是否可以重置/重新加载/重建数组顺序?

我正在制作这个“太空入侵者”游戏,当游戏重新启动时,敌人需要重新启动到它的位置。当我击落敌人并重置游戏时,我杀死的敌人一直消失。

所以这里有一些负责的代码:

var spiderArray:Array = new Array(enemyField.enemy1,enemyField.enemy2,
                             enemyField.enemy3,enemyField.enemy4,
                             enemyField.enemy5,enemyField.enemy6,
                             enemyField.enemy7,enemyField.enemy8,
                             enemyField.enemy9,enemyField.enemy10,
                             enemyField.enemy11,enemyField.enemy12,
                             enemyField.enemy13,enemyField.enemy14,
                             enemyField.enemy15,enemyField.enemy16,
                             enemyField.enemy17,enemyField.enemy18,
                             enemyField.enemy19,enemyField.enemy20,
                             enemyField.enemy21,enemyField.enemy22,
                             enemyField.enemy23,enemyField.enemy24,
                             enemyField.enemy25,enemyField.enemy26,
                             enemyField.enemy27,enemyField.enemy28,
                             enemyField.enemy29,enemyField.enemy30,
                             enemyField.enemy31,enemyField.enemy32,
                             enemyField.enemy33,enemyField.enemy34,
                             enemyField.enemy35,enemyField.enemy36,
                             enemyField.enemy37,enemyField.enemy38,
                             enemyField.enemy39,enemyField.enemy40,
                             enemyField.enemy41,enemyField.enemy42,
                             enemyField.enemy43,enemyField.enemy44,
                             enemyField.enemy45,enemyField.enemy46,
                             enemyField.enemy47,enemyField.enemy48,
                             enemyField.enemy49,enemyField.enemy50,
                             enemyField.enemy51,enemyField.enemy52,
                             enemyField.enemy53,enemyField.enemy54,
                             enemyField.enemy55,enemyField.enemy56,
                             enemyField.enemy57,enemyField.enemy58,
                             enemyField.enemy59,enemyField.enemy60,
                             enemyField.enemy61,enemyField.enemy62,
                             enemyField.enemy63,enemyField.enemy64,
                             enemyField.enemy65,enemyField.enemy66);

现在是杀死敌人的地方:

function enemyHitTest():void {
//for each of the three spiders
for(var i:int = 0; i < spiderArray.length; i++) {
    //the each of the six bullets
    for(var j:int = 0; j < 6; j++) {
        //don't consider bullets that aren't in play:
        if(bulletArray[j].y > SpelerMC.y) continue;
        if(spiderArray[i].hitTestObject(bulletArray[j])) {
            score += 10;
            scoreTxt.text = score.toString();
            trace("Invader " + i + " neergeschoten!");
            spiderArray[i].parent.removeChild(spiderArray[i]);
            bulletArray[j].x = j * 70 + 100;
            bulletArray[j].y = 595;
        }
    }
}

现在我想我需要放一些theArray.pop();东西,但不知道如何使用它,但我需要把它放在这个函数中:

function startGame() {
trace("Start het spel opnieuw...");
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
enemyField.x = 400;
enemyField.y = 160;
SpelerMC.x = 83;
SpelerMC.y = 531;
}

请帮我!已经搜索了5个小时。
提前致谢!

4

2 回答 2

0

删除元素编号i使用array.splice(i, 1);http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#splice().com/en_US/FlashPlatform/reference/actionscript/3/Array.html# splice%28%29)但请记住, 重置数组array.length会减少,只需再次调用
spidersArray = new Array(enemyField.enemy1, etc)


enemyHitTest函数中更新if(enemyArray[i].visible && enemyArray[i].hitTestObject(laserArray[j]))我将删除条件更改为and enemyArray[i].parent.removeChild(enemyArray[i]);toenemyArray[i].visible = false;
并添加

function respawnEnemies():void{
    for(var i:int = 0; i < enemyArray.length; i++) {
        enemyArray[i].visible = true;
    }
}

调用它startGame

完整代码在这里

upd 2
所以问题不在于重置数组,而在于您的enemyField 及其敌人被手动添加到舞台并以编程方式删除,因此没有代码可以调用以将它们带回来

于 2012-10-13T23:44:29.640 回答
0

如果您的所有实例都应该保留 - 例如,您在开始时生成所有演员,然后所有演员都被重用(在新游戏中),您可以设置它们的数组并在每次开始游戏时制作副本并删除“死”敌人从复制的数组

更好的选择是使用向量,例如

var enemies:Vector.<Enemy> = Vector.<Enemy>([]);//in brackets references to the instances of Enemy class
var inGame:Vector.<Enemy> = enemies.concat();

还有5小时的研究?请耐心等待,更加努力,

此致

于 2012-10-14T20:28:15.233 回答