AS3-在屏幕上移动MovieClip [英] AS3 - Move MovieClip across screen

查看:204
本文介绍了AS3-在屏幕上移动MovieClip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要同时在屏幕上移动3个不同的MovieClip. (从下到上) 使用Caurina之类的补间类,执行此操作的最佳方法是什么?

I've 3 different MovieClips that I need to move at the same time across the screen. (From bottom to top) What is the best way of doing this without using a tweening class like Caurina?

谢谢你的提示.

推荐答案

您可以在显示对象的父容器中添加事件侦听器,以监听Event.ENTER_FRAME事件.在每个Event.ENTER_FRAME事件上,您都可以像下面的示例那样简单地减小显示对象的y属性.

You could add an event listener to the parent container of the display objects which listens for the Event.ENTER_FRAME event. On each Event.ENTER_FRAME event you simply decrement the y property of the display objects like in the following example.

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    [SWF(width="600", height="500")]
    public class Main extends Sprite 
    {
        private var _squares:Vector.<Square>;

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

        }// end function

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

            _squares = new Vector.<Square>();

            var redSquare:Square = new Square(0xFF0000, 100);
            redSquare.x = 0; 
            redSquare.y = 400;
            addChild(redSquare);

            var greenSquare:Square = new Square(0x00FF00, 100);
            greenSquare.x = 300; 
            greenSquare.y = 300;
            addChild(greenSquare);

            var blueSquare:Square = new Square(0x0000FF, 100);
            blueSquare.x = 500; 
            blueSquare.y = 100;
            addChild(blueSquare);

            _squares.push(redSquare, greenSquare, blueSquare);

            addEventListener(Event.ENTER_FRAME, onEnterFrame);

        }// end function

        private function onEnterFrame(e:Event):void
        {
            for each(var square:Square in _squares)
            {
                if (square.y > 0) square.y -= 5;

            }// end for

        }// end function

    }// end class

}// end package

import flash.display.Sprite;

internal class Square extends Sprite
{
    public function Square(color:uint, size:Number)
    {
        graphics.beginFill(color);
        graphics.drawRect(0, 0, size, size);
        graphics.endFill();

    }// end function

}// end function

我认为,即使您只使用Greensock的补间平台,您仍可以使自己的生活更轻松代替.

I think you'd be making life easier for yourself though if you simply used Greensock's Tweening platform instead.

这篇关于AS3-在屏幕上移动MovieClip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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