添加雪碧的多个实例? [英] Adding multiple instances of a Sprite?

查看:131
本文介绍了添加雪碧的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个自定义图像选取器,它显示6替代版本。然而,照片只显示6号项目。

_model.selectedPhoto 返回雪碧,并且不会让应用程序正常运行。

然而,当我使用 _model.photos [II] ,照片将被添加到每个项目 - 这是为什么?我需要添加 _model.selectedPhoto 每个 S:雪碧

 的(VAR II:INT;二< 6;二++)
        {
            //创建BG
            变种S:雪碧=新的Sprite();
            s.graphics.beginFill(的Math.random()* 0XFFFFFF,0.4);
            s.graphics.drawRect(0,0,291,184);
            //添加照片
            VAR号码:雪碧=新的Sprite();
            p.addChild(_model.selectedPhoto);
            p.scaleX = p.scaleY = 0.2;
            p.x = 0;
            p.y = 0;
            s.addChild(对​​);
            cards.push(多个);
        }
 

解决方案

嗯这只是展示,因为你正在运行一个for循环6次,将所选择的照片,以连续的每个新的精灵在一张照片上的6项。你看,当您将显示对象添加到另一个显示对象,然后添加相同的显示对象到别的东西,原来的项目是从第一个显示对象拉,并添加到下一个。我知道这可能是很难理解的时候写的方式,以便让打破下来是这样的:

  VAR照片:雪碧=新的Sprite();

VAR container1:雪碧=新的Sprite();

VAR container2的:雪碧=新的Sprite();

//添加到第一容器
container1.addChild(照片);

//在当添加到下一个容器此时,对象从container1取出并放在里面容器2
container2.addChild(照片);
 

这就是为什么当你使用_model.photos [II]添加了独特的图片给每个你让你的for循环,因为有添加到每个容器的唯一项目的6个新集装箱的精灵。您正在使用数组索引(二VAR),它与每个循环递增访问这些独特的项目。

如果你想在同一个画面添加到每个六个项目,那么你将需要复制原始图像数据的6倍。你可以做到这一点的方法之一是使用URLLoader对象,并重新加载的二进制数据,构成了原始图片。你应该这样做,像这样:

  VAR originalPictureLoader:的URLLoader =新的URLLoader();

originalPictureLoader.addEventListener(引发Event.COMPLETE,originalPictureLoaded);

originalPictureLoader.dataFormat = URLLoaderDataFormat.BINARY;

originalPicture.load(新的URLRequest(http://www.mysite.com/picture.jpg));

私有函数originalPictureLoaded(五:事件):无效
{
    VAR pictureBytes:的ByteArray =的URLLoader(e.currentTarget).DATA为ByteArray的;

    VAR imageDiplicateLoader:装载机;

    对于(VAR II:INT;二< 6;二++)
    {
        //创建BG
        变种S:雪碧=新的Sprite();
        s.graphics.beginFill(的Math.random()* 0XFFFFFF,0.4);
        s.graphics.drawRect(0,0,291,184);
        //添加照片
        VAR号码:雪碧=新的Sprite();

        imageDiplicateLoader =新的Loader();
        imageDiplicateLoader.loadBytes(pictureBytes);

        p.addChild(imageDiplicateLoader);
        p.scaleX = p.scaleY = 0.2;
        p.x = 0;
        p.y = 0;
        s.addChild(对​​);
        cards.push(多个);
    }
}
 

现在请记住,这只是一种方法,我已经写了这个把我的头顶部。所以,我不知道,但你可能需要复制里面的pictureBytes数据的循环,但我不这么认为。如果你确实有,这是你如何做到这一点:

  VAR bytesCopy:ByteArray的=新的ByteArray();
pictureBytes.position = 0;
bytesCopy.writeBytes(pictureBytes);
 

I am building a Custom Image Picker, that shows 6 alternative versions. However the photo is only showing on the 6th item.

_model.selectedPhoto returns a Sprite, and does not let the app function correctly.

However when I use _model.photos[ii], A photo is added to each item - Why is this? I need to add _model.selectedPhoto to each s:Sprite

        for (var ii:int; ii < 6; ii++)
        {
            //Create BG
            var s:Sprite = new Sprite();
            s.graphics.beginFill(Math.random() * 0xffffff, 0.4);
            s.graphics.drawRect(0, 0, 291, 184);
            //add Photo
            var p:Sprite = new Sprite();
            p.addChild(_model.selectedPhoto);
            p.scaleX = p.scaleY = 0.2;
            p.x = 0;
            p.y = 0;
            s.addChild(p);
            cards.push(s);
        }

解决方案

Ummm it's only showing the one photo on the 6th item because you're running a for loop 6 times and adding the selected photo to each new sprite in succession. You see when you add a display object to another display object, and then add that same display object to something else, the original item is pulled from the first display object and added to the next. I know that might be hard to understand when written that way so lets break to down this way:

var photo:Sprite = new Sprite();

var container1:Sprite = new Sprite();

var container2:Sprite = new Sprite();

//Add to the first container
container1.addChild(photo);

//At this point when you add to the next container, the object is removed from container1 and placed inside container 2
container2.addChild(photo);

That is why when you use _model.photos[ii] you add a unique picture to each of the 6 new container sprites you're making in your for loop, because there are unique items to add to each container. You're accessing these unique items by using an array index (the ii var) which increments with each loop.

If you want to add the same picture to each six items, then you're going to need to duplicate the data of the original picture 6 times. One way you can do this is by using a URLLoader object and reloading the binary data that makes up the original picture. You'd do this like so:

var originalPictureLoader:URLLoader = new URLLoader();

originalPictureLoader.addEventListener(Event.COMPLETE, originalPictureLoaded);

originalPictureLoader.dataFormat = URLLoaderDataFormat.BINARY;

originalPicture.load(new URLRequest("http://www.mysite.com/picture.jpg"));

private function originalPictureLoaded(e:Event):void
{
    var pictureBytes:ByteArray = URLLoader(e.currentTarget).data as ByteArray;

    var imageDiplicateLoader:Loader;

    for (var ii:int; ii < 6; ii++)
    {
        //Create BG
        var s:Sprite = new Sprite();
        s.graphics.beginFill(Math.random() * 0xffffff, 0.4);
        s.graphics.drawRect(0, 0, 291, 184);
        //add Photo
        var p:Sprite = new Sprite();

        imageDiplicateLoader = new Loader();
        imageDiplicateLoader.loadBytes(pictureBytes);

        p.addChild(imageDiplicateLoader);
        p.scaleX = p.scaleY = 0.2;
        p.x = 0;
        p.y = 0;
        s.addChild(p);
        cards.push(s);
    }
}

Now keep in mind this is just one method AND I've written this off the top of my head. So, I'm not sure but you MAY need to duplicate the pictureBytes data inside the for loop but I don't believe so. If you did have to, this is how you'd do it:

var bytesCopy:ByteArray = new ByteArray();
pictureBytes.position = 0;
bytesCopy.writeBytes(pictureBytes);

这篇关于添加雪碧的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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