我有一个硬币类,它需要一个整数numOfCoins
和一个枚举LayoutType
,可以是线性或曲线。 线性布局一个接一个地显示硬币的直线 – 我们将使用这个方法,因为这个方法很小且直截了当。
我的构造函数如下所示:
public Coin(float xPos, float yPos, int numOfCoins, LayoutType layout) { setType(ObjectType.COIN); coinList = new ArrayList<Coin>(); // arraylist of coins setPos(xPos, yPos); // set starting position of coin(s) if (layout == LayoutType.LINEAR)) { linearLayout(); // display coins in a straight line } else if (layout == LayoutType.CURVE{ curveLayout(); } }
我的linearLayout()
方法看起来像这样
public void linearLayout() { //draw numOfCoins amount of coins in a straight line for (int i = 0; i < numOfCoins; i++) { Coin coin = new Coin(xPos, yPos); coinList.add(coin); xPos += 50; // place coins 50 pixels apart } }
我有一个工作的animation类和硬币的spritesheet – 然而,我模仿我的硬币的方式是这样的一个硬币对象可以包含numOfCoins
数量的硬币,所以我不能直接调用animation绘制方法。
类的draw()
方法在Coin
扩展的抽象类GameObject
中。 它看起来像这样:
public void draw(SpriteBatch batch) { // if the object is a coin iterate through the list of coins // and draw the sprites, if it isn't a coin, simply draw the objects sprite if (type == ObjectType.COIN) { for (Coin coin : getCoinList()) { coin.getCoinList().draw(batch); } } else { sprite.draw(batch); } }
这个draw()方法在World.render()中被调用。
如果我有6个硬币在一条直线上,我将如何使用我的Animator
类为每个硬币创建一个animation? 我似乎无法find办法。 非常感谢任何帮助,谢谢。
所有的硬币都在做同样的animation吗? 您可以在Animation
类中创建一个将硬币作为参数的方法,然后在该硬币上执行animation。 然后,在另一个类中,创建一个遍历硬币列表的循环,并在您的Animation
类中调用该方法,从而在您浏览列表时为每个硬币设置animation。 您还可以在每个硬币animation之间添加延迟。
顺便说一句,我只是好奇,你有一个coinList
里面的Coin
,每次调用构造函数,你调用linearLayout, which adds
在列表中linearLayout, which adds
numOfCoin`数量的硬币。 难道不是不必要的硬币?