(Java)尝试使用(工作)动画类设置硬币数组列表的动画
我有一个coin类,它接受一个整数(Java)尝试使用(工作)动画类设置硬币数组列表的动画,java,android,animation,libgdx,2d,Java,Android,Animation,Libgdx,2d,我有一个coin类,它接受一个整数numofoins和一个enumLayoutType,可以是线性的,也可以是曲线的。线性布局以一条直线一个接一个地显示硬币-我们将使用这种方法,因为这种方法是小而直接的 我的构造函数如下所示: public Coin(float xPos, float yPos, int numOfCoins, LayoutType layout) { setType(ObjectType.COIN); coinList = new ArrayL
numofoins
和一个enumLayoutType
,可以是线性的,也可以是曲线的。线性布局以一条直线一个接一个地显示硬币-我们将使用这种方法,因为这种方法是小而直接的
我的构造函数如下所示:
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();
}
}
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);
}
}
我有一个工作动画类和一个硬币的精灵表-但是,我对硬币建模的方式是,一个硬币对象可以包含numofoins
硬币数量,因此我不能直接在draw方法中调用动画
类“draw()
方法位于一个抽象类GameObject
中,该类Coin
扩展了它。看起来是这样的:
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();
}
}
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个硬币在一条直线上,我将如何为每个硬币创建动画?我似乎找不到办法。非常感谢您的帮助,谢谢 我被你的班级结构(硬币拥有硬币列表)弄糊涂了。我会为硬币组创建一个单独的类
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 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;
}
游戏或screen类需要在CoinGroup上每帧调用一次update
然后,您可以通过引用相同的动画来绘制每个硬币。我不明白你的取款方法是怎么回事(为什么一枚硬币的取款方法需要检查它是否是一枚硬币?)
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);
}
}
静态最终浮点动画\u交错=0.15f;
公共作废取款(SpriteBatch批量){
对于(int i=0;i您是否熟悉如何使用纹理区域数组填充动画类?(从文档中)是的,这是我的动画类当前所做的。其他对象,如player和Foreign,我可以在其draw()中调用Animator.render()方法但这一类的问题是,一个硬币类可以包含7-8个不同的硬币,所以我不知道该怎么做。我开始写答案,但后来我完全被你的类结构搞糊涂了。硬币是一个包含硬币列表的类。我猜上层硬币包含的硬币没有使用它们自己的数组列表?当收集到组中的单个硬币时,这将变得非常复杂,但其他硬币仍然存在。为什么不为一个硬币组设置一个单独的类?另外,您上面展示的绘制方法是硬币类的绘制方法,还是游戏或屏幕类的绘制方法?