在AS3中不能平滑滚动

时间:2011-11-07 14:31:12

标签: flash actionscript-3 scroll

我正在尝试在AS3中制作侧滚动游戏,但是当我尝试制作简单无尽的滚动背景时,动画并不流畅。任何人都可以解决这个问题吗?

这是swf(30fps)的链接:

http://megaswf.com/serve/1221647

代码: `包{

import flash.display.*;
import flash.events.*;
public class testscroll extends MovieClip{
    public function testscroll(){
        var bg = new bg1();
        var bg2 = new bg1();
        addChild(bg);
        addChild(bg2);
        bg2.x = 500;
        bg2.addEventListener(Event.ENTER_FRAME,mainloop);
        bg.addEventListener(Event.ENTER_FRAME,mainloop);
    }
    public function mainloop(e:Event){
        var target = e.currentTarget as MovieClip;
        target.x -= 5;
        if(target.x<=-500){
            target.x = 500;
        }
    }
}

} `

由于

3 个答案:

答案 0 :(得分:2)

尝试使用这部分代码:

target.x -= 5;

并将其设为较低的值,例如:

target.x -= 1;

现在你正在每帧移动5个像素,这可能看起来很不稳定。这将减慢滚动的速度,但使其看起来更平滑。你可能想摆弄fps来改变速度。

此外,如果您的背景不是位图,则应将其缓存为位图以提高速度(滚动位图比使用矢量更容易)。

答案 1 :(得分:0)

下面使用blitting来减少处理器上的负载,以及将重绘区域限制在最小可见部分的有用技巧。

我也在这个主题之外进行了相当广泛的评论:我意识到这是一个粗略的准备测试,但它们可能对某些人有用。

package 
{
    import flash.display.*;
    import flash.geom.Rectangle;
    import flash.utils.setTimeout;
    import flash.utils.setInterval;
    import flash.events.Event;

    // Best practice naming convention: CamelCase for classes
    public class TestScroll extends Sprite
    {
        private var __bmp:Bitmap;


        public function TestScroll()
        {           
            __bmp = addChild(new Bitmap(_bmpd)) as Bitmap;

            // These two instructions act in concert to 
            // reduce the area redrawn onEnterFrame
            // to that defined by the scrollRect
            cacheAsBitmap = true;
            scrollRect = new Rectangle(0, 0, 500, 400);

            // Always a good habit to set the value for useWeakReference to true
            // when adding event listeners: helps with garbage collection.
            // See http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html
            // That said, ALWAYS remove your listeners as soon as they're no longer required:
            // http://gingerbinger.com/2010/07/actionscript-3-0-events-the-myth-of-useweakreference/
            addEventListener(Event.ENTER_FRAME, _onEnterFrame, false, 0, true);
        }

        public function _onEnterFrame(event:Event):void
        {
            // Running @ 60fps: reduced move rate               
            __bmp.x = (__bmp.x > -500) ? __bmp.x - 2 : 0;
        }

        // Personal preference of mine: break out basic instantiation
        // into subfunctions to keep the constructor as terse as possible
        // Renamed the references to the library items in line with naming 
        // convention for classes: bg1 -> Background
        private function get _bmpd():BitmapData
        {
            var mc:Sprite = new Sprite(),
                b1:Sprite = mc.addChild(new Background()) as Sprite,
                b2:Sprite = mc.addChild(new Background()) as Sprite,
                bmpd:BitmapData;

            b2.x = 500;

            bmpd = new BitmapData(mc.width, mc.height, true, 0);
            bmpd.draw(mc);

            return bmpd;
        }
    }
}

有关scrollRect / cacheAsBitmap技术的更全面说明,请参阅http://jacksondunstan.com/articles/629

答案 2 :(得分:0)

看看这个:

http://megaswf.com/serve/1368761/

采用不同的方法将原始mc绘制到位图,然后将移动的源矩形绘制到静态位图中。

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    [SWF(backgroundColor="#FFFFFF", frameRate="60", width="500", height="400")]
    public class Main extends Sprite
    {
        [Embed(source="assets/swf/assets.swf", symbol="Background")]
        private var Background:Class;

        private var __app_w:int = 500;
        private var __app_h:int = 400;
        private var __src_rect:Rectangle = new Rectangle(0, 0, __app_w, __app_h);
        private var __src_bmpd:BitmapData;
        private var __dest_bmpd:BitmapData = new BitmapData(__app_w, __app_h);
        private var __dest_pt:Point = new Point();


        /* Constructor
        ----------------------------------------------------------------------------------------------*/
        public function Main()
        {
            stage.addEventListener(Event.RESIZE, _onStageResized, false, 0, true);
            stage.align     = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            // Initialise
            __src_bmpd       = _bmpd;            
            scrollRect       = __src_rect;
            cacheAsBitmap    = true;
            opaqueBackground = 0xFFFFFF;

            addChild(new Bitmap(__dest_bmpd)) as Bitmap;
            addEventListener(Event.ENTER_FRAME, _onEnterFrame, false, 0, true);
        }

        private function _onStageResized(event:Event):void
        {            
            x = (stage.stageWidth >> 1)  - (__app_w >> 1);
            y = (stage.stageHeight >> 1) - (__app_h >> 1);
        }

        public function _onEnterFrame(event:Event):void
        {
            __src_rect.x = (__src_rect.x == __app_w) ? 0 : __src_rect.x + 2;                
            __dest_bmpd.copyPixels(__src_bmpd, __src_rect, __dest_pt);
        }

        private function get _bmpd():BitmapData
        {
            var mc:Sprite = new Sprite(),
                b1:Sprite = mc.addChild(new Background()) as Sprite,
                b2:Sprite = mc.addChild(new Background()) as Sprite,
                bmpd:BitmapData;

            b1.x = 0;
            b2.x = b1.width;

            bmpd = new BitmapData(mc.width, mc.height, false); 
            bmpd.draw(mc);

            return bmpd;
        }
    }
}