As3分裂攻击

时间:2018-04-05 18:09:16

标签: actionscript-3 addchild

我正在制作一个玩家必须躲避攻击的游戏,这些攻击来自波浪,在第二波中,我希望一个物体从屏幕顶部掉落,当它撞到地面时分成两个独立的左右两边的碎片。

目前在第二次浪潮中我的物体掉落了,但是我不能让它分开并左右移动。

var gameTimer:Timer = new Timer(85000, 1); // game lasts the length of the song
var thesong:SoundChannel = new SoundChannel();
var songsnd:song = new song();
var nextObject:Timer;
var count:int = 0;
var objects:Array = new Array();
var wave:int = 1; // attack.
stage.addEventListener (Event.ENTER_FRAME, game);

function game(e:Event) { 
    stage.removeEventListener (Event.ENTER_FRAME, game);
    thesong = songsnd.play();
    setGameTimer();
    setNextObject();
    addEventListener(Event.ENTER_FRAME, moveObject);
}
function setGameTimer():void {
    trace("GAME TIMER STARTED");
    gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, nextattack);
    gameTimer.start();
}
function setNextObject():void { // depending on which attack the speed is different.
    if(wave == 1) {
        nextObject = new Timer(1000, 1);
    } else if(wave == 2) {
        nextObject = new Timer(1500, 1);
    } else {
        nextObject = new Timer(250, 1)
    }
    nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);
    nextObject.start();  
}

function newObject(e:TimerEvent) {
    //create next object
    var r2:int = Math.random() * 2 + 1;
    if(wave == 1) {
        var classRef:Class= getDefinitionByName("ATCK01") as Class;
    } else if (wave == 2) {
        var classRef:Class= getDefinitionByName("ATCK02") as Class;
        // var classRef:Class= getDefinitionByName("Split") as Class;
    } else {
        var classRef:Class= getDefinitionByName("ERR") as Class;
    }

    var newObject:MovieClip = new classRef();

    if (wave == 1) {
        if(r2 == 1) {
            newObject.typestr = "slideL";
            newObject.x = Rwall.x; // right wall
            newObject.y = Player.y;
            addChild(newObject);
            objects.push(newObject);
        } else if(r2 == 2) {
            newObject.typestr = "slideR";
            newObject.x = Lwall.x; // left wall
            newObject.y = Player.y;
            addChild(newObject);
            objects.push(newObject);
        }
    } else if (wave == 2) {
        newObject.typestr = "fallD";
        newObject.x = Player.x;
        newObject.y = Twall.y;
        addChild(newObject);
        objects.push(newObject);
    }
    count++; // this section is what changes the current attack wave
    if (count == 48) { wave = 2; }
    if (count == 55) { wave = 1; }

    setNextObject();
}

function moveObject(e:Event) { // speed at which each attack goes and the pattern
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        if (objects[i].typestr == "fallD") {
            objects[i].y +=5;//5 is speed
            if (objects[i].y > 357) { // this is where the floor is
                removeChild(objects[i]);
                objects.splice(i, 1);
                // here is where I try to add the split and change the sprite to 'split'
                // where the typestr should be slideL and slideR
            }
        } else if (objects[i].typestr == "slideL") {
            objects[i].x -=5;
            if (objects[i].x < 100){
                removeChild(objects[i]);
                objects.splice(i, 1);
            }
        } else if (objects[i].typestr == "slideR") {
            objects[i].x +=5;
            if (objects[i].x > (Rwall.x+50)) {
                removeChild(objects[i]);
                objects.splice(i, 1);
            }
        }
    }
}

我认为我的问题出在moveObject上,我不太确定如何进行拆分我当前的尝试是否以错误结束

1 个答案:

答案 0 :(得分:0)

在我在fallD中评论过的moveObject中,我在尝试中错过了newObject:

            var classRef1:Class = getDefinitionByName("Split") as Class;
            var newObject1:MovieClip = new classRef1();
            newObject1.typestr = "slideL";
            newObject1.x = xLoc; // x location before the split
            newObject1.y = Bwall.y;
            addChild(newObject1);
            objects.push(newObject1);
            var classRef2:Class = getDefinitionByName("Split") as Class;
            var newObject2:MovieClip = new classRef2();
            newObject2.typestr = "slideR";
            newObject2.x = xLoc; 
            newObject2.y = Bwall.y;
            addChild(newObject2);
            objects.push(newObject2);