dimanche 29 novembre 2015

Is anyone interested in looking at my code for a game? I'm getting strange errors

I'm trying to make my first real, simple shooter game. I've read through tutorials online and instead of directly copying code, I tried to take things the next level to actually understand the code and add my own twists. With this in mind, I've recently been learning arrays and not sure if I am using them correctly.

I'm getting strange, intermittent

Error #2025 DisplayObject must be a child of the caller

messages in the output window, not the compiler window.

So, I don't know what line of code is generating this problem. By commenting out blocks of code I have narrowed it down to the modules labeled "CLEANUP MISSED ENEMIES" and "BULLET RATE OF SHOOTING", but the 'why' is beyond my understanding.

I am sure there will be many great comments pointing out conventions I get wrong and errors I am making. I value every chance to learn so please give your input as you see fit! I bet there are way better methods to do the things I am doing!

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();
}
}
}
}

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire