调用函数更改矢量后绘制的位图消失

发布于 2024-11-07 00:15:48 字数 10436 浏览 3 评论 0原文

我正在尝试随机化向量中 MovieClip 的位置。添加到主舞台的显示列表中。我有这个函数可以生成具有属性的影片剪辑:

    private function initMovieClips():Vector.<MovieClip>                                                                                                                                                                                
    {                                                                                                                                                                                                                                   
        var initVec:Vector.<MovieClip> = new Vector.<MovieClip>();                                                                                                                                                                      

        for (var i:int = 0; i < Math.ceil(this.numBlocks / 2); i++)                                                                                                                                                                     
        {                                                                                                                                                                                                                               

            var typeVarianceChance:Number = Math.random();                                                                                                                                                                              
            var colorVarianceChance:Number = Math.random();                                                                                                                                                                             

            var ranType:Number = Math.floor(this.numTypes * Math.random());                                                                                                                                                             

            var coRanType:Number;                                                                                                                                                                                                       
            if (typeVarianceChance < .2)                                                                                                                                                                                                
            {                                                                                                                                                                                                                           
                coRanType = Math.floor(this.numTypes * Math.random());                                                                                                                                                                  
            } else {                                                                                                                                                                                                                    
                coRanType = this.typeHash[ranType];                                                                                                                                                                                     
            }                                                                                                                                                                                                                           

            var ranColor:Number = Math.floor(this.imageURLs.length * Math.random());                                                                                                                                                    

            var coRanColor:Number;                                                                                                                                                                                                      
            if (colorVarianceChance > .1)                                                                                                                                                                                               
            {                                                                                                                                                                                                                           
                coRanColor = Math.floor(this.imageURLs.length * Math.random());                                                                                                                                                         
            } else {                                                                                                                                                                                                                    
                coRanColor = ranColor;                                                                                                                                                                                                  
            }                                                                                                                                                                                                                           

            var mc:MovieClip = new MovieClip();                                                                                                                                                                                         
            var comc:MovieClip = new MovieClip();                                                                                                                                                                                       

            mc.type = ranType;                                                                                                                                                                                                          
            comc.type = coRanType;                                                                                                                                                                                                      

            mc.color = ranColor;                                                                                                                                                                                                        
            comc.color = coRanColor;                                                                                                                                                                                                    

            initVec.push(mc,comc);                                                                                                                                                                                                      
        }                                                                                                                                                                                                                               
        return initVec;                                                                                                                                                                                                                 
    } 

然后加载位图并将其绘制到每个图像上。然后,我调用此函数来随机“洗牌”向量。

    private function shuffle(vec:Vector.<MovieClip>):Vector.<MovieClip>                                                                                                                                                                 
    {                                                                                                                                                                                                                                   
        var shuffled:Vector.<MovieClip> = new Vector.<MovieClip>();                                                                                                                                                                     
        var origLength:uint = vec.length;                                                                                                                                                                                               

        for (var i:int = 0; i < origLength; i++){                                                                                                                                                                                       
            var ranPos:uint = Math.floor(Math.random() * vec.length);                                                                                                                                                                   
            shuffled.push(vec[ranPos]);                                                                                                                                                                                                 
            delete vec[ranPos];                                                                                                                                                                                                         
        }                                                                                                                                                                                                                               
        return shuffled;                                                                                                                                                                                                                
    }

如果我不调用随机播放功能,则位图绘制得很好。但是如果我在 Vector 上调用 shuffle 函数。在我将某些影片剪辑添加到显示列表后(我已经测试过并且知道正在发生这种情况),将其绘制到屏幕上的某些影片剪辑不会显示。有些是随机出现的,有些则不会。每次运行应用程序时,哪些显示,哪些不显示是随机的(也许这与我的 initMovieClips 函数有关,该函数使用伪随机代码?)。

我尝试在代码中的不同点实现 shuffle 函数,在已经绘制位图的地方,以及在我什至没有调用该函数来开始加载位图的地方(所以我认为这不是线程问题)。我什至尝试插入代码在每个 MovieClip 的图形属性上绘制一个简单的测试圆。只是为了看看是否是位图的问题,但未绘制的位图没有显示位图或测试圆。这让我相信这是图形属性本身的问题。这让我完全难住了。关于它可能是什么有什么想法吗?

I am trying to randomize the positions of MovieClips in a Vector. to be added to the main stage's display list. I have this function that generates the MovieClips with their properties:

    private function initMovieClips():Vector.<MovieClip>                                                                                                                                                                                
    {                                                                                                                                                                                                                                   
        var initVec:Vector.<MovieClip> = new Vector.<MovieClip>();                                                                                                                                                                      

        for (var i:int = 0; i < Math.ceil(this.numBlocks / 2); i++)                                                                                                                                                                     
        {                                                                                                                                                                                                                               

            var typeVarianceChance:Number = Math.random();                                                                                                                                                                              
            var colorVarianceChance:Number = Math.random();                                                                                                                                                                             

            var ranType:Number = Math.floor(this.numTypes * Math.random());                                                                                                                                                             

            var coRanType:Number;                                                                                                                                                                                                       
            if (typeVarianceChance < .2)                                                                                                                                                                                                
            {                                                                                                                                                                                                                           
                coRanType = Math.floor(this.numTypes * Math.random());                                                                                                                                                                  
            } else {                                                                                                                                                                                                                    
                coRanType = this.typeHash[ranType];                                                                                                                                                                                     
            }                                                                                                                                                                                                                           

            var ranColor:Number = Math.floor(this.imageURLs.length * Math.random());                                                                                                                                                    

            var coRanColor:Number;                                                                                                                                                                                                      
            if (colorVarianceChance > .1)                                                                                                                                                                                               
            {                                                                                                                                                                                                                           
                coRanColor = Math.floor(this.imageURLs.length * Math.random());                                                                                                                                                         
            } else {                                                                                                                                                                                                                    
                coRanColor = ranColor;                                                                                                                                                                                                  
            }                                                                                                                                                                                                                           

            var mc:MovieClip = new MovieClip();                                                                                                                                                                                         
            var comc:MovieClip = new MovieClip();                                                                                                                                                                                       

            mc.type = ranType;                                                                                                                                                                                                          
            comc.type = coRanType;                                                                                                                                                                                                      

            mc.color = ranColor;                                                                                                                                                                                                        
            comc.color = coRanColor;                                                                                                                                                                                                    

            initVec.push(mc,comc);                                                                                                                                                                                                      
        }                                                                                                                                                                                                                               
        return initVec;                                                                                                                                                                                                                 
    } 

Bitmaps are then loaded and drawn onto each of the images. I then invoke this function to randomly "shuffle" the Vector.

    private function shuffle(vec:Vector.<MovieClip>):Vector.<MovieClip>                                                                                                                                                                 
    {                                                                                                                                                                                                                                   
        var shuffled:Vector.<MovieClip> = new Vector.<MovieClip>();                                                                                                                                                                     
        var origLength:uint = vec.length;                                                                                                                                                                                               

        for (var i:int = 0; i < origLength; i++){                                                                                                                                                                                       
            var ranPos:uint = Math.floor(Math.random() * vec.length);                                                                                                                                                                   
            shuffled.push(vec[ranPos]);                                                                                                                                                                                                 
            delete vec[ranPos];                                                                                                                                                                                                         
        }                                                                                                                                                                                                                               
        return shuffled;                                                                                                                                                                                                                
    }

If I don't invoke the shuffle function the Bitmaps are drawn just fine. But if I invoke the shuffle function on my Vector. to be drawn onto the screen some of the MovieClips are not displaying after I add them to the display list (which I've tested and know is happening). Some randomly appear and some don't. Which ones display and which ones don't appears to be random each time I run my application (maybe it has something to do with my initMovieClips function, which uses pseudorandom code?).

I've tried implementing the shuffle function at different points in my code, in places where the Bitmaps have already been drawn, and in places where I haven't even invoke the function to start loading the Bitmaps (so I don't think it's a threading issue). I even tried inserting code to draw a simple test circle on every MovieClip's graphics property. Just to see if it was a problem with the Bitmaps, but the one's that weren't being drawn showed no Bitmap or test circle. This leads me to believe that it's a problem with the graphics property itself. Which leaves me completely stumped. Any ideas on what it could be?

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

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

发布评论

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

评论(1

暖风昔人 2024-11-14 00:15:48

我认为可能是在 for 循环中。您正在循环遍历 vec 的长度(在本例中我假设是 initVec)。但是为了获得 vec 的索引,您需要进行随机化,

var ranPos:uint = Math.floor(Math.random() * vec.length); 
shuffled.push(vec[ranPos]); 

所以现在发生的情况是,尽管您可能会获得从 0 到 vec 长度的值,但您可能会重复相同的值两次。因此,如果 vec.length = 5,您可能会得到 1, 2, 3,2,4,在这种情况下,您会丢失 5 并获得 2 两次,这个顺序基本上是“随机”的,因此在大规模中,您可能不会遍历所有你的 vec[] 值的

另一种方法是使用 while 循环,如果 random 带来一个已经使用过的索引,则重试直到所有索引值都被使用(强力),如果你是 vec.length 是巨大的尝试http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms

I think the probably is in the for loop. You are looping through the length for your vec (which in this case I'm going to assume is initVec). But in order to get the index of vec you do a randomize

var ranPos:uint = Math.floor(Math.random() * vec.length); 
shuffled.push(vec[ranPos]); 

so now whats happening is that although you may getting values from 0 to length of vec, you may be repeating the same value twice. So if vec.length = 5 you may get 1, 2, 3,2,4 in which case you are missing 5 and getting 2 twice, this order is basically "random" so in a large scale you may not be going though all your vec[] values

another way of doing this would be to use a while loop and if random brings an index thats already been used retry until all index values have been used (brute force), if you're vec.length is massive try http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms

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