对象池不工作

问题描述:

我在设置我的对象池时遇到问题。我创建了一个“BallPoll”自定义类来处理池化逻辑。我首先调用fillPool()将20个Ball对象添加到我的数组中。然后在我的文档类中,当我想创建一个Ball时,我检查了这个池数组。它不工作,我不知道为什么。对象池不工作

------文档类---------

function throwBall(e:TimerEvent):void { 

    if (mouseY>stage.stageHeight-180) { 
     return; 
    } 

    var tBall:Ball = Pool.getBall(new Point(mouseX,mouseY),new Point(Math.random()+Math.random()*5+Math.random()*8),gravity,friction); 
    tBall.gotoAndStop(BallColor); 
    addChild(tBall); 
    ballArray.push(tBall);  

} 

----------- BallPool类---------

package { 


import flash.events.TimerEvent; 
import flash.geom.Point; 
import flash.events.*; 
import flash.display.*; 
import flash.utils.*; 
import flash.system.*; 
import Ball; 

public class BallPool extends MovieClip { 


    private static const gravity:Number=1.5; 
    private static const friction:Number=.50; 
    public var STOREDBALLS:Array = new Array(); 

    public function BallPool() { 

     fillPool(); 

    } 

    public function fillPool() { 

     for (var i:int = 0; i < 20; i++) { 

      var NewBall:Ball = new Ball(new Point(mouseX,mouseY),new Point(Math.random()+Math.random()*5+Math.random()*8),gravity,friction); 
      STOREDBALLS.push(NewBall); 
     } 


    } 

    public function getBall($position:Point, $vector:Point, $gravity:int, $friction:Number):Ball { 

     if (STOREDBALLS.length > 0) { 

      var counter:int = 0; 

      trace(STOREDBALLS.length); 
      var ball:Ball = STOREDBALLS[counter]; 
      trace("44"); 
      counter ++; 
      return ball; 

     } else { 

       return new Ball($position, $vector, $gravity, $friction); 
     } 

     //return null; 
    } 
} 
} 
+1

哪部分不工作?究竟是什么问题? – Cadin 2011-12-28 23:45:10

+0

getBall函数不起作用。我无法弄清楚如何通过STOREDBALLS数组循环,并确定哪些球在舞台上,哪些不在舞台上。此外,正确地从父文档类中回收球。谢谢 – user1114288 2011-12-29 04:57:45

我认为一个水池应该在放回时释放球。这不是一个包含你所有球的列表(对不起),但它是一个你目前不使用的球的列表。所以你的getBall()函数应该返回一个新的Ball并从STOREDBALLS中删除参考。执行此操作的最佳方法是使用pop()shift(),它将从数组中删除最后一个元素并返回该元素的值。

您的计数器有误(它总是0?),不应该这样使用。

我会做这样的:

public function getBall($position:Point, $vector:Point, $gravity:int, $friction:Number):Ball { 
    if (STOREDBALLS.length) { 

     // grab ball from list + remove it 
     var ball:Ball = STOREDBALLS.pop(); 

     // reset its values 
     ball.position = $position; 
     ball.vector = $vector; 
     ball.gravity = $gravity; 
     ball.friction = $friction; 

     return ball; 
    } 

    return new Ball($position, $vector, $gravity, $friction); 
} 

BTW;它看起来像来自PHP背景。在ActionScript 3中,没有人使用美元符号,您不需要使用它们。

更新:要再次将球推入池,你可以使用此功能:

public function addBall($ball:Ball):void { 
    STOREDBALLS.push($ball); 
} 

从你的类,你正在使用的游泳池,你应该使用removeChild()addChild(),显示的处理-list不是游泳池类的责任。

+0

谢谢你的回应!请最后一个问题。我已经实现了你的新的getBall函数。球完美卸载,但如何将它们发回阵列。当我将它们从池中拉出来时,我将这些球存储在我的文档类中的一个名为“ballArray”的数组中。 “Pool”是我的“BallPool.as”类的实例。我尝试调用“Pool.STOREDBALLS.push(ballArray [i]);”但它不起作用。所以总结一下,我怎样才能将我完成的球送回到自定义类中的STOREDBALLS数组中?如何将对象从文档类中的自定义类中推送到数组? – user1114288 2011-12-29 17:50:34

+0

我已经更新了答案,希望有所帮助。 – 2011-12-30 09:27:33