无尽的重复滚动背景as3 Adob​​e Air

时间:2016-04-01 08:28:04

标签: actionscript-3 flash air

我是动作脚本的新手。我试图创建一个无休止的滚动背景,从舞台上下到舞台,当然是垂直的。我尝试创建一个影片剪辑,将图像放在影片剪辑中并应用一些代码,它就像一个魅力,但我注意到一些泄漏。因此我尝试了另一种我在堆栈委员会中找到的方法,它建议使用newBitmapData方法。我发现的问题是背景将从左向右移动,我尝试过,它是完美的 我试图理解其中的所有变量和因素,以便更改它并使其从上到下滚动,但我每次都遇到来自flash文件的奇怪行为。我确信我做错了什么。我真的很感激帮助..我渴望学习。
这是堆栈源委员会链接我得到了答案:
The link and the answer

以下是我尝试实施的相同代码,从链接中复制:

package  {
    import flash.display.MovieClip;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.geom.Point;


    public class EndlessBG extends MovieClip{
        //this one stays stationary, we're getting the pixels for the right side of the pic from here
        private var _source:BitmapData;
        //this is the one moving to the left (the pixels for the right side are not visible except for once a cycle);
        private var _movingPixels:BitmapData;
        private var _canvas:Bitmap;
        private var _xOffset:int = 0;
        private var _rect:Rectangle = new Rectangle();;
        private var _point:Point = new Point();

        public function EndlessBG() {
            super();
            _source = new BathroomStillLife();
            _canvas = new Bitmap(new BitmapData(_source.width, _source.height));
            _canvas.bitmapData.draw(_source);
            _canvas.x = stage.stageWidth/2 - _canvas.width/2;
            _canvas.y = 5;
            addChild(_canvas);
            addEventListener(Event.ENTER_FRAME, gameLoop);
            setGeometryDefaults();
            _movingPixels = new BitmapData(_source.width, _source.height);
            _movingPixels.copyPixels(_source, _rect, _point);
            //turn this on to watch red pixels be drawn where the source pixels are coming in
            //_source = new BitmapData(_source.width, _source.height, false, 0xFF0000);
        }

        private function gameLoop(e:Event):void {
            _xOffset--;//where the background is moving to
            if (_xOffset < -_source.width) {
                _xOffset = 0;
                //this doesn't seem to work correctly:
                //_movingPixels.scroll(_source.width, 0);
                _movingPixels = new BitmapData(_source.width, _source.height);
                _movingPixels.copyPixels(_source, _rect, _point);
            }
            trace(_xOffset);
            setGeometryDefaults();
            _movingPixels.scroll(-1, 0);
            //draw the moved part of the canvas
            _canvas.bitmapData.copyPixels(_movingPixels, _rect, _point);
            //If we stop here, we get a smear to the right
            //so, get the remaining pixels directly from the source
            //1) reset our rect and point to be to the right side
            _rect.x = 0;
            _rect.width = -_xOffset;
            _point.x = _source.width + _xOffset;
            //2) copy from the source
            _canvas.bitmapData.copyPixels(_source, _rect, _point);
        }
        private function setGeometryDefaults():void {
            _rect.x=0;
            _rect.y=0;
            _rect.width = _source.width;
            _rect.height = _source.height;
            _point.x = 0;
            _point.y = 0;
        }

    }

}

我非常感谢帮助...渴望学习。

2 个答案:

答案 0 :(得分:1)

通常,您只想在动画运行时移动或显示和隐藏位图。动画运行时,不应绘制或使用copypixels。你在“加载”期间执行这些操作,因为它可以减慢你的fps。在动画开始之前,将所有内容转换为位图并删除所有源图形(jpg,png,文本和矢量图像)。

对于滚动背景,您应该在动画片段中使用两个位图副本并移动动画片段。

您应该将可重复的背景图像(jpg,png)放在动画片段中,或者使用矢量在动画片段中创建背景。将该动画片段绘制到位图。删除原始动画片段。制作两个bitmapdata副本。将两个端到端堆叠在一个动画片段中。然后滚动动画片段背景图像的长度 - 从0开始并滚动,直到背景的第二个副本达到0.然后将第一个重置为0。

以下是执行上述操作的示例代码:

public function Main() {

    nBackgroundSpeed = 1;

    iBitmapWidth = mcBackground.mcBackgroundSource.width;
    iBitmapHeight = mcBackground.mcBackgroundSource.height;

    var bdBackground: BitmapData = new BitmapData(iBitmapWidth, iBitmapHeight, true, 0x00000000);
    bdBackground.draw(mcBackground.mcBackgroundSource, null, null, null, null, true);
    var bmpBackground0: Bitmap = new Bitmap(bdBackground, "auto", false);
    var bmpBackground1: Bitmap = new Bitmap(bdBackground, "auto", false);

    mcBackground.removeChild(mcBackground.mcBackgroundSource);
    mcBackground.mcBackgroundSource = null;

    mcBackground.addChild(bmpBackground0);
    bmpBackground0.x = 0;
    bmpBackground0.y = 0;

    mcBackground.addChild(bmpBackground1);
    bmpBackground1.x = 0;
    bmpBackground1.y = iBitmapHeight;

    addEventListener(Event.ENTER_FRAME, fEnterFrame);
}


private function fEnterFrame(event: Event): void {
    if (mcBackground.y > (0 - iBitmapHeight)) {
        mcBackground.y -= nBackgroundSpeed;
    } else {
        mcBackground.y = 0;
    }
}

让我知道任何问题。

答案 1 :(得分:0)

我对adobe Air并不太熟悉,但这也是我遇到的问题。我使用flashdevelop和FlashPunk库(两者都完全免费),我找到了一个解决方案here

祝你好运!