【问题标题】:Object isn't added in the right place, despite correct coordinates尽管坐标正确,但对象未添加到正确的位置
【发布时间】:2012-05-05 21:35:30
【问题描述】:

我在 Actionscript 3 (Flash CS5) 中创建了一个程序,当您单击时,它会在舞台上生成一个 Egg(现在是一个圆圈),然后使用(编码)补间将 Egg 发送到您单击的位置。此外,它会对其进行缩放以尝试产生 3D 错觉。

我为 Egg 的其中一个补间添加了 TweenEvent.MOTION_FINISH,以检查动作何时结束。当它发生时,我创建一个新对象(称为 Splat),其坐标与原始鼠标单击的坐标相同。

问题 1: Splat 对象未添加到正确的位置,如下图所示。我检查了坐标,它们在 Egg 类和 Splash 类中都匹配。我还确保两个对象的注册点都在它们的中间。最后,我检查了是否是导致问题的比例,但即使我删除了补间比例,我也会遇到同样的问题。

(编辑:鸡蛋放置在正确的位置。鼠标点击的原始坐标应该是它们应该是的。似乎只有星星物体被移位了。)

http://i45.tinypic.com/24bj1h3.png

问题 2: Splat 对象按与 Egg 对象相同的比例缩小。为什么是这样?我没有(打算)让 Splash 依赖于 Egg。

这里是相关代码:

package  
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.MouseEvent;

public class eggMain extends MovieClip
{
    import flash.events.Event;

    public function eggMain() 
    {
        stage.addEventListener(MouseEvent.CLICK, spawn);
    }

    function spawn(event)
    {
        for(var i:int = 0; i<1; i++)
        {
            trace("mX in main class = " + mouseX);
            trace("mY in main class = " + mouseY);
            var e:Egg = new Egg(mouseX, mouseY);
            addChild(e);

        }
    }
}
}


package  
{
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;


public class Egg extends MovieClip 
{
    var stageWidth:int = 550;
    var stageHeight:int = 400;
    var buffer:int = 100;
    var mX = 0;
    var mY = 0;

    public function Egg(moX:int, moY:int) //mouseX, mouseY
    {
        mX = moX;
        mY = moY;
        trace("mX in Egg constructor: " + mX);
        trace("mY in Egg constructor: " + mY);

        spawnAtFixedPoint();
        animate();
    }




    function spawnAtFixedPoint()
    {
        this.x = stageWidth + buffer;
        this.y = stageHeight - buffer;
    }

    //Moves the egg in from the spawn point using tweens. Scales down on the way to give 3D illusion.
    function animate()
    {
        var xMovement:Tween;
        var yMovement:Tween;
        var xScale:Tween;
        var yScale:Tween;

        trace("mX in animate() = " + mX);
        trace("mY in animate() = " + mY);
        xMovement = new Tween(this, "x", Regular.easeOut, this.x, mX, 1, true); //obj, property, ease function, start, time, in seconds (true)
        yMovement = new Tween(this, "y", Regular.easeOut, this.y, mY, 1, true);
        xScale = new Tween(this, "scaleX", Regular.easeIn, 1, 0.1, 1, true);
        yScale = new Tween(this, "scaleY", Regular.easeIn, 1, 0.1, 1, true);


        //Checks when the object has finished animating (which mean the egg has landed)
        xMovement.addEventListener(TweenEvent.MOTION_FINISH, splat);
    }

    //Spawns a splat (currently a star) at the same coordinates as the egg
    function splat(event)
    {
        trace("mX in splat() = " + mX);
        trace("mY in splat() = " + mY);
        var s = new Splat(mX, mY);
        addChild(s);
    }

}
}

更新: 我创建了一个自定义 SplatEvent 并让我的主类监听它。偶数在 MOTION_FINISHED 处理程序中调度。有效!

尽管如此,我必须通过在主类中使用公共静态变量来传递回 Egg 着陆点的原始坐标来使其工作。就像在主类中一样:

public static var splatX:int;
public static var splatY:int;

在 Egg 类中就像这样:

eggMain.splatX = mX;
eggMain.splatY = mY;

我可以接受它,但我不想使用全局变量。对此提出建议,尽管主要问题已得到解答。谢谢。

【问题讨论】:

    标签: flash actionscript flash-cs5 tween coordinate


    【解决方案1】:

    您正在鸡蛋实例中创建 splat,然后将其添加到鸡蛋的显示列表中。这意味着 splat(星形)是在蛋的坐标空间中创建的,因此会被蛋在舞台上的位置偏移。这也意味着 splat 继承了您对鸡蛋所做的任何转换,例如比例,因为它是转换后的鸡蛋实例的子对象。

    更好的方法是在主类中侦听鸡蛋的运动完成事件(查看事件冒泡)或从运动完成处理程序触发的自定义事件。当收到该事件时,创建一个 splat 实例并将其添加到主类的显示列表中。

    【讨论】:

    • 谢谢,我会调查自定义事件并尝试您的方法。不过,这如何解释为什么恒星会移位?哪些继承的坐标和变换应该取代星星?
    • 查看更新后的帖子。希望这次我解释得更好。
    • 我创建了一个自定义 SplatEvent 并让我的主类监听它。偶数在 MOTION_FINISHED 处理程序中调度。它有效!
    猜你喜欢
    • 2021-07-07
    • 2020-12-06
    • 2014-09-20
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多