1

これをアプリケーションに追加するにはどうすればよいですか。画面上の敵を殺すために、InputProcessor の実装から onTouch() メソッドを使用したいと考えています。それ、どうやったら出来るの?敵のクラスに何かしなければなりませんか?また、敵の配列を追加しようとしていますが、例外がスローされ続けるか、弾丸クラスでフリップメソッドを使用した後、弾丸が左を向いています。

すべてのコードは以下にあるので、どなたでもお気軽にご覧ください。助けてください ありがとう

M

// これは弾丸クラスです。

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 クラスですべてのグラフィックスをレンダリングしています。

配列を機能させようとしていますが、配列が初期化されると、弾丸が元に戻り、逆になってしまうことがわかり続けています???? 配列を作成すると、例外がスローされ続けますか??? 助けてくれてありがとう。

4

1 に答える 1

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 クラスで、「Bullet」の配列を作成するだけです

Array<Bullet> enemies=new Array<Bullet>(); // that's it!

配列に箇条書きを追加するには、次を使用します

enemies.add(new Bullet(x,y));

そして今、あなたのリストにあります!

すべての弾丸をレンダリングするには、render メソッドでこれを使用します

    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);

そして、上記の touchdown メソッドをコピーしてください!

それでおしまい!

于 2014-06-08T18:56:25.707 に答える