如何在我的应用程序中添加它。 我想使用来自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类和gameworld类中渲染所有图形 如果您需要更多信息,请告诉我
我正在尝试使数组工作,但一直发现当一个数组被初始化时,子弹会重新回到原来并最终向后移动???? 如果我创建一个数组,我一直得到异常抛出??? 感谢您提供任何帮助。
答案 0 :(得分:2)
向敌人类添加一个返回边界矩形的函数。
public Rectangle getBounds()
{
return new Rectangle(xPosition,yPosition,width,height);
}
现在在你的屏幕课程或你想要的地方,制作一系列敌人或者所谓的课程。
Array<Enemy> enemies =new Array<Enemy>();
添加你想要的敌人数量
enemies.add(new Enemy(.......));
在触地得分方法中,如果他们被触摸,请检查每一个。
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for(int i=0; i<enemies.size; i++)
if(enemies.get(i).getBounds().contains(screenX,screenY))
enemies.removeIndex(i--); // if we touch an enemy remove it, we use i-- because we want to go back one, so we won't get the out of range exception if the last element in the array is touched!
return false;
}
编辑:
将此添加到您的子弹类。
public Rectangle getBounds()
{
return this.getBoundingRectangle();
}
在你的gamerenderer课程中,只需制作&#39;子弹&#39;
的数组Array<Bullet> enemies=new Array<Bullet>(); // that's it!
要向您的阵列添加项目符号,请使用
enemies.add(new Bullet(x,y));
现在就在你的名单中!
要渲染每个项目符号,请在渲染方法中使用它
for(int i=0; i<enemies.size; i++)
enemies.get(i).draw(batch);
要更新它们,请使用
for(int i=0; i<enemies.size; i++)
enemies.get(i).update(delta);
现在复制上面的触地得分方法!
那就是它!