AS3创建影片剪辑之后对方一个线索线索、影片剪辑、对方

2023-09-09 21:31:25 作者:朝朝皆念空对影丶

所以,我试图得到一些影片剪辑遵循它的precursor并有最后一个跟随鼠标。问题是我是从code创建它们,而不是使用接口和,因为我不是专家,我不能让他们的工作。

So, I'm trying to get a few movieclips to follow it's precursor and have the last one follow the mouse. The problem is I'm creating them from code instead of using the interface and, since I'm not an expert, I can't get them to work.

所有我在图书馆是一个影​​片剪辑(链接:LETRA),其中包含内部包含textField(实例名称:myTextField的)。

All I have in the library is a MovieClip(linkage:"LETRA") which contains a textField inside(instance name:"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;
}

使用了code,只有一个字母是继鼠标(下称E),其余的都留在我加入他们的使用addChild和posXLetter变量。

With that code, only one letter is following the mouse (the "E") and the rest are staying where I added them using addChild and the posXLetter variable.

另外,我试图让它更像一个线索,所以如果我动起来,这些字母将滞后在我身下;如果我向左移动,这些字母将滞后到我的权利,但我认为我目前的做法,他们将a)将所有汇集到同一地点或B)总是挂在鼠标的左侧。

Also, I'm trying to get it to behave more like a trail, so if I move up, the letters will lag beneath me; if I move to the left, the letters will lag to my right but I think that with my current approach they will either A) move all together to the same spot or B) always hang to the left of the cursor.

感谢任何可能的帮助。

推荐答案

这是一种运动叫的反向运动学,这是一个很受欢迎的方法,使布娃娃游戏。它使用一个名为组合模式的设计模式,其中一个对象增加了一个对象作为它和一个孩子那么当它的update()函数,如果调用,它调用所有(通常是一个)孩子的更新()函数。这方面最常见的例子是一条蛇。蛇的头跟随你的鼠标和蛇的体块的其余部分移动的蛇,看起来非常逼真。这个确切的例子进行了说明,并建立此处尽管它不包括合资限制的。

This is a kind of motion called Inverse Kinematics and it is a quite popular way to make rag dolls in games. It uses a design pattern called the Composite Pattern where one object adds another object as a child of its and then when it's update() function if called, it calls all of its (usually one) child's update() functions. The most common example of this is of a snake. The snake's head follows your mouse, and the rest of the snake's body pieces move with the snake, and it looks immensely realistic. This exact example is explained and build here although it does not include joint restrictions at all.

这例子是在一本书的中间,这样就很难再开始阅读,但如果你稍微熟悉设计模式和/或与编程的一些中间经验,那么我敢肯定,你可以把它理解。我建议你​​,阅读和理解的例子后,挠你现在有什么,因为这是不是很优雅的编码。你可能会觉得这个例子中使用了太多的课,但相信我,它的价值,因为它可以让你的非常轻松地编辑您的code,如果你决定要在将来改变它,没有缺点。

This example is in the middle of a book, and so may be hard to start reading, but if your somewhat familiar with design patterns and/or have some intermediate experience with programming, then i'm sure you can understand it. I advise that you, after reading and understanding the example, scratch what you have now because it is not very elegant coding. You may feel that this example uses too many classes, but trust me, its worth it as it allows you to very easily edit your code, if you decide to change it in the future, with no drawbacks.

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

Also, i know that this snake is not what you want, but if you understand the concept then you can apply it to your own specific needs.

我希望这有助于。

 
精彩推荐
图片推荐