无尽的重复滚动背景as3 Adob​​e Air [英] Endless Repeating scrolling Background as3 Adobe Air

查看:154
本文介绍了无尽的重复滚动背景as3 Adob​​e Air的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手脚本。我试图创建一个无尽的滚动背景,从上到下直到舞台,当然是垂直的。我试图通过创建一个影片剪辑,将影像放入一个影片剪辑并应用一些代码来实现,它像一个魅力,但我注意到一些泄漏。因此,我尝试了另一种方法,我发现堆栈委员会,建议使用newBitmapData方法。我发现的问题是背景会从左到右移动,我尝试过,它是完美的。

我试图理解所有的变量和因素,以便改变它并使它从上到下滚动,但我每次都卡住Flash文件的怪异行为。我相信我做错了什么。


这就是stack source委员会的链接,我得到了答案:



这是相同的代码,我

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


public class EndlessBG扩展MovieClip {
//这个保持不动,我们从这里获取图片右边的像素
private var _source:的BitmapData;
//这个是向左移动的(除了一个周期外,右侧的像素是不可见的)。
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);
//打开这个来观察红色像素被绘制在哪里源像素来
// _ source = new BitmapData(_source.width,_source.height,false,0xFF0000);


private function gameLoop(e:Event):void {
_xOffset - ; //这里背景移动到
if(_xOffset< _source.width){
_xOffset = 0;
//这似乎不能正常工作:
//_movingPixels.scroll(_source.width,0);
_movingPixels = new BitmapData(_source.width,_source.height);
_movingPixels.copyPixels(_source,_rect,_point);
}
trace(_xOffset);
setGeometryDefaults();
_movingPixels.scroll(-1,0);
//绘制画布的移动部分
_canvas.bitmapData.copyPixels(_movingPixels,_rect,_point);
//如果我们在这里停下来,我们得到右边的污点
//所以直接从源代码获得剩下的像素
// 1)重置我们的矩形并指向右边
_rect.x = 0;
_rect.width = -_xOffset;
_point.x = _source.width + _xOffset;
// 2)从源
复制_canvas.bitmapData.copyPixels(_source,_rect,_point);

私有函数setGeometryDefaults():void {
_rect.x = 0;
_rect.y = 0;
_rect.width = _source.width;
_rect.height = _source.height;
_point.x = 0;
_point.y = 0;




code


$ b <

解决方案

一般来说,您只想移动或显示,并在您的动画正在运行时隐藏位图。动画运行时不应绘制或复制像素。你在加载时会这样做,因为它会减慢你的fps。将所有内容转换为位图,并在动画开始之前摆脱所有源图形(jpg,png,文本和矢量图像)。

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

您应该将可重复的背景图像(jpg,png)放在动画片段中,或者使用矢量将动画片段中的背景创建为动画片段。将该movieclip绘制到一个位图。删除原始的动画片段。制作两个位图数据的副本。将两个端对端堆叠在一个动画片段中。然后滚动movieclip的背景图像的长度 - 从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);


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




$ b

让我知道任何问题。


I am new to action script. I tried to create an endless scrolling background that goes from up to down the stage, vertically of course. I tried to do it with creating a movie clip, putting the image in a movie clip and applying some code, it worked like a charm, but i noticed some leaks. Therefore i tried another method i found on the stack committee, which suggested using the newBitmapData method. The question i found consisted that a background will move from left to right, i tried it, it was perfect.
I tried to comprehend all the variables and factors in it in order to change it and make it scroll from top to bottom, but i get stuck every time with a weird behavior from the flash file. I am sure i am doing something wrong. I would really appreciate the help.. i am eager to learn.
THis is is stack source committee link i got the answer from :
The link and the answer

Here is the same code that i am trying to implement, copied from the link :

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;
        }

    }

}

I really Appreciate the help...eager to learn.

解决方案

In general, you only want to move or show-and-hide bitmaps when your animation is running. You shouldn't draw or copypixels when the animation is running. You do that stuff during "loading" because it can slow your fps. Convert everything to bitmaps and get rid of all source graphics (jpg, png, text, and vector images) before your animation starts.

For a scrolling background, you should use two copies of a bitmap in a movieclip and move the movieclip.

You should put the repeatable background image (jpg, png) in a movieclip or creat the background in a movieclip using vectors. Draw that movieclip to a bitmap. Remove the original movieclip. Make two copies of the bitmapdata. Stack the two end-to-end in a movieclip. Then scroll the movieclip the length of the background image - start at 0 and scroll until the second copy of the background reaches 0. Then reset the first to 0.

Here's example code that does the above:

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;
    }
}

Let me know of any questions.

这篇关于无尽的重复滚动背景as3 Adob​​e Air的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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