AS3 - 简单对象池示例?

发布于 2024-12-22 12:25:36 字数 1275 浏览 4 评论 0原文

预先感谢您提供如此出色的帮助。

我读了很多关于对象池的好处的文章。在网上找到了一些“教程”,都超出了我的技能水平。任何人都可以向我展示一个非常简单的对象池示例。

我的游戏所做的是当用户按住鼠标时创建 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 技术交流群。

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

发布评论

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

评论(2

老旧海报 2024-12-29 12:25:36

这是一篇关于“补间和对象池”的有趣文章:

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/

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