简体   繁体   English

在游戏中绘制收藏品的最佳方法?

[英]Best way to draw collectibles in a game?

I want my game to draw coins from a sprite and then my main character would be able to pick them up. 我希望我的游戏从一个精灵中提取硬币,然后我的主要角色将能够捡起它们。 I'm trying to use array list for this but i have no idea how to implement this feature. 我正在尝试为此使用数组列表,但我不知道如何实现此功能。 Do I make an array list of Sprites I need to draw and then reuse it ? 我是否需要绘制然后重用的Sprite的数组列表? If so , how ? 如果是这样,怎么办? Or do I use some other function ? 还是使用其他功能? I'm a begginer , so sorry if this question seems odd . 我是一个初学者,如果这个问题看起来很奇怪,请抱歉。

I recommend using Libgdx's Array class instead of a Java ArrayList, as it is optimized for game performance. 我建议使用Libgdx的Array类而不是Java ArrayList,因为它针对游戏性能进行了优化。

Create an Array to hold all the active coins. 创建一个数组以容纳所有活动硬币。

Array<Sprite> activeCoins = new Array<Sprite>();

Then create a coin pool. 然后创建一个硬币池。 A pool is a helper for creating copies of something without causing garbage collection hiccups. 池是用于在不造成垃圾收集故障的情况下创建某些内容的副本的助手。 You need to make a subclass of Pool to use. 您需要使Pool成为子类才能使用。 It describes how to create a new coin when one is needed. 它描述了在需要时如何创建新硬币。

public class CoinPool extends Pool<Sprite> {
    TextureRegion coinTextureRegion;

    public CoinPool(TextureRegion coinTextureRegion){
        this.coinTextureRegion = coinTextureRegion;
    }

    protected Sprite newObject(){
        return new Sprite(coinTextureRegion);
    }
}

Then create an instance of CoinPool to use in your game. 然后创建一个CoinPool实例以在您的游戏中使用。

Now whenever you want a new coin, pull one from the pool. 现在,每当您想要一个新硬币时,就从池中取出一个。 It will only instantiate a new one if necessary. 只会在必要时实例化一个新的实例。 Otherwise, it will hand you a recycled one from earlier. 否则,它将为您提供较早的回收产品。 And once you get it, set it's position and other parameters wherever you want it. 一旦获得它,就可以在任意位置设置它的位置和其他参数。 Keep in mind it might still have old values attached to it, so if you are rotating your coins and moving them around or changing color, you'll want to be sure you reset position and rotation, and color. 请记住,它可能仍附加有旧值,因此,如果您旋转硬币并在周围移动硬币或更改颜色,则需要确保重置位置,旋转和颜色。

Sprite newCoin = coinPool.obtain();
newCoin.setPosition(x,y,z); //as desired 
newCoin.setRotation(0); //if you are rotating them
newCoin.setColor(Color.WHITE); //if you have been changing their color

activeCoins.add(newCoin); //Add your new coin to the list of active coins

And whenever you are done with a coin (when it has been collected), you need to remove it from the active coins list and return it to the pool: 而且,无论何时使用硬币(收集完毕),都需要将其从活动硬币列表中删除,然后将其返回池中:

for (Sprite coin : activeCoins){
    boolean grabbed = isCoinGrabbed(coin); //however you want to do this
    if (grabbed){
        coinGrabbed(); //whatever you want to happen when a coin is grabbed

        activeCoins.remove(coin);
        coinPool.free(coin); //return it to the pool
    }
}

And to draw them, you can just submit all the sprites currently in the activeCoins list to the sprite batch. 要绘制它们,您只需将activeCoins列表中当前的所有Sprite提交给Sprite批处理。

I recently designed a game in Java that was a 2D side scrolling role playing game. 我最近用Java设计了一款2D侧滚动角色扮演游戏。 Part of the game was to walk around and pick up items like swords and armor. 游戏的一部分是四处走走,拿起剑和盔甲之类的物品。

The way that I accomplished loading the sprites to my game was to give a JLabel my image (which was retrieved locally on start of the game) by setting it's icon to the image. 我完成将精灵加载到游戏中的方法是,通过将JLabel的图标设置为图像来为其提供图像(在游戏开始时在本地检索)。

Next I would randomly place my JLabel on the game map. 接下来,我将JLabel随机放置在游戏地图上。 I didn't use an array list, although you could if you had multiple types of swords, or coins, etc. 我没有使用数组列表,但是如果您有多种类型的剑或硬币等也可以使用。

The array list could contain types of swords (JLabel's with their icon set to a sprite) such as a bronze, silver, and a gold sword. 数组列表可以包含剑的类型(JLabel的剑图标设置为精灵),例如青铜,银和金剑。

Once your JLabel('s) is loading to your map you can give them a hit box and if your character moves inside the hit box then remove the JLabel from the map and into the players backpack (or whatever it is, maybe append a coin amount?). 将JLabel载入地图后,您可以给他们一个命中框,如果您的角色在命中框中移动,则将JLabel从地图上移到玩家背包中(或其他任何东西,也许要附加一个硬币)量?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM