开发者

Trouble in dueling with loader loading the same thing twice

I have a background symbol that does the loading external jpg job, and a engine as3 that loops 2 backgrounds continuously on the screen. However, only one background can load file although they "new" from the same symbol. To test this urr... phenomenon, I exchange the order of "new" code of 2 backgrounds, then whichever on top will perfectly load the jpg file and the other will remain blank. has anyone faced this problem before? or knowing how to solve it? plz teach me. this external loading is really painful to me from the beginning.

For the code: (because it is very very basic so I think I could neglect it; sorry for the inconvenience)

Background code

var URLvar:URLRequest;
var fBGLoader : Loader = new Loader( );
URLvar = new URLRequest( "bac开发者_Go百科kground collection/fyrise/fyriseFront.png" );
fBGLoader.load(URLvar);
this.addChild(fBGLoader);

Engine code

private var B1:fBG;
private var B2:fBG;
private var Container= new MovieClip();
BLen=2500;
B2=new fBG();
B1=new fBG();

B1.x=0;
B1.y=0;
B2.x=B1.x-BLen;
B2.y= B1.y;
Container.addChild(B1);
Container.addChild(B2);
stage.addChild(Container);

so as you can see, I put the B2 = new fBG() prior to B1=new fBG(), then I can see B2 but not B1 and if I exchange the order, I can see B1 but not B2. That's the problem!


Are you loading the image twice or just once? If you load the image once and use it for both fBG objects you would get that problem since Bitmap objects can only have a single parent (the last addChild(fBGLoader) is the only one working, the first one will be cancelled by the later one).

To fix this, you need to make sure that both fBG objects have their own Bitmap instance (they can however share the same BitmapData). So wherever you are doing :

this.addChild(fBGLoader);

Do this instead :

var img : Bitmap = Bitmap(fBGLoader.content);
this.addChild(new Bitmap(img.bitmapData));

This will make sure that each fBG object has it's own set of coordinates and information to display the background image.


Is the "background code" the fBG class? And if so, is that code being run in the constructor? If so, I think your solution should work. But it's hard to say without seeing a bit more context.

You should be able to load the same image twice and not have a problem. So my guess is that your issue is revolving around how you've set up your classes (e.g. the context I mentioned before). I'm guessing you've got some other issue there that isn't shown in the code you've provided.

As an aside, you don't really need two separate loaders if you just want two of the same image. You could just load the image once and then use the Bitmap classes to create a copy of the Loader's content.

Oh, and on second look, your statement:

BLen=2500;

won't work. You need something like:

var BLen:Number = 2500;


i put together a small example over at wonderfl: http://wonderfl.net/c/1dze

i just try and load the same image twice - and it works. here is the code:

package {
  import flash.net.URLRequest;
  import flash.display.Loader;
  import flash.display.Sprite;
  public class FlashTest extends Sprite {

     private var loader1:Loader;
     private var loader2:Loader;

     public function FlashTest() {
        loader1 = new Loader();
        loader2 = new Loader();
        loader2.x = 40;
        loader2.y = 40;

        loader1.load(new URLRequest("http://4.bp.blogspot.com/_zqzgnaU9IiE/TIuSdJyuWsI/AAAAAAAAAA0/4qVZxO503vE/s1600/fat+cat.jpg"));
        loader2.load(new URLRequest("http://4.bp.blogspot.com/_zqzgnaU9IiE/TIuSdJyuWsI/AAAAAAAAAA0/4qVZxO503vE/s1600/fat+cat.jpg"));
        addChild(loader1);
        addChild(loader2);
     }
  }
}

the problem has to be somewhere else in your code.

maybe try changing the position of the movieclips first to see if both are loaded - like i did in the code above.

edit

you could also put this into two classes like so:

package {
  import flash.display.Sprite;

  public class FlashTest extends Sprite {
    private var bgLoader1:BGLoader;
    private var bgLoader2:BGLoader;

    public function FlashTest() {
        bgLoader1 = new BGLoader();
        bgLoader2 = new BGLoader();
        bgLoader2.x = 40;
        bgLoader2.y = 40;
        addChild(bgLoader1);
        addChild(bgLoader2);
    }
  }
}

and then

package {
  import flash.display.Loader;
  import flash.display.Sprite;

  public class BGLoader extends Sprite {
    private var loader:Loader;
    public function BGLoader() {
        loader = new Loader();
        loader.load("URLTOIMAGE");
        addChild(loader);
    }
  }
}


One of your instances is at -2500 px x coordinate on the screen, if I read your code correctly. Is it possible that the graphic is loading, but because it is so far to the left of the viewport, you can't see it?

Also, I agree with some of the other posters that it is probably better to only load the file once, then duplicate it. You can find out more about techniques for doing this here: http://www.adobe.com/devnet/flash/articles/blitting_mc.html.

HTH;

Amy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜