通过 Actionscript3 加载图像两次?

发布于 2024-10-16 16:34:33 字数 2035 浏览 5 评论 0原文

我正在尝试使用我在 这个问题。我正在使用 FlashCS5 IDE 进行开发。

我需要做的绝对最后一件事是在电影的其余部分加载后从外部加载大背景图像(“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;
}
}

I'm trying to use the infinite scroll AS3 I discuss in this SO question. I'm developing this using the FlashCS5 IDE.

The absolutely last thing I need to do is load the large background image ("header_bg_vert.jpg") externally after the rest of the movie has loaded, instead of requiring the entirety of the animation to load beforehand (or preloading the header animation in).

My AS3 is below. The "infinite loop" works by placing two copies of the same seamless image end-to-end, then moving the second image to the front after it scrolls off screen -- currently, this code only displays one copy of the image. What's wrong with it? Also, is this the best way of accomplishing this?

Many thanks.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-10-23 16:34:33

显然它显示了两个副本,但彼此重叠。
发生这种情况是因为 Flash Player 在加载之前不知道 s1 有多高。您应该将图像放置在 loadComplete() 中

Apparently it displays both copies but on top of each other.
It happens because flash player does not know how tall s1 is before it is loaded. You should position your images within loadComplete()

或十年 2024-10-23 16:34:33

您可以加载无缝图像一次,然后使用 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

我使用鼠标事件来滚动图像,因为我不确定您使用的是哪种滚动条,但应该很容易修改它以适应您的场景。

You can load the seemless image once and then use copyPixels() to make 2 copies of the image:

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

I used mouse events to scroll the images as I wasn't sure what kind of scroller you were using, but it should be easy to modify it to fit your scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文