如何在我的应用程序中添加它。我想使用 InputProcessor 实现中的 onTouch() 方法来杀死屏幕上的敌人。我怎么做?我必须对敌人阶级做些什么吗?我也在尝试添加一个敌人数组,它不断抛出异常,或者在我在子弹类中使用翻转方法之后,子弹现在再次面向 LEFT <---。
所有代码都在下面,所以请任何人随意看看谢谢。请帮忙谢谢
米
// 这是子弹类。
public class Bullet extends Sprite {
public static final float BULLET_HOMING = 6000;
public static final float BULLET_SPEED = 300;
private Vector2 velocity;
private float lifetime;
private Rectangle bul;
public Bullet(float x, float y) {
velocity = new Vector2(0, 0);
setPosition(x, y);
AssetLoader.bullet1.flip(true, false);
AssetLoader.bullet2.flip(true, false);
setSize(AssetLoader.bullet1.getWidth(), AssetLoader.bullet1.getHeight());
bul = new Rectangle();
}
public void update(float delta) {
float targetX = GameWorld.getBall().getX();
float targetY = GameWorld.getBall().getY();
float dx = targetX - getX();
float dy = targetY - getY();
float distToTarget = (float) Math.sqrt(dx * dx + dy * dy);
dx /= distToTarget;
dy /= distToTarget;
dx *= BULLET_HOMING;
dy *= BULLET_HOMING;
velocity.x += dx * delta;
velocity.y += dy * delta;
float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
velocity.x /= vMag;
velocity.y /= vMag;
velocity.x *= BULLET_SPEED;
velocity.y *= BULLET_SPEED;
bul.set(getX(), getY(), getOriginX(), getOriginY());
Vector2 v = velocity.cpy().scl(delta);
setPosition(getX() + v.x, getY() + v.y);
setOriginCenter();
setRotation(velocity.angle());
}
public Rectangle getBounds() {
return bul;
}
public Rectangle getBounds1() {
return this.getBoundingRectangle();
}
}
// 这是我从中加载所有图像的类
public class AssetLoader {
public static Texture texture;
public static TextureRegion bg, ball1, ball2;
public static Animation bulletAnimation, ballAnimation;
public static Sprite bullet1, bullet2;
public static void load() {
texture = new Texture(Gdx.files.internal("SpriteN1.png"));
texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
bg = new TextureRegion(texture, 80, 421, 395, 30);
bg.flip(false, true);
ball1 = new TextureRegion(texture, 0, 321, 32, 32);
ball1.flip(false, true);
ball2 = new TextureRegion(texture, 32, 321, 32, 32);
ball2.flip(false, true);
bullet1 = new Sprite(texture, 380, 350, 45, 20);
bullet1.flip(false, true);
bullet2 = new Sprite(texture, 425, 350, 45, 20);
bullet2.flip(false, true);
TextureRegion[] balls = { ball1, ball2 };
ballAnimation = new Animation(0.16f, balls);
ballAnimation.setPlayMode(Animation.PlayMode.LOOP);
}
Sprite[] bullets = { bullet1, bullet2 };
bulletAnimation = new Animation(0.06f, aims);
bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);
}
public static void dispose() {
texture.dispose();
}
// 这是用于渲染或绘图到屏幕/画布上。
public class GameRenderer {
private Bullet bullet;
private Ball ball;
public GameRenderer(GameWorld world) {
myWorld = world;
cam = new OrthographicCamera();
cam.setToOrtho(true, 480, 320);
batcher = new SpriteBatch();
// Attach batcher to camera
batcher.setProjectionMatrix(cam.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);
// Call helper methods to initialize instance variables
initGameObjects();
initAssets();
}
private void initGameObjects() {
ball = GameWorld.getBall();
bullet = myWorld.getBullet();
scroller = myWorld.getScroller();
}
private void initAssets() {
ballAnimation = AssetLoader.ballAnimation;
bulletAnimation = AssetLoader.bulletAnimation;
}
public void render(float runTime) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batcher.begin();
batcher.disableBlending();
batcher.enableBlending();
batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());
batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY(), bullet.getOriginX(), bullet.getOriginY(), bullet.getWidth(), bullet.getHeight(), 1.0f, 1.0f, bullet.getRotation());
// End SpriteBatch
batcher.end();
}
}
// 我猜这是在屏幕上加载图像等
public class GameWorld {
public static Ball ball;
private Bullet bullet;
private ScrollHandler scroller;
public GameWorld() {
ball = new Ball(480, 273, 32, 32);
bullet = new Bullet(10, 10);
scroller = new ScrollHandler(0);
}
public void update(float delta) {
ball.update(delta);
bullet.update(delta);
scroller.update(delta);
}
public static Ball getBall() {
return ball;
}
public ScrollHandler getScroller() {
return scroller;
}
public Bullet getBullet() {
return bullet;
}
}
//这是输入处理程序类
public class InputHandler implements InputProcessor {
private Ball myBall;
private Bullet bullet;
private GameRenderer aims;
// Ask for a reference to the Soldier when InputHandler is created.
public InputHandler(Ball ball) {
myBall = ball;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
如果您需要更多信息,我正在渲染 GameRender 类和游戏世界类中的所有图形,请告诉我
我正在尝试使数组工作,但不断发现当数组初始化时,子弹飞回原来的位置并最终倒退????如果我创建一个数组,我会不断抛出异常???感谢您提供的任何帮助。