AS3 - 简单对象池示例?
预先感谢您提供如此出色的帮助。
我读了很多关于对象池的好处的文章。在网上找到了一些“教程”,都超出了我的技能水平。任何人都可以向我展示一个非常简单的对象池示例。
我的游戏所做的是当用户按住鼠标时创建 Ball 对象,当用户抬起鼠标时停止。
我需要将这些 Ball 对象存储在数组(或向量)中,并用其他对象对它们进行命中测试,当命中另一个对象时将它们从舞台上删除。我想创建一个包含 20 个这样的池,创建一次,然后回收它们。
我该怎么做?如果您能以一种简单的方式解释这一点,我将不胜感激。
再次感谢。
---------Ball 类代码---------
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;
}
}
------------文档类代码 ----------- ----
动作脚本代码:
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);
}
thanks in advance for this excellent source of help.
I've been reading a lot about the benefits of Object Pooling. Found some "tutorials" online, all above my skill level. Can anyone please show me an extremely simple example of an Object Pool.
What my game does is creates Ball objects when the user holds down the mouse, stops when user lifts up mouse.
I need to store these Ball objects in an array(or Vector), and hit test them with other objects, removing them from the stage when the hit another object. I'd like to create a pool of say 20 of so, created once, and recycle them.
How would I do this? If you could explain this in a dumbed down way I would greatly appreciate it.
Thanks again.
---------Ball Class code---------
ActionScript Code:
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 CLASS CODE ---------------
ActionScript Code:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一篇关于“补间和对象池”的有趣文章:
http://blog.joa-ebert.com/2008/05/07/tweening-and-object-pools/
this is an interesting article about "tweening and object pooling":
http://blog.joa-ebert.com/2008/05/07/tweening-and-object-pools/
我做了这个可以帮助您开始:
http:// www.plugin.io/2012/11/general- Purpose-as3-object-pool/
I made this that can help you get started:
http://www.plugin.io/2012/11/general-purpose-as3-object-pool/