【问题标题】:(Java) Trying to animate an arraylist of coins with a (working) animation class(Java)尝试使用(工作)动画类对硬币数组列表进行动画处理
【发布时间】:2015-03-25 11:05:40
【问题描述】:

我有一个硬币类,它接受一个整数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 
        }
}

我有一个工作动画类和一个用于硬币的精灵表 - 但是,我为硬币建模的方式是 1 个硬币对象可以包含 numOfCoins 数量的硬币,所以我不能直接在draw方法中调用动画。

类的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 个硬币在一条直线上,我将如何为每个硬币创建动画?我似乎找不到方法。非常感谢任何帮助,谢谢。

【问题讨论】:

  • 您是否熟悉如何使用一组 TextureRegions 填充 Animation 类? (来自文档)
  • 是的,这就是我的动画课程目前所做的。其他对象,如玩家和敌人,我可以在他们的 draw() 方法中调用 Animator.render() 但这个问题是一个硬币类可以包含 7-8 种不同的硬币,所以我不知道该怎么做
  • 我开始写答案,但后来我完全被你的班级结构弄糊涂了。 Coin 是一个包含 Coin 列表的类。我猜上层 Coin 包含的 Coin 没有使用它们自己的 ArrayLists?当收集到该组中的单个硬币时,这将变得非常复杂,但其他硬币仍然存在。为什么不为 CoinGroup 设置一个单独的类?另外,您在 Coin 类或 Game 或 Screen 类的 draw 方法上方显示的 draw 方法是什么?

标签: java android animation libgdx 2d


【解决方案1】:

我对你的类结构感到困惑(拥有硬币列表的硬币)。我会为硬币组创建一个单独的类。

public class Coin {
    Vector2 position;

    public Coin (float x, float y){
        position = new Vector2(x, y);
    }
}

public class CoinGroup extends GameObject {
    // This would be very similar to your current Coin class, and contain a list of 
    // the new Coins.
}

为 CoinGroup 类提供将由其所有项目共享的 Animation 的引用,以及用于跟踪已用时间的浮点变量。

Animation coinAnimation;
float elapsedTime;

public CoinGroup(float xPos, float yPos, int numOfCoins, LayoutType layout,
            Animation coinAnimation) {
        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();
        }
        this.coinAnimation = coinAnimation;
}

然后您需要更新每一帧的经过时间以用于动画。您可能已经有某种更新方法可以将其粘贴,但如果您不这样做,它将是这样的:

public void update (float deltaTime){
    elapsedTime += deltaTime;
}

游戏或屏幕类需要每帧调用一次 CoinGroup 上的update

然后您可以通过引用相同的动画来绘制每个硬币。我不明白你的 draw 方法发生了什么(为什么硬币的 draw 方法需要检查它是否是硬币?),但这是一般的想法。

public void draw(SpriteBatch batch) {
    TextureRegion coinFrameRegion = coinAnimation.getKeyFrame(elapsedTime);
    for (Coin coin : coinList){
        Vector2 coinPosition = coin.position;
        batch.draw(coinFrameRegion , coinPosition.x, coinPosition.y);
    }
}

如果你想变得花哨,你可以错开动画。

static final float ANIMATION_STAGGER = 0.15f;

public void draw(SpriteBatch batch) {
    for (int i=0; i<coinList.size; i++){
        Vector2 coinPosition = coin.position;
        batch.draw(coinAnimation.getKeyFrame(elapsedTime + i*ANIMATION_STAGGER ), coinPosition.x, coinPosition.y);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2019-11-27
    相关资源
    最近更新 更多