【问题标题】:Array as3 collisions help coding数组 as3 冲突有助于编码
【发布时间】:2014-02-10 10:27:04
【问题描述】:

我在这里需要一些帮助我不能做碰撞,因为它的“未定义”我想我需要制作和数组来检查它,但我需要帮助做它以前从未做过:/ 在这里遇到一些困难尝试了一些数组但没有成功新的 javascript 需要帮助非常感谢任何帮助的人

/* Código que pára a timeline na 1 frame para que o menu continue apresentado*/
stop();
/*Movimenta a nave fazendo a seguir os movimentos do rato e esconde o cursor do sistema operacional*/

stage.addChild(arma_tiro);
arma_tiro.mouseEnabled = false;
arma_tiro.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);

function fl_CustomMouseCursor(event:Event)
{
    arma_tiro.x = stage.mouseX;
}
Mouse.hide();


/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.

Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/

stage.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);

function fl_MouseClickHandler_3(event:MouseEvent):void
{


    var bullet:bullet_ = new bullet_();
    addChild(bullet);
    bullet.x=arma_tiro.x;
    bullet.y=arma_tiro.y;
    bullet.addEventListener(Event.ENTER_FRAME, moverbala);

}

function moverbala(event:Event):void // função para mover a bala para cima */
{
    event.target.y=event.target.y-20;
}

//stage.addEventListener(Event.ENTER_FRAME, Primeira);

setInterval(Primeira, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Primeira(){  //funçao de spawn de nave 1

    var invader1:invader_1 = new invader_1();
    addChild(invader1);
    invader1.x=0;
    invader1.y=15;
    invader1.addEventListener(Event.ENTER_FRAME, mover1);
} 

function mover1(event:Event):void // função para mover a nave para lado direito */
{
    event.target.x+=10;
}

//Nave 2

setInterval(Segunda, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Segunda(){  //funçao de spawn de nave 1

    var invader2:invader_2 = new invader_2();
    addChild(invader2);
    invader2.x=0;
    invader2.y=45;
    invader2.addEventListener(Event.ENTER_FRAME, mover2);
} 

function mover2(event:Event):void // função para mover a nave para lado direito */
{
    event.target.x+=10;
}

//Nave 3

setInterval(Terceira, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Terceira(){  //funçao de spawn de nave 1

    var invader3:invader_3 = new invader_3();
    addChild(invader3);
    invader3.x=0;
    invader3.y=85;
    invader3.addEventListener(Event.ENTER_FRAME, mover3);
} 

function mover3(event:Event):void // função para mover a nave para lado direito */
{
    event.target.x+=10;
}

if (bullet.hitTestObject(invader1))
        {
            //Remove bullet and enemy
            removeChild(bullet);
            removeChild(invader1);

        }

【问题讨论】:

    标签: javascript actionscript-3 flash


    【解决方案1】:

    似乎 bullet_ 是一个类而不是一个实例,所以你不能在它上面调用 hitTestObject。也许尝试用子弹替换bullet_。

    有很多解决方案,但对我来说最简单的方法是保留 2 个阵列,一个用于子弹,一个用于敌人。

    所以添加数组:

    // create the array for the bullets
    bullets  :Array = [];
    // create the array for the enemies
    enemies :Array = [];
    

    添加一个 onEnterFrame 事件侦听器来对每一帧进行测试和游戏逻辑:

    addEventListener( Event.ENTER_FRAME, _gameLoop );
    

    改变你的功能来制造子弹和敌人:

    function fl_MouseClickHandler_3( event:MouseEvent ):void
    {
        // create the bullet
        var bullet:bullet_ = new bullet_();
        addChild(bullet);
        bullet.x=arma_tiro.x;
        bullet.y=arma_tiro.y;
    
        // add the bullet to the bullets array
        bullets.push( bullet );
    }
    
    function Primeira():void
    {
        var invader1:invader_1 = new invader_1();
        addChild(invader1);
        invader1.x=0;
        invader1.y=15;
    
        enemies.push( invader1 );
    }
    
    function Segunda():void
    {
        var invader2:invader_2 = new invader_2();
        addChild(invader2);
        invader2.x=0;
        invader2.y=45;
    
        enemies.push( invader2 );
    } 
    
    function Terceira():void
    {
        var invader3:invader_3 = new invader_3();
        addChild(invader3);
        invader3.x=0;
        invader3.y=85;
    
        enemies.push( invader3 );
    } 
    

    现在创建游戏循环函数:

    function _gameLoop(e:Event):void
    {
        var firstLoop:Boolean = true;
    
        // loop to move/remove the bullets
        for( var a:int = bullets.length-1; a>=0; a-- )
        {
            bullets[a].y -= 20;
            // if the bullet is not on screen anymore, remove it from array
            if( bullets[j].y < 0 )
            { 
                removeChild( bullet[a] );
                bullets.splice(a,1);
                continue;
            }
        }
    
        // loop enemies
        for( var i:int = enemies.length-1; i>=0; i-- )
        {
            // move the enemy
            enemies[i].x += 10;
            // loop the bullets to see if on collide the enemy
            for( var j:int = bullets.length-1; j>=0; j-- )
            {
                // test collision with the enemy
                if( enemies[i].hitTestObject( bullets[j] )
                {
                    // make your enemy dead
                    removeChild( enemies[i] );
                    // remove it from the array
                    enemies.splice(i,1);
                }
            }
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 按照你告诉我的最后一个错误:ArgumentError:错误 #1063:flashproject_fla::MainTimeline/_gameLoop() 上的参数计数不匹配。预期为 0,得到 1。
    • 一切都好,我发现很多:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多