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