因此,对于编码分配,我们必须制作坦克游戏。让子弹通过屏幕的两侧
package com.MyName.battletanks;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Bullet {
public Sprite b;
public float vx = 0;
public float vy = 0;
public Bullet(Sprite temp) { b = temp;}
public void move() { b.translate(vx*3,vy*3);}
}
我的变量如下:
Sprite Player1;
Sprite Player2;
ArrayList<Bullet> bullets;
一旦点击空间,它将使用这个子弹:我用这个创建了一个子弹类
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
Bullet bullet = new Bullet(new Sprite(new Texture("Bullet1.png")));
bullet.b.setPosition(Player1.getX() + Player1.getWidth()/2, Player1.getY() + Player1.getHeight()/2);
float rotation = (float) Math.toRadians(Player1.getRotation());
bullet.vx = (float) Math.cos(rotation);
bullet.vy = (float) Math.sin(rotation);
bullets.add(bullet);
}
现在,这里是我的代码让我的坦克通过屏幕的一侧到另一个:
if (Player1.getX() > Gdx.graphics.getWidth()){
Player1.setX(-64f);
} else if(Player1.getX()<-64f){
Player1.setX(Gdx.graphics.getWidth());
}
if (Player1.getY() > Gdx.graphics.getHeight()){
Player1.setY(-64);
} else if(Player1.getY() < -64f){
Player1.setY(Gdx.graphics.getHeight());
}
现在,玩家1是精灵,但是,子弹是使用数组列表和自制子弹类创建的。因此,我不能使用我为子弹做的Player1代码。所以,我的问题是,我怎样才能让我的子弹通过屏幕的另一端?
无论我放在哪里,bullet.x中的x都显示为红色,并且它一直给我一个错误。不太确定该怎么做。然而,vector2确实工作得很好。 –
@LucasZ这是因为你没有在你的Bullet Class中写入变量x和y!你的位置在Sprite中。因此,你应该做'bullet.b.setPosition(bullet.b.getX()%Gdx.graphics.getWidth(),bullet.b.getY()%Gdx.graphics.getHeight());' – Fish
如果这工作对你来说,你能接受它作为答案吗? :) – Zac