Actionscript 3 AS3创建一条电影嘴唇相互跟踪的轨迹

Actionscript 3 AS3创建一条电影嘴唇相互跟踪的轨迹,actionscript-3,flash,actionscript,trail,Actionscript 3,Flash,Actionscript,Trail,所以,我试着让一些电影嘴唇跟随它的前身,让最后一个跟着鼠标。问题是我是从代码中创建它们,而不是使用接口,因为我不是专家,我无法让它们工作 库中只有一个MovieClip(linkage:“LETRA”),其中包含一个textField(实例名:“myTextField”) 以下是我所拥有的: import flashx.textLayout.operations.MoveChildrenOperation; import flash.display.MovieClip; import flash

所以,我试着让一些电影嘴唇跟随它的前身,让最后一个跟着鼠标。问题是我是从代码中创建它们,而不是使用接口,因为我不是专家,我无法让它们工作

库中只有一个MovieClip(linkage:“LETRA”),其中包含一个textField(实例名:“myTextField”)

以下是我所拥有的:

import flashx.textLayout.operations.MoveChildrenOperation;
import flash.display.MovieClip;
import flash.events.Event;

//this are the letters that will be following the mouse
var phrase:Array = ["H","a","c","e","r"," ","u","n"," ","p","u","e","n","t","e"];

//variable to spread them instead of creating them one of top of each other
var posXLetter:Number = 0;

//looping through my array
for (var i:Number = 0; i < phrase.length; i++)
{
    //create an instance of the LETRA movieclip which contains a text field inside
    var newLetter:MovieClip = new LETRA();

    //assing a letter to that text field matching the position of the phrase array
    newLetter.myTextField.text = phrase[i];

    //assign X position to the letter I'm going to add
    newLetter.x = posXLetter;

    //add properties for storing the letter position
    var distx:Number = 0;
    var disty:Number = 0;

    //add the listener and the function which will move each letter
    newLetter.addEventListener(Event.ENTER_FRAME, moveLetter);

    function moveLetter(e:Event){

        distx = newLetter.x - mouseX;
        disty = newLetter.y - mouseY;

        newLetter.x -= distx / 10;
        newLetter.y -= disty / 10;
    }

    //add each letter to the stage
    stage.addChild(newLetter);

    //increment the next letter's x position
    posXLetter +=  9;
}
导入flashx.textLayout.operations.MoveChildrenOperation;
导入flash.display.MovieClip;
导入flash.events.Event;
//这是将跟随鼠标的字母
变量短语:数组=[“H”,“a”,“c”,“e”,“r”,“u”,“n”,“p”,“u”,“e”,“n”,“t”,“e”];
//变量来展开它们,而不是将它们一个接一个地创建
var posXLetter:数字=0;
//在我的数组中循环
for(变量i:Number=0;i
有了这些代码,只有一个字母在鼠标后面(“E”),其余的字母留在我使用addChild和posXLetter变量添加它们的地方

而且,我试图让它表现得更像一条轨迹,所以如果我向上移动,字母会落在我下面;如果我向左移动,字母将滞后于我的右侧,但我认为,按照我目前的方法,它们要么A)一起移动到同一点,要么B)始终挂在光标的左侧


感谢您的帮助。

这是一种运动,称为,它是一种在游戏中制作布娃娃的非常流行的方式。它使用一个名为的设计模式,其中一个对象添加另一个对象作为其子对象,然后当调用其update()函数时,它调用其所有(通常是一个)子对象的update()函数。最常见的例子是蛇。蛇头跟随你的鼠标,蛇身体的其他部分随着蛇移动,看起来非常逼真。虽然该示例根本不包括联合限制,但仍对其进行了解释和构建

这个例子是在一本书的中间,所以可能很难开始阅读,但是如果你对设计模式有点熟悉和/或有一些中间编程的经验,那么我相信你能理解它。我建议您在阅读并理解了这个示例之后,重新开始编写您现在拥有的代码,因为它不是非常优雅的编码。您可能会觉得这个示例使用了太多的类,但是相信我,它是值得的,因为它允许您非常轻松地编辑您的代码,如果您决定在将来更改它,并且没有任何缺点

另外,我知道这条蛇不是你想要的,但是如果你理解的概念,那么你可以将它应用于你自己的特定需求


我希望这会有所帮助。

我认为这是一个范围问题。您可能需要修改处理程序

function moveLetter(e:Event){
    trace(e.target); //check if this is the right movie clip
    distx = e.target.x - mouseX;
    disty = e.target.y - mouseY;

    e.target.x -= distx / 10;
    e.target.y -= disty / 10;
}

太好了。我一直在寻找方法来扩展我的知识和加强我的编程技能。谢谢你的回复,我将试一试:)在这里发布后,我意识到你所说的,并使用currentTarget使它工作。问题是它们在一组中跟随光标,而不是具有X距离。