我们如何才能将一个movieclip 朝另一个movieclip 移动的方向移动?我有一个影片剪辑,即猎物,它随着加速度计的更新而移动。我希望跟随者影片剪辑像猎物一样赶上那个影片剪辑。猎物随着加速度计的变化而移动。我希望跟随者影片剪辑跟随猎物。这可以做到吗?
问问题
2326 次
2 回答
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;
}
}
于 2012-06-24T01:15:22.967 回答
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);!
我在这里做的是添加mc1
和mc2
进入myContainerVar
. 诸如移动myContainerVar
变量之类的更改也将自动应用于mc1
和mc2
。
一个小旁注:如果你再深入一点,你应该知道,在统计上,改变 myContainerVar 的位置时,mc1 和 mc2 的位置不会改变。谨防 ;)
于 2012-06-22T09:53:42.447 回答