是否有人有兴趣查看我的游戏代码?我收到了奇怪的错误

时间:2015-11-29 16:34:24

标签: actionscript-3 flash

我试图制作我的第一个真正简单的射击游戏。我已经在线阅读了教程,而不是直接复制代码,我试图将事情提升到另一个层次,以便真正理解代码并添加我自己的代码。考虑到这一点,我最近一直在学习阵列,不确定我是否正确使用它们。

我变得奇怪,间歇性

Error #2025 DisplayObject must be a child of the caller

输出窗口中的消息,而不是编译器窗口。

所以,我不知道产生这个问题的代码行。通过注释掉代码块,我将其缩小到标有"CLEANUP MISSED ENEMIES""BULLET RATE OF SHOOTING"的模块,但是为什么'超出了我的理解。

我相信会有很多很棒的评论指出我遇到的错误和错误。我珍惜每一个学习的机会,所以请在您认为合适的时候给出您的意见!我打赌有更好的方法可以做我正在做的事情!

package{

import flash.events.Event;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.ColorTransform;
import fl.motion.Color;
import flash.display.DisplayObject;

public class chopper extends MovieClip{
public function chopper(){
    boot();
}
public function boot():void{

/////////VARS
var coptr:MovieClip = new copter();
var bulit:MovieClip = new bullit();
var mouseIsDn = false;
var speed = 0;
var PEW = false
var meter:MovieClip = new meters();

var bltArray:Array = new Array();

var airArray:Array = new Array();
var gndArray:Array = new Array();

var gameTIMERa:Timer = new Timer(5000);
var gameTIMERb:Timer = new Timer(10000);

stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
stage.addEventListener(MouseEvent.MOUSE_UP, unclicked);

function clicked(e:Event):void{
    mouseIsDn = true;
}
function unclicked(e:Event):void{
    mouseIsDn = false;
}

/////////INTRO SCREEN
var titl:MovieClip = new title();
addChild(titl);
var strt:MovieClip = new start();
addChild(strt);
var govr:MovieClip = new gOVER();

var ctINTRO:Color = new Color();
ctINTRO.setTint(Math.random()*0xFFFFFF, 0.5);
BG.transform.colorTransform = ctINTRO;

/////////MUSIC
var ChanAB:SoundChannel = new SoundChannel();
var Amusic:Sound = new musicA();
var Bmusic:Sound = new musicB();
ChanAB = Amusic.play(0, 9999);

/////////SFx
var ChanSFx:SoundChannel = new SoundChannel();
var Ashoot:Sound = new shootA();

var SFx:Array = new Array();
var Asuck:Sound = new suckA();
SFx.push(Asuck);
var Bsuck:Sound = new suckB();
SFx.push(Bsuck);
var Csuck:Sound = new suckC();
SFx.push(Csuck);

var gOver:Sound = new over();

/////////START
strt.startBTN.addEventListener(MouseEvent.CLICK, str);
function str(e:Event):void{
    removeChild(titl);
    removeChild(strt);
    ChanAB.stop();
    BG.transform.colorTransform = new ColorTransform;
    ////////////
    ChanAB = Bmusic.play(0, 9999);
    addChild(coptr);
    TweenLite.to(coptr, 3, {x:157, y:316});
    addChild(meter);
    meter.x = 861;
    meter.y = 9;
    TweenLite.to(meter, 1, {x:735});
    meter.life.gotoAndPlay(2);
    gameTIMERa.addEventListener(TimerEvent.TIMER, addAIR);
    gameTIMERa.start();
    gameTIMERb.addEventListener(TimerEvent.TIMER, addGND);
    gameTIMERb.start();
}

/////////ADDING ENEMIES
function addAIR(e:TimerEvent):void{
    var Aair:MovieClip = new airA();
    Aair.x = 805;
    Aair.scaleX = .25
    Aair.y = Math.random() * stage.stageHeight - Aair.height;
    Aair.scaleY = .25
    airArray.push(Aair);
    addChild(Aair);
}

function addGND(e:TimerEvent):void{
    var Agnd:MovieClip = new gndA();
    Agnd.x = 805;
    Agnd.scaleX = .25
    Agnd.y = 430 + Math.floor(Math.random() * 36);
    Agnd.scaleY = .25
    gndArray.push(Agnd);
    addChild(Agnd);
}



addEventListener(Event.ENTER_FRAME, startLoop);
function startLoop(e:Event):void{
    if(coptr.x == 157 && coptr.y == 316){
        speed = 10;
        meter.life.gotoAndStop(10);
        removeEventListener(Event.ENTER_FRAME, startLoop);
        addEventListener(Event.ENTER_FRAME, gameLoop);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, shoot);
    }
}

/////////COPTER SHOOTING
function shoot(e:Event):void{
    if(PEW == false){
        ChanSFx = Ashoot.play();
        bulit.x = coptr.x + 5;
        bulit.y = coptr.y;
        bltArray.push(bulit);
        addChild(bulit);
        PEW = true;
    }
}

/////////LIFE METER
function lifeMeterA(e:Event = null):void{
    if(meter.life.width > 59){
        meter.life.scaleX -= .04;
    }
    else if(meter.life.width < 59 && meter.life.width > 29){
        meter.life.gotoAndStop(11);
        meter.life.scaleX -= .04;
    }
    else if(meter.life.width < 29 && meter.life.width > 15){
        meter.life.gotoAndStop(12)
        meter.life.scaleX -= .04;
    }
    else if(meter.life.width < 15 && meter.life.width > 1.5){
        meter.life.gotoAndPlay(2);
        meter.life.scaleX -= .04;
    }
}
function lifeMeterB(e:Event = null):void{
    if(meter.life.width > 59){
        meter.life.scaleX -= .01;
    }
    else if(meter.life.width < 59 && meter.life.width > 29){
        meter.life.gotoAndStop(11);
        meter.life.scaleX -= .01;
    }
    else if(meter.life.width < 29 && meter.life.width > 15){
        meter.life.gotoAndStop(12)
        meter.life.scaleX -= .01;
    }
    else if(meter.life.width < 15 && meter.life.width > 1.5){
        meter.life.gotoAndPlay(2);
        meter.life.scaleX -= .01;
    }
}

////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////MAIN LOOP
function gameLoop(e:Event) {
    ////////////////////////COPTER MOVEMENT
    trace(bltArray);
    trace(airArray);
    trace(gndArray);
    coptr.y += speed;
    if(mouseIsDn){
        if(speed > -5){
            speed -= 1;
        }
    }else{
        if(speed < 10){
            speed += .25;
        }
    }

    ////////////////////////BULLET MOVEMENT
    if(bulit){
        bulit.x += 10;
    }


    if(coptr.y > stage.stageHeight - coptr.height*.5){
        coptr.y = stage.stageHeight - coptr.height*.5;
    }
    else if(coptr.y < 0 + coptr.height*.5){
        coptr.y = 0 + coptr.height*.5;
    }

    ////////////////////////COLLISIONS
    for(var i = 0; i<numChildren; i++){
        if(getChildAt(i) is airA){
            var b = getChildAt(i) as airA;
            if(b.hitTestObject(coptr)){
                lifeMeterA();
            }
            if(b.hitTestObject(bulit)){
                airArray.shift();
                removeChild(b);
                removeChild(bltArray[0]);
                PEW = false;
                var m:uint = uint(Math.random() * 3);
                ChanSFx = SFx[m].play();
            }
        }
        else if(getChildAt(i) is gndA){
            var c = getChildAt(i) as gndA;
            if(c.hitTestObject(coptr)){
                lifeMeterB();
            }
            if(c.hitTestObject(bulit)){
                gndArray.shift();
                removeChild(c);
                removeChild(bltArray[0]);
                PEW = false;
                var n:uint = uint(Math.random() * 3);
                ChanSFx = SFx[n].play();
            }
        }
    }
    if(coptr.y > stage.stageHeight - coptr.height){
        lifeMeterA();
    }

    ////////////////////////CLEANUP MISSED ENEMIES
    if(airArray[0] && airArray[0].x < 0){
        airArray.shift();
    }

    if(gndArray[0] && gndArray[0].x < 0){
        gndArray.shift();
    }

    ////////////////////////BULLET RATE OF SHOOTING
    if(bltArray[0] && bltArray[0].x > stage.stageWidth){
        PEW = false;
        bltArray.shift();
    }

    ////////////////////////END GAME
    if(meter.life.width < 1.5){
        removeEventListener(Event.ENTER_FRAME, gameLoop);
        gameTIMERa.stop();
        gameTIMERb.stop();
        meter.life.gotoAndStop(13);
        TweenLite.to(meter, .5, {x:861});
        gameO();
    }
}

function gameO(e:Event = null):void{
    addChild(govr);
    govr.x = 0;
    govr.y = 0;
    ChanAB.stop();
    ChanSFx = gOver.play();
    ChanSFx.addEventListener(Event.SOUND_COMPLETE, restart);
}
function restart(e:Event):void{
    removeChild(govr);
    removeChild(coptr);
    boot();
}
}
}
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

这意味着您正在某个对象的某处使用removeChild(),该对象的父级不是您要将其删除的容器。

例如,此代码会导致该错误:

var shape:Shape = new Shape();
stage.removeChild(shape);

您可能在代码中的某个地方多次调用removeChild()