本文介绍了跟踪未命名的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用libgdx做一个自上而下的2D射击游戏。会有很多,我想跟踪和处理,当他们去关闭屏幕的子弹对象。我在想,我会使用类似

I want to make a top-down 2d shooting game using libgdx. There will be a lot of bullet objects that I want to keep track of and dispose when they go off the screen. I was thinking that I would use something like

static ArrayList<Bullet> bullets;

要跟踪我的子弹在子弹类,检查这个数组列表是在屏幕的任何子弹,处理他们,如果他们都和删除ArrayList中的子弹。我想知道,这是做到这一点的最好办法。看来喜欢的事,应该是pretty的普遍,所以我想确保,这是这样做的最好方法。

to keep track of my bullets in the Bullet class, check this array list for any bullets that are off the screen, dispose of them if they are, and delete that bullet from the ArrayList. I was wondering if this is the best way to do this. It seems like something that should be pretty common and so I wanted to make sure that this is the best way of doing this.

推荐答案

使用对象便便 L是一个更好的办法。

Using an object pool is a better approach.

对象池的想法很简单,非常适合在这种情况下,您的需求。池处理创建和存储子弹的对象。所有你需要做的是调用当你需要一个子弹(用户笋)获得池的方法调用池的回收方法,这种子弹时,它的无不再需要在比赛中(走到屏幕外)。

The idea of an object pool is simple and fits perfectly for your needs in this case. The pool handles creating and storing the Bullet objects. All you have to do is to call the obtain method of the pool when you need a bullet (User shoots) and call the recycle method of the pool for this bullet when it's no longer needed in the game (Goes offscreen).

下面是一个<一个href="http://$c$c.google.com/p/andengine/source/browse/src/org/anddev/andengine/util/pool/GenericPool.java?r=081b452a22a79e04865e6ef72a60d1ceb3731482"相对=nofollow> code例如的从AndEngine源采取了游泳池。你可以用它来学习它是如何工作,使自己的池类(或用这一个)。

Here is a code example for a pool taken from AndEngine's source. You can use it to learn how it works and make your own pool class (Or use this one).

这篇关于跟踪未命名的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:39
查看更多