AS3跟踪并使用旋转对象的坐标 [英] AS3 tracking and using coordinates of a rotated object

查看:461
本文介绍了AS3跟踪并使用旋转对象的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何一个轨道,并使用该旋转上初始化一个物体的坐标

How does one track and use the coordinates of an object that is rotated on initialization?

让我们说我有一个是把舞台上的主要的init()剑;和旋转(调整),这样它看起来确定连同人物的视角。在另一类不过,我想提出的剑旋转一些更上了一个关键的preSS计时器事件所以要建立一个摇摆的动画。

Let's say I have a sword that is put on the stage in Main init(); and rotated (adjusted) so that it would look ok together with the character perspective. In another class however, I am making the sword rotate some more on a keypress timer event so to create a 'swing' animation.

这一切是通过FlashDevelop中完成的。我只用CS6创建的符号。而作为这种摇摆正在发生的事情,我想补充另一个符号到这是一个碰撞点对象剑的尖端。它被添加到舞台上时,摆动开始,每一次挥杆后取出。我想按照这个对象剑的最顶端,但好像我也只能达到它沿用了原先的剑对象的坐标,就好像我最初没有修改该刀的旋转。我试图执行GlobalToLocal()和localToGlobal()方法,但我不认为我完全明白发生了什么这一点。

All this is done through flashdevelop. I only used CS6 to create the symbols. And as this 'swing' is happening, I want to add another symbol onto the tip of the sword which is a collision point object. It's being added to the stage when the swing starts and removed after every swing. I want this object to follow the very tip of the sword, yet it seems like I can only achieve that it follows the coordinates of the original sword object, as if I hadn't initially modified the rotation of the said sword. I tried to implement GlobalToLocal() and LocalToGlobal() methods, but I don't think I fully understand what is happening with that.

我希望我是再清楚不过的是我想要做的。谢谢。这是相关的code问题。在code是因为之前我试过上面提到的两个方法是,这件事情目前正在严格按照之前描述的。我想任何的这些方法还是我只是在做别的事情了?

I hope I'm being clear enough of what I'm trying to do. Thank you. This is the relevant code in question. The code is as was before I tried the two mentioned methods and the issue currently is exactly as described before that. Do I want any of those methods or am I just doing something else wrong?

主要初始化:

sword = new Sword();

sword.x = 53;
sword.y = 90;
addChild(sword);
sword.rotationZ = -150;
sword.rotationY = 25;
sword.rotationX = -15;

Coll_Point = new coll_point();

与摆动交易的类有这样的方法:

private function SwingTime(event:Event):void
{
    Main.Coll_Point.x = Main.sword.x + Main.sword.width;
    Main.Coll_Point.y = Main.sword.y + Main.sword.height;
    Main.MazeNr1.addChild(Main.Coll_Point);

    if (Main.sword.rotationZ > -330)
    Main.sword.rotationZ -= 20;

    if (Main.sword.rotationX < 15)
    Main.sword.rotationX += 10;

    if ((Main.sword.rotationZ == -330) && (Main.sword.rotationX == 15))
    {
        SwingTimer.stop();
        SwingBckTimer.start();
    }
}

编辑: 一个更全面的版本的code:

A more holistic version of the code:

public class Main extends MovieClip 
{
    public static var from_point:Point = null;
    public static var to_point:Point = new Point();
    public function Main():void
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    // Puts everything on the stage here.
    private function init(e:Event = null):void
    {
    removeEventListener(Event.ADDED_TO_STAGE, init);
        PlayerInst = new Dorf();
        PlayerInst.x = 45;
        PlayerInst.y = 51;
        addChild(PlayerInst);

        sword = new Sword();
        sword.x = 53;
        sword.y = 90;
        sword.rotationZ = -150;
        sword.rotationY = 25;
        sword.rotationX = -15;
        from_point = new Point (Main.sword.width, Main.sword.height);
        to_point = sword.localToGlobal(from_point);
        addChild(sword);
        swordBD = new BitmapData(32, 32, true, 0x0000000000);
        swordBD.draw(sword);

        Coll_Point = new coll_point();
        Coll_PointBD = new BitmapData(2, 2, true, 0x0000000000);
        Coll_PointBD.draw(Coll_Point);
    }
}

这是怎么了主要的样子,并从字面上每一个对象实例添加到舞台上这种方式。包括碰撞点,背景,人物,景象半径等,以及相关的符号类线的渐变填充去有点像这样的:

This is how the Main looks like and literally every single object instantiation is added onto the stage this way. Including collision points, background, characters, gradient fills of line of sight radius, etc. And the relevant symbol class goes somewhat like this:

public class Creature extends MovieClip 
{
    protected var Swing:Boolean;
    private var SwingTimer:Timer = new Timer (5, 0);
    private var SwingBckTimer:Timer = new Timer (150, 1);
// Constructor.
    public function Creature()
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    // Initializer.
    private function init(event:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        SwingTimer.addEventListener(TimerEvent.TIMER, SwingTime);
        SwingBckTimer.addEventListener(TimerEvent.TIMER, SwingBack);
    }
private function SwingAction():void
    {
        if (Swing == true)
        {
            SwingTimer.start();
        }
    }

    private function SwingTime(event:Event):void
    {
        Main.Coll_Point.x = Main.sword.localToGlobal(Main.from_point).x;
        Main.Coll_Point.y = Main.sword.localToGlobal(Main.from_point).y;
        Main.sword.addChild(Main.Coll_Point);
        trace(Main.Coll_Point.x);
        trace(Main.Coll_Point.y);

        if (Main.sword.rotationZ > -330)
        Main.sword.rotationZ -= 20;

        if (Main.sword.rotationX < 15)
        Main.sword.rotationX += 10;

        if ((Main.sword.rotationZ == -330) && (Main.sword.rotationX == 15))
        {
            SwingTimer.stop();
            SwingBckTimer.start();
        }
    }

    private function SwingBack(event:Event):void
    {
        Main.sword.rotationZ = -150;
        Main.sword.rotationX = -15;
        //Main.MazeNr1.removeChild(Main.Coll_Point);
    }

还有一个相当长的update();功能动画和移动,需要移动的每一个对象。

There is also a rather long update(); function that animates and moves every single object that needs moving.

推荐答案

我认为你的问题可能是

    Main.Coll_Point.x = Main.sword.x + Main.sword.width;
    Main.Coll_Point.y = Main.sword.y + Main.sword.height;

Col​​l_Point预计,今年全球坐标。

Coll_Point expects global coordinates.

零件 + Main.sword.width + Main.sword.height 只按预期工作,如果剑不转动,使得高度与y轴和宽度与x轴一致。

The parts + Main.sword.width and + Main.sword.height only work as expected if the sword is not rotated so that height is aligned with the y-axis and width with the x-axis.

您应该使用<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#localToGlobal%28%29"相对=nofollow> localToGlobal()上是本地的 Main.sword (位置 Main.sword.width,Main.sword.height )来获取重新presents旋转尖剑添加它作为一个孩子之前在全球的地位。

You should use localToGlobal() on the position that is local to Main.sword ( Main.sword.width, Main.sword.height) to get the global position that represents the swords rotated tip before you add it as a child.





有两种方法可以处理这个方法(你似乎已经有所结合两者)。您可以


  • 添加Coll_Point作为一个孩子的东西上面的剑在层次结构(第一阶段,MazeNr1,...)和手动更新的位置每一个定时器的回调。你将不得不重新计算的位置每次,所以取 localToGlobal()从初始化到您的定时器功能。它不会更新,如果它不被调用。
  • Add the Coll_Point as a child to something above the sword in hierarchy (Stage, MazeNr1, ...) and update the position manually every timer callback. You would have to recalculate the position everytime, so take the localToGlobal() from init to your timer function. It won't update if it doesn't get called.

有关,你应该有这样的code。在计时器回调:

For that you should have this kind of code in the timer callback:

var local:Point = new Point(Main.sword.width, Main.sword.height);
var global:Point = Main.sword.localToGlobal(local);
Main.Coll_Point.x = global.x;
Main.Coll_Point.y = global.y;





  • 添加点作为子剑。这可能是一个更好的方法,因为随后的位置将被自动更新。我不记得是什么之前,是你,然后给在本地形式的坐标,所以不要使用 localToGlobal()

运行这个一旦你创建其中 Col​​lision_Point

Run this once where you create the Collision_Point:

Coll_Point.x = <your x offset>;
Coll_Point.y = <your y offset>;
Main.sword.attachChild(Coll_Point);

而不是剑高度宽度你可能想尝试像 -height 宽度/ 2

Instead of sword height and width you might want to try something like -height and width/2.





下面是一个快速的(而不是prettiest)图片说明问题。局部空间旋转与对象

Here is a quick (and not the prettiest) picture to demonstrate the problem. Local space is rotated with the object:

这篇关于AS3跟踪并使用旋转对象的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆