【问题标题】:AS3 create a trail of movieclips following each otherAS3 创建一系列彼此跟随的影片剪辑
【发布时间】:2013-11-21 04:03:30
【问题描述】:

所以,我试图让一些影片剪辑跟随它的前身,并让最后一个跟随鼠标。问题是我是从代码而不是使用界面创建它们,而且由于我不是专家,我无法让它们工作。

我在库中只有一个 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;
}

使用该代码,只有一个字母跟随鼠标(“E”),其余的则留在我使用 addChild 和 posXLetter 变量添加它们的位置。

另外,我试图让它表现得更像一条小路,所以如果我向上移动,字母会落后于我;如果我向左移动,字母将滞后于我的右侧,但我认为按照我目前的方法,它们要么 A)一起移动到同一个位置,要么 B)总是挂在光标的左侧。

感谢您提供任何可能的帮助。

【问题讨论】:

    标签: actionscript-3 flash actionscript trail


    【解决方案1】:

    这是一种叫做Inverse Kinematics的动作,它是一种在游戏中制作布娃娃的非常流行的方式。它使用一种称为Composite Pattern 的设计模式,其中一个对象添加另一个对象作为其子对象,然后当调用它的 update() 函数时,它会调用其所有(通常是一个)子对象的 update() 函数。最常见的例子是蛇。蛇的头部跟随你的鼠标,而蛇的其余部分则随着蛇移动,看起来非常逼真。这个确切的例子被解释并构建了here,尽管它根本不包括联合限制。

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

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

    我希望这会有所帮助。

    【讨论】:

    • 太好了。我一直在寻找扩展知识和提高编程技能的方法。感谢您的回复,会试一试:)
    【解决方案2】:

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

    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 距离。
    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多