0

这是我的播放器类

package 
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;

public class Player extends MovieClip
{
    //private var stageRef:Stage;
    //var xx:Number;
    //var yy:Number;
    private var BG:MovieClip;
    var speed:Number = 7;

    public function Player(xx:Number,yy:Number,BG:MovieClip) : void
    {
        x = xx;
        y = yy;

        this.BG = BG;

        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    }
    private function loop(e:Event) : void
    {

        var yDistance:Number = BG.mouseY - y;
        var xDistance:Number = BG.mouseX - x;
        var radian:Number = Math.atan2(yDistance, xDistance);
        rotation = radian * 180 / Math.PI;
    }

}

}

子弹类

    package 
   {
    import flash.display.*;
    import flash.events.*;
    import flash.utils.getTimer;

    public class Bullet extends MovieClip
    {
        private var dx,dy:Number;
        private var lastTime:int;

        public function Bullet(mX,mY:Number, rotate:Number, speed:Number)
        {
            // set start position
            var GunLength:Number = 70;
            this.x = 600 + GunLength*Math.cos(2*Math.PI*rotate/360);
            this.y = 450 + GunLength*Math.sin(2*Math.PI*rotate/360);
            this.rotation = rotate;

            // get speed
            dx = speed*Math.cos(2*Math.PI*rotate/360);
            dy = speed*Math.sin(2*Math.PI*rotate/360);

            // set up animation
            lastTime = getTimer();
            addEventListener(Event.ENTER_FRAME,moveBullet);

        }
        public function moveBullet(event:Event)
        {
            // get time passed
            var timePassed:int = getTimer() - lastTime;
            lastTime +=  timePassed;

            // move bullet
            this.x +=  dx * timePassed / 1000;
            this.y +=  dy * timePassed / 1000;
        }

    }

    }

敌人等级

package 
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;

public class Enemy1 extends MovieClip
{
    private var BG:MovieClip;
    private var speed:Number = 0.5;

    public function Enemy1(BG:MovieClip) : void
    {
        var RandomX:Array = new Array(100,200,300,400,800,900,1000,1100);
        var RandomY:Array = new Array(100,200,300,600,700,800);
        var r:int = (Math.random() * 8);
        var s:int = (Math.random() * 6);

        x = RandomX[r];
        y = RandomY[s];

        this.BG = BG;

        addEventListener(Event.ENTER_FRAME, moveEnemy); //false, 0, true);.
    }
    private function moveEnemy(e:Event):void
    {
        if (this.x > 660)
        {
            this.x -=  speed;
            this.rotation = 180;
        }

        if (this.x < 540)
        {
            this.x +=  speed;
            this.rotation = 0;
        }

        if (this.y > 510)
        {
            this.y -=  speed;
            this.rotation = 270;
        }

        if (this.y < 390)
        {
            this.y +=  speed;
            this.rotation = 90;
        }

        if (this.x > 660 && this.y > 510)
        {
            this.x -=  speed;
            this.y -=  speed;
            this.rotation = 225;
        }

        if (this.x < 540 && this.y < 390)
        {
            this.x +=  speed;
            this.y +=  speed;
            this.rotation = 45;
        }

        if (this.x < 540 && this.y > 510)
        {
            this.x +=  speed;
            this.y -=  speed;
            this.rotation = 315;
        }

        if (this.x > 660 && this.y < 390)
        {
            this.x -=  speed;
            this.y +=  speed;
            this.rotation = 135;
        }
    }

}

}

主班

package 
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.ui.Mouse;


public class Main extends MovieClip
{
    // Player Var
    private var player:MovieClip;
    private var bunker_mc:MovieClip;
    // Bullet var & Array
    private var b:MovieClip;
    private var bullets:Array;
    private var gunFire:Boolean = false;
    // Enemies, deadAnimation var & Array
    private var enemy1:MovieClip;
    private var tempEnemy1:MovieClip;
    private var dead1:MovieClip;
    private var enemies1:Array;
    private var deadArray1:Array;
    private var dustArray:Array;
    // Effect  var
    private var dust:MovieClip;
    // Score & other Helper var
    private var Level:int = 1;
    private var Score:int = 0;

    public function startMain()
    {
        // Timers Initialisation
        var GameLevelTimer:Timer = new Timer(1000,15);     
        var EnemyTimer:Timer = new Timer(1000,10);      
        //
        var bg_mcX = bg_mc.width/2;
        var bg_mcY = bg_mc.height/2;
        // Variable initialisation          
        bunker_mc = new bunker();
        bunker_mc.x = stage.stageWidth/2;
        bunker_mc.y = stage.stageHeight/2;
        addChild(bunker_mc);
        Lvl_txt.text = String(Level);
        Scr_txt.text = String(Score);
        if(Level == 1)
        {
            player = new Player(bg_mcX,bg_mcY,bg_mc);
            bg_mc.addChild(player);
        }

        // Objects arrays
        bullets = new Array();
        enemies1 = new Array();
        deadArray1 = new Array();
        dustArray = new Array();

        // Event Listeners
        stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
        stage.addEventListener(Event.ENTER_FRAME,updateBullet);
        stage.addEventListener(KeyboardEvent.KEY_DOWN,moveBG);
        // Timer Events Start
        EnemyTimer.addEventListener(TimerEvent.TIMER,EnemyTimerStart);
        EnemyTimer.start();
            GameLevelTimer.addEventListener(TimerEvent.TIMER_COMPLETE,GameLevelTimerEnd);
        GameLevelTimer.start();
    }

    //Mouse Down Event Handler
    private function mouseDownFun(event:MouseEvent):void
    {
        fire();
    }
    private function moveBG(e:KeyboardEvent):void
    {

    }
    private function EnemyTimerStart(e:TimerEvent):void
    {
        if(Level == 1)
        {
            enemy1 = new Enemy1(bg_mc);
            enemies1.push(enemy1);
            bg_mc.addChild(enemy1);
        }
    }
    private function updateBullet(e:Event):void
    {
        var i:int;
        var tempBullet:MovieClip;

        // loop for _bullets array
        for (i = 0; i < bullets.length; i++)
        {
            // save a reference to current bullet
            tempBullet = bullets[i];

            // check for collision with Enemies.
            if (checkCollision(tempBullet.x,tempBullet.y))
            {
                destroyBullet(tempBullet);
            }
        }
    }

    private function fire()
    {
        player.play();
        if(Level == 1)
        {
            b = new Bullet(mouseX,mouseY,player.rotation,500);
            bullets.push(b);
            bg_mc.addChild(b);
        }
    }

    private function checkCollision(testX:Number,testY:Number):Boolean
    {
        for (var i = 0; i < enemies1.length; i++)
        {
            tempEnemy1 = enemies1[i];
            if(tempEnemy1.hitTestPoint(testX,testY,true))
            {
                enemies1.splice(i, 1);
                //tempEnemy1.parent.removeChild(tempEnemy1);
                bg_mc.removeChild(tempEnemy1);
                dead1 = new     Dead1(tempEnemy1.x,tempEnemy1.y,tempEnemy1.rotation);
                deadArray1.push(dead1);
                bg_mc.addChild(dead1);
                Score += 10;
                Scr_txt.text = String(Score);
                return true;
            }
        }
            //removeChild(b,enemy1);
        return false;//}
    }
    private function destroyBullet(bullet:MovieClip):void
    {
        var tempBullet:MovieClip;
        for (var i = 0; i<= bullets.length;i++)
        {
            bg_mc.tempBullet = bullets[i];
            if (tempBullet == bullet)
            {
                dust = new dust_mc();
                dust.x = bullet.x;
                dust.y = bullet.y;
                dust.rotation = (Math.random() * 360);
                // remove from array
                bullets.splice(i, 1);
                bullet.parent.removeChild(bullet);
                //bg_mc.removeChild(bullet);
                dustArray.push(dust);
                bg_mc.addChild(dust);
                return;
            }
        }
    }
    private function GameLevelTimerEnd(e:TimerEvent):void
    {
        var tempDead:MovieClip;
        var tempDust:MovieClip;
        removeChild(player);
        removeChild(bunker_mc);
        stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
        stage.removeEventListener(Event.ENTER_FRAME,updateBullet);
        for (var i = 0; i < deadArray1.length; i++)
        {
            tempDead = deadArray1[i];
            removeChild(tempDead);
        }
        for (var j = 0; j < dustArray.length; j++)
        {
            tempDust = dustArray[j];
            removeChild(tempDust);
        }
        Level += 1;
        gotoAndStop("end");
    }
    //Mouse Up Event Handler
    /*private function mouseUpFun(event:MouseEvent):void
    {

    }*/

}

}

我面临的问题是我的命中测试属性不起作用,还有错误 2025

 "ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/GameLevelTimerEnd()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/tick()"

我是否缺少任何代码,以便该属性可以工作..?

请帮我解决这个问题.. 提前致谢

4

2 回答 2

1

关于 2025 错误:

这是因为 Player、Dead1 和dust_mc 实例都是 bg_mc 的子级。

所以要删除它们:

bg_mc.removeChild( player );
bg_mc.removeChild( tempDead );
bg_mc.removeChild( tempDust );

hitTestPoint 不起作用,因为它是“x 和 y 参数指定舞台坐标空间中的一个点,而不是包含显示对象的显示对象容器”( as3 文档),在本例中为 bg_mc。

对此的一种补救方法是将子弹本身作为 checkCollision() 的参数,这样我们就可以获得它的舞台坐标(这里命名为“test”):

private function checkCollision( mc:MovieClip ):Boolean
{
    // be sure to import flash.geom.Point;
    var test:Point = mc.localToGlobal( new Point() ); 

    for ( var i = 0; i < enemies1.length; i++ )
    {
        tempEnemy1 = enemies1[ i ];
        if ( tempEnemy1.hitTestPoint( test.x, test.y, true ) )
        {
            enemies1.splice( i, 1 );
            // tempEnemy1.parent.removeChild( tempEnemy1 );
            bg_mc.removeChild( tempEnemy1 );
            dead1 = new Dead1( tempEnemy1.x, tempEnemy1.y, tempEnemy1.rotation );
            deadArray1.push( dead1 );
            bg_mc.addChild( dead1 );
            Score += 10;
            Scr_txt.text = String( Score );
            return true;
        }
    }
        // removeChild( b,enemy1 );
    return false; // }
}

在我的游戏中,虽然我总是使用 hitTestObject(),但不那么令人困惑:

private function checkCollision( mc:MovieClip ):Boolean
{
    for ( var i = 0; i < enemies1.length; i++ )
    {
        tempEnemy1 = enemies1[ i ];
        if ( tempEnemy1.hitTestObject( mc ) )
        {
            enemies1.splice( i, 1 );
            // tempEnemy1.parent.removeChild( tempEnemy1 );
            bg_mc.removeChild( tempEnemy1 );
            dead1 = new Dead1( tempEnemy1.x, tempEnemy1.y, tempEnemy1.rotation );
            deadArray1.push( dead1 );
            bg_mc.addChild( dead1 );
            Score += 10;
            Scr_txt.text = String( Score );
            return true;
        }
    }
        // removeChild( b,enemy1 );
    return false; // }
}

我希望这行得通。

于 2013-02-06T08:22:16.550 回答
0

这是MovieClips内部MovieClips之间碰撞检测的图示

http://www.swfcabin.com/open/1360154866

这是代码:

package com.hittestpoint
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;

public class Application extends Sprite {

    private var bg:MovieClip;
    private var bullets:Array = [];
    private var enemies:Array = [];
    private var tf:TextField = new TextField();

    public function Application() {
        bg = makeBg();
        for ( var i:int = 0; i < 3; i++ )
            enemies.push( makeActor( 1 ) );
        addEventListener( MouseEvent.CLICK, fire );
        tf.text = "Click stage to throw in bullets."
        tf.width = 300;
        addChild( tf );
    }

    private function
    checkCollision():void {
        for ( var i:int = 0; i < bullets.length; i++ ) {
            var enemyHitIndex:int = getEnemyHitIndex( bullets[ i ].mc );
            if ( enemyHitIndex > - 1 ) {
                bg.removeChild( bullets.splice( i--, 1 )[ 0 ].mc );
                bg.removeChild( enemies.splice( enemyHitIndex, 1 )[ 0 ].mc );
            }
        }
    }

    private function
    fire( e:MouseEvent ):void {
        for ( var i:int = 0; i < 1; i++ )
            bullets.push( makeActor( 0 ) );
    }

    private function
    getActorObj( mc:MovieClip, type:int ):Object {
        var a:Array = type == 0 ? bullets : enemies;
            for ( var i:int = 0; i < a.length; i++ )
                if ( mc == a[ i ].mc )
                    return a[ i ];

        return null;
    }

    // Using hitTestPoint
    private function
    getEnemyHitIndex( mc:MovieClip ):int {
        var test:Point = mc.localToGlobal( new Point() );
        // or // var test:Point = localToGlobal( new Point( mc.x, mc.y ) );

        for ( var i:int = 0; i < enemies.length; i++ )
            if ( enemies[ i ].mc.hitTestPoint( test.x, test.y, true ) )
               return i;

        return -1;
    }

    // Using hitTestObject
    private function
    getEnemyHitIndex2( mc:MovieClip ):int {
        for ( var i:int = 0; i < enemies.length; i++ )
            if ( enemies[ i ].mc.hitTestObject( mc ) )
                return i;

        return - 1;
    }

    private function
    killActor( e:Event ):void {
        var mc:MovieClip = e.currentTarget as MovieClip;
        mc.removeEventListener( Event.ENTER_FRAME, moveActor );
        mc.removeEventListener( Event.REMOVED_FROM_STAGE, killActor );
    }

    private function
    makeActor( type:int ):Object {
        var actor:Object = {};
        var mc:MovieClip = new MovieClip();

        with( mc.graphics ) {
            beginFill( type == 0 ? 0x0000ff : 0xff0000 );
            lineStyle( 0, 0, 0 );
            drawCircle( 0, 0, type == 0 ? 4 : 16 );
            endFill();
        }
        mc.x = bg.width / 2;
        mc.y = bg.height / 2;
        bg.addChild( mc );

        actor.mc = mc;
        actor.vx = Math.random() * 8;
        actor.vy = Math.random() * 8;

        mc.addEventListener( Event.ENTER_FRAME, moveActor );
        mc.addEventListener( Event.REMOVED_FROM_STAGE, killActor );

        return actor;
    }

    private function
    makeBg():MovieClip {
        var mc:MovieClip = new MovieClip();

        with( mc.graphics ) {
            beginFill( 0x00ffff );
            lineStyle( 0, 0, 0 );
            drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
            endFill();
        }
        addChild( mc );

        return mc;
    }

    private function
    moveActor( e:Event ):void {
        var mc:MovieClip = e.currentTarget as MovieClip;
        var actor:Object = getActorObj( mc, mc.width == 8 ? 0 : 1 );
        var wHalf:Number = mc.width / 2;
        var hHalf:Number = mc.height / 2;

        mc.x += actor.vx;
        mc.y += actor.vy;

        if ( mc.x > bg.width * .999 - wHalf ) {
            mc.x = bg.width * .999 - wHalf;
            actor.vx = - actor.vx;
        }
        else if ( mc.x < wHalf ) {
            mc.x = wHalf;
            actor.vx = - actor.vx;
        }

        if ( mc.y > bg.height * .999 - hHalf ) {
            mc.y = bg.height * .999 - hHalf;
            actor.vy = - actor.vy;
        }
        else if ( mc.y < hHalf ) {
            mc.y = hHalf;
            actor.vy = - actor.vy;
        }

        checkCollision();
    }
}
}
于 2013-02-06T14:56:49.117 回答