所以,我试着找一些电影来跟随它的前兆,让最后一个跟着鼠标。问题是,我是从代码中创建它们,而不是使用接口,而且,由于我不是专家,我无法让它们工作。
库中只有一个MovieClip(链接:“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;
}使用该代码,鼠标后面只有一个字母( "E"),其余字母则停留在我使用addChild和posXLetter变量添加它们的位置。
另外,我试图让它表现得更像一条轨迹,所以如果我往上移动,字母就会落在我下面;如果我向左移动,字母就会滞后于我的右边,但我认为,按照我目前的方法,它们要么一起移动到同一个位置,要么就是B)总是挂在光标的左边。
谢谢任何可能的帮助。
发布于 2013-11-09 02:57:17
这是一种叫做逆运动学的运动,它是在游戏中制作破布娃娃的一种很流行的方法。它使用一个名为复合图案的设计模式,其中一个对象将另一个对象添加为its的子对象,然后当它的update()函数被调用时,它将调用其所有(通常为一个)子对象的update()函数。最常见的例子是蛇。蛇的头跟着你的老鼠,其余的蛇的身体碎片随着蛇的移动,它看起来非常现实。这个精确的例子是解释和构建这里的,尽管它根本不包括联合限制。
这个例子是在一本书的中间,因此可能很难开始阅读,但是如果您对设计模式有点熟悉和/或有一些编程方面的中级经验,那么我相信您可以理解它。我建议您在阅读并理解了该示例之后,对您现在拥有的内容进行抓取,因为它不是非常优雅的编码。您可能觉得这个示例使用了太多的类,但是相信我,它是值得的,因为它允许您非常容易地编辑您的代码,如果您决定在未来修改它,没有任何缺点。
而且,我知道这条蛇不是您想要的,但是如果您了解概念,那么您可以将它应用到您自己的特定需求中。
我希望这能帮到你。
发布于 2013-11-09 02:58:30
我认为这是一个范围界定的问题。您可能需要修改处理程序。
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;
}https://stackoverflow.com/questions/19871020
复制相似问题