碰撞检测测试不起作用并且还得到错误2025

时间:2013-02-06 07:59:38

标签: actionscript-3 flash

这是我的Player类

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

}

}

Bullet Class

    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()"

我错过了任何代码 所以财产可以工作..?

请帮我解决问题.. 在此先感谢

2 个答案:

答案 0 :(得分:1)

关于2025年的错误:

这是因为Player,Dead1和dust_mc实例都是bg_mc的子项。

所以要删除它们:

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

hitTestPoint无效,因为它的x和y参数指定了舞台坐标空间中的一个点,而不是包含显示对象的显示对象容器&#34; (as3 docs),在本例中为bg_mc。

对此的一个补救措施是使子弹本身成为checkCollision()的参数,这样我们就可以获得它的舞台坐标(此处命名为&#34; test&#34;):

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; // }
}

我希望有效。

答案 1 :(得分:0)

以下是MovieClips内部MovieClip之间碰撞检测的说明

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