使用as3中的另一个动画片段移动动画片段

时间:2012-06-22 08:59:43

标签: actionscript-3 flash flex air flex4.5

我们如何在另一个动画片段移动的方向上移动一个动画片段?我有一个动画片,即猎物,随着加速计的更新而移动。而且我希望追随者的电影剪辑像猎物一样赶上那部电影剪辑。随着加速度计的变化,猎物正在移动。我希望追随者电影剪辑跟随猎物。可以这样做吗?

2 个答案:

答案 0 :(得分:0)

你可以让movieclip#2跟随第一个像这样:

//this would usually be in an ENTER_FRAME event since there's no tweening going on here
//mc1 is your first movieclip, mc2 the second
mc1.x += 20; //this is the first one that needs to be followed
mc2.x = (mc1.x - 40); //this is the follower; it's always 40px horizontally behind mc1

另外,你可以使用'容器'精灵。这是一个空的精灵,你可以添加其他Sprite或MovieClip。它的工作原理如下:

myContainerVar.addChild(mc1); //myContainerVar is a(n empty) Sprite.
myContainerVar.addChild(mc2);
myContainerVar.x += 20; //this moves both movieclips.
//make sure you do this INSTEAD of the normal addChild(m1);!

我在这里做的是将mc1mc2添加到myContainerVar。移动myContainerVar变量等更改也会自动应用于mc1mc2

一个小小的注意事项:如果你进一步提高一点,你应该知道,在统计上,当改变myContainerVar的位置时,mc1和mc2的位置不会改变。小心;)

答案 1 :(得分:0)

这是让一个剪辑追逐另一个剪辑的一种非常简单的方法。将其粘贴到新的fla文件中并编译以查看结果。

import flash.display.MovieClip;

var mcOne : MovieClip = new MovieClip();
mcOne.graphics.beginFill( 0x000000 );
mcOne.graphics.drawCircle( 0, 0, 10 );
mcOne.speedX = 1.9;
mcOne.speedY = 1.9;
addChild( mcOne );

var mcTwo : MovieClip = new MovieClip();
mcTwo.graphics.beginFill( 0xFF0000 );
mcTwo.graphics.drawCircle( 0, 0, 10 );
mcTwo.speedX = 2;
mcTwo.speedY = 2;
addChild( mcTwo );

moveClipsToRandomPosition( mcOne );
moveClipsToRandomPosition( mcTwo );

stage.addEventListener( Event.ENTER_FRAME, onStageEnterFrame );

function onStageEnterFrame( evt : Event ) : void
{
    moveMC( mcOne );
    chase( mcTwo, mcOne );
    checkBounds( mcOne );
}

function chase( chasingMC : MovieClip, chasedMC : MovieClip ) : void
{
    if( chasingMC.x < chasedMC.x )
    {
        if( chasingMC.x + chasingMC.speedX > chasedMC.x )
        {
            chasingMC.x = chasedMC.x;
        }
        else
        {
            chasingMC.x += chasingMC.speedX;
        }
    }
    else if( chasingMC.x > chasedMC.x )
    {
        if( chasingMC.x - chasingMC.speedX < chasedMC.x )
        {
            chasingMC.x = chasedMC.x;
        }
        else
        {
            chasingMC.x -= chasingMC.speedX;
        }
    }

    if( chasingMC.y < chasedMC.y )
    {
        if( chasingMC.y + chasingMC.speedY > chasedMC.y )
        {
            chasingMC.y = chasedMC.y;
        }
        else
        {
            chasingMC.y += chasingMC.speedY;
        }
    }
    else if( chasingMC.y > chasedMC.y )
    {
        if( chasingMC.y - chasingMC.speedY < chasedMC.y )
        {
            chasingMC.y = chasedMC.y;
        }
        else
        {
            chasingMC.y -= chasingMC.speedY;
        }
    }

    if( chasingMC.y == chasedMC.y && chasingMC.x == chasedMC.x )
    {
        trace( "ChasingMC caught chasedMC" );
        moveClipsToRandomPosition( mcOne );
        moveClipsToRandomPosition( mcTwo );
    }
}

function moveClipsToRandomPosition( mc : MovieClip ) : void
{
    mc.x = ( Math.random() * ( stage.stageWidth - 20 ) ) + 10;
    mc.y = ( Math.random() * ( stage.stageHeight - 20 ) ) + 10;
}

function moveMC( mc : MovieClip ) : void
{
    mc.x += mc.speedX;
    mc.y += mc.speedY;
}

function checkBounds( mc : MovieClip ) : void
{
    if( ( mc.x + mc.width / 2 ) >= stage.stageWidth )
    {
        mc.speedX *= -1;
    }
    else if( ( mc.x - mc.width / 2 ) <= 0 )
    {
        mc.speedX *= -1;
    }

    if( ( mc.y + mc.height / 2 ) >= stage.stageHeight )
    {
        mc.speedY *= -1;
    }
    else if( ( mc.y - mc.height / 2 ) <= 0 )
    {
        mc.speedY *= -1;
    }
}