如何设置MovieClip位置而不弄乱补间?

发布于 2024-10-16 04:07:59 字数 1924 浏览 2 评论 0原文

我正在开发带有移动平台的 AS3 和 Box2D 游戏。这些平台在 Flash 中进行动画处理,在 ActionScript 中我可以读取其当前位置并调整物理体以进行匹配。

然而,总是存在延迟,即动画比物理提前一帧。我想通过读取剪辑的当前位置,将其存储以供以后使用,然后将剪辑放回到最后一帧的位置来解决此问题。

但当我这样做时,它系统地拒绝让步。

我用一个移动框编写了一个简单的测试来测试这个想法,我遇到了同样的问题(“movingBox”是一个符号,它为其中的单个“框”符号设置动画):

package 
{
    import flash.display.*;
    import flash.events.*;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            m_movingBox = new MovingBoxClass();
            addChild(m_movingBox);

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(i_event:Event) : void
        {
            const box : Sprite = m_movingBox.getChildAt(0) as Sprite;
            trace("frame:", m_movingBox.currentFrame, ", x:", box.x);

            box.x = 0;
        }


        [Embed(source="../lib.swf", symbol="movingBox")]
        private var MovingBoxClass:Class;

        private var m_movingBox : MovieClip;
    }

}

而不是打印出移动的位置盒子,盒子保持静止,跟踪调用输出:

frame: 1 , x: 0
frame: 2 , x: 0
frame: 3 , x: 0
frame: 4 , x: 0
frame: 5 , x: 0
...

有什么想法吗?谢谢

更新: 需要明确的是,如果我删除 box.x = 0; 行,则框会正确移动,并且跟踪调用会输出 x 的递增值。

更新:我举了一些例子:

输出

库,舞台上有“movingBox”

Flash 文件

包含 Flash Develop 项目和其余的

I'm working on a AS3 and Box2D game with moving platforms. The platforms are animated in Flash, and in actionscript I can read in their current position and adjust the physics bodies to match.

However, there's always a delay, where the animation is one frame ahead of the physics. I wanted to fix this by reading in the clip's current position, storing it for later, and then putting the clip back to its position at the last frame.

But when I do this, it systematically refuses to budge.

I coded up a simple test with a single moving box to test the idea, and I get the same problem ("movingBox" is a symbol that animates a single "box" symbol within it):

package 
{
    import flash.display.*;
    import flash.events.*;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            m_movingBox = new MovingBoxClass();
            addChild(m_movingBox);

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(i_event:Event) : void
        {
            const box : Sprite = m_movingBox.getChildAt(0) as Sprite;
            trace("frame:", m_movingBox.currentFrame, ", x:", box.x);

            box.x = 0;
        }


        [Embed(source="../lib.swf", symbol="movingBox")]
        private var MovingBoxClass:Class;

        private var m_movingBox : MovieClip;
    }

}

Instead of printing out the positions of the moving box, the box just stays still, and the trace call outputs:

frame: 1 , x: 0
frame: 2 , x: 0
frame: 3 , x: 0
frame: 4 , x: 0
frame: 5 , x: 0
...

Any ideas? Thanks

UPDATE: Just to be clear, if i remove the box.x = 0; line, the box moves correctly and the trace call spits out increasing values for x.

UPDATE: I put up the examples:

The output

The lib, with "movingBox" on the stage

The Flash file

Zip file containing the Flash Develop project and all the rest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-23 04:07:59

更新:

package{    
import flash.display.*;
import flash.events.*;    
public class Main extends Sprite {

    public function Main():void {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point            
        m_movingBox = new MovingBoxClass();
        this.addChild(m_movingBox);
        m_movingBox.stop();
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onEnterFrame(i_event:Event) : void    {
        var str: String = m_movingBox.getChildAt(0).x + ' - ';
        m_movingBox.gotoAndStop(m_movingBox.currentFrame + 1 < m_movingBox.totalFrames ? m_movingBox.currentFrame + 1 : 0);
        str += m_movingBox.getChildAt(0).x;
        trace(str);
    }        

    [Embed(source="../lib.swf", symbol="movingBox")]
    private var MovingBoxClass:Class;        
    private var m_movingBox : MovieClip;
    }    
 }

现在您可以管理此动画并调整每帧的其他坐标

upd:

package{    
import flash.display.*;
import flash.events.*;    
public class Main extends Sprite {

    public function Main():void {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point            
        m_movingBox = new MovingBoxClass();
        this.addChild(m_movingBox);
        m_movingBox.stop();
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onEnterFrame(i_event:Event) : void    {
        var str: String = m_movingBox.getChildAt(0).x + ' - ';
        m_movingBox.gotoAndStop(m_movingBox.currentFrame + 1 < m_movingBox.totalFrames ? m_movingBox.currentFrame + 1 : 0);
        str += m_movingBox.getChildAt(0).x;
        trace(str);
    }        

    [Embed(source="../lib.swf", symbol="movingBox")]
    private var MovingBoxClass:Class;        
    private var m_movingBox : MovieClip;
    }    
 }

now you can manage this animation and adjust other coords every frame

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文