2011-02-11 129 views
0

我想使用無限滾動AS3我在this SO question討論。我正在使用FlashCS5 IDE進行開發。通過Actionscript3加載圖像兩次?

我需要做的絕對最後一件事是在電影的其餘部分加載完畢後從外部加載大背景圖像(「header_bg_vert.jpg」),而不是要求事先加載整個動畫(或者預加載標題動畫)。

我的AS3如下。 「無限循環」通過將兩個相同的無縫圖像端到端地放置兩個副本,然後在將第二個圖像滾動到屏幕之後移動到前方來工作 - 目前,此代碼僅顯示圖像的一個副本。它出什麼問題了?另外,這是完成這個的最好方法嗎?

非常感謝。

stop(); 

//load the bg image 
var request:URLRequest = new URLRequest("header_bg_vert.jpg"); 
var s1:Loader = new Loader(); 
var s2:Loader = new Loader(); 

s1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress); 
s1.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); 

s2.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress); 
s2.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); 


function loadProgress(event:ProgressEvent):void { 
var percentLoaded:Number = event.bytesLoaded/event.bytesTotal; 
percentLoaded = Math.round(percentLoaded * 100); 
trace("Loading: "+percentLoaded+"%"); 
} 
function loadComplete(event:Event):void { 
trace("Complete"); 
} 

s1.load(request); 
s2.load(request); 

//The speed of the scroll movement. 
var scrollSpeed:uint = 1; 

//Originally, this added two instances of the movie clip onto the stage. 
//var s1:ScrollBg = new ScrollBg(); 
//var s2:ScrollBg = new ScrollBg(); 

addChild(s1); 
addChild(s2); 
setChildIndex(s1, 0); 
setChildIndex(s2, 0); 
//This positions the second movieclip next to the first one. 
s1.y = 0; 
s2.y = s1.height; 

//Adds an event listener to the stage. 
stage.addEventListener(Event.ENTER_FRAME, moveScroll); 

//This function moves both the images to left. If the first and second 
//images goes pass the left stage boundary then it gets moved to 
//the other side of the stage. 
function moveScroll(e:Event):void{ 
s1.y -= scrollSpeed; 
s2.y -= scrollSpeed; 

if(s1.y <= -s1.height){ 
s1.y = s1.height - scrollSpeed; 
}else if(s2.y <= -s2.height){ 
s2.y = s2.height - scrollSpeed; 
} 
} 

回答

1

顯然它顯示兩個副本,但在彼此之上。 發生這種情況的原因是Flash播放器在加載之前不知道s1有多高。你應該位置)內loadComplete您的圖像(

+0

您好,先生,贏得了萬億美元的互聯網。這一直在擾亂我至少一個星期。謝謝! – aendrew 2011-02-11 09:14:30

1

您可以一次裝載無縫圖像,然後使用copyPixels(),使圖像的2份:

import flash.display.Bitmap; 
import flash.display.BitmapData; 
import flash.display.Loader; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.events.ProgressEvent; 
import flash.geom.Point; 
import flash.geom.Rectangle; 
import flash.net.URLRequest; 

var bitmap1:Bitmap; 
var bitmap2:Bitmap; 

var loader:Loader = new Loader(); 
loader.load(new URLRequest("images/seemless.png")); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete); 
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress); 

function onLoaderProgress(e:ProgressEvent):void 
{ 
    var percent:Number = Math.floor((e.bytesLoaded/e.bytesTotal)*100); 

}// end function 

function onLoaderComplete(e:Event):void 
{ 
    var bitmap:Bitmap = Bitmap(e.target.content); 

    var bitmapData1:BitmapData = new BitmapData(bitmap.width, bitmap.height); 
    var bitmapData2:BitmapData = new BitmapData(bitmap.width, bitmap.height); 

    bitmapData1.copyPixels(bitmap.bitmapData, 
          bitmap.bitmapData.rect, 
          new Point(0,0)); 

    bitmapData2.copyPixels(bitmap.bitmapData, 
          bitmap.bitmapData.rect, 
          new Point(0,0)); 

    bitmap1 = new Bitmap(bitmapData1); 
    var bitmapSprite1:Sprite = new Sprite(); 

    bitmapSprite1.addChild(bitmap1); 

    bitmap2 = new Bitmap(bitmapData2); 
    var bitmapSprite2:Sprite = new Sprite(); 
    bitmapSprite2.addChild(bitmap2); 
    bitmap2.x = bitmap2.width; 

    addChild(bitmapSprite1); 
    addChild(bitmapSprite2); 

    stage.addEventListener(MouseEvent.MOUSE_OVER, onStageMouseOver); 
    stage.addEventListener(MouseEvent.MOUSE_OUT, onStageMouseOut); 

}// end function 

function onStageEnterFrame(e:Event):void 
{ 
    var speed:Number = 10; 

    if(stage.mouseX < stage.stageWidth/2) 
    { 
     bitmap1.x -= speed; 
     bitmap2.x -= speed; 

    }// end if 

    if(stage.mouseX > stage.stageWidth/2) 
    { 
     bitmap1.x += speed; 
     bitmap2.x += speed; 

    }// end if 

    if(bitmap1.x <= -(bitmap1.width)) 
    { 
     bitmap1.x = bitmap1.width; 

    }// end if 

    if(bitmap2.x <= -(bitmap2.width)) 
    { 
     bitmap2.x = bitmap2.width; 

    }// end if 

    if(bitmap1.x >= bitmap1.width) 
    { 
     bitmap1.x = -(bitmap1.width); 

    }// end if 

    if(bitmap2.x >= bitmap2.width) 
    { 
     bitmap2.x = -(bitmap2.width); 

    }// end if 

}// end function 

function onStageMouseOver(e:MouseEvent):void 
{ 
    stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame); 

}// end function 

function onStageMouseOut(e:MouseEvent):void 
{ 
    if(stage.hasEventListener(Event.ENTER_FRAME)) 
    stage.removeEventListener(Event.ENTER_FRAME, onStageEnterFrame); 

}// end function 

我用鼠標事件,我沒來滾動圖片不知道你使用的是什麼樣的滾輪,但應該很容易修改它以適應你的情況。

+0

+10 for copyPixels();碼。我今天在兩個有線上網本上加載了我的文件,這很殘酷。現在快得多。此外 - 我試過你的代碼,它似乎不滾動兩次後加載第二個圖像。我可能已經拙劣地將它調整到我自己的目的地,但是你可能想要調查這些代碼是否在任何地方。 – aendrew 2011-03-09 08:52:39