2011-12-24 76 views
1

在此感謝此優秀的幫助來源。AS3 - 簡單對象池示例?

我一直在閱讀很多關於對象池的好處。在網上找到一些「教程」,都在我的技能水平之上。任何人都可以向我展示一個極其簡單的對象池示例。

我的遊戲所做的是在用戶按住鼠標時創建球對象,當用戶擡起鼠標時停止。

我需要將這些Ball對象存儲在一個數組(或Vector)中,然後用其他對象作爲測試對象,在擊中另一個對象時從舞臺上移除它們。我想創建一個如此說20的池,創建一次,並回收它們。

我該怎麼做?如果你能以一種愚蠢的方式來解釋這一點,我將不勝感激。

再次感謝。

---------球類代碼---------

ActionScript代碼:

import flash.events.TimerEvent; 
import flash.geom.Point; 

public class Ball extends Particle { 

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

     //Set initial position 
     x = position.x; 
     y = position.y; 

     updateTimer.addEventListener(TimerEvent.TIMER, setPosition, false, 0, true); 

    } 

    public function setPosition (e:TimerEvent):void { 

     x = position.x; 
     y = position.y; 

    } 
} 

----------- -DOCUMENT類代碼---------------

ActionScript代碼:

function throwBall(e:TimerEvent):void { 

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

    } 
+0

相當不錯的張貼在這裏:http://www.pixelthismobile.com/blog/1/06/2011/elegant-object-pooling-as3 – 2011-12-24 21:58:02

+0

AdamHarte,@ pkyeck的答案中的博客文章要好得多。 。 。其中一條評論使用工廠進行解釋,從長遠來看更容易管理。此外,pixwlthismobile文章中的每條評論似乎都是垃圾郵件! – iND 2011-12-25 04:49:49

回答