简体   繁体   中英

Making a bullet pass opposite sides of the screen

So, for a coding assignment we have to make a tank game. I created a bullet class using this:

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

}

My variables are as follows:

Sprite Player1;
Sprite Player2;
ArrayList<Bullet> bullets;

Upon Clicking space it creates the bullet using this:

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

Now, here is my code for getting my tanks to pass through one side of the screen to another:

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

Now, Player 1 is a sprite, however, the bullets are created using an arraylist and a self made bullet class. As a result, I cannot use the code for Player1 that I did for the bullet. SO, my question is, how can I get my bullet to pass to the other side of the screen?

You can use the modulo % operator to do something like this:

bullet.position.x %= Gdx.graphics.getWidth();
bullet.position.y %= Gdx.graphics.getHeight();

This isn't tested but it should work. Also, I noticed you're using 2 floats for your velocity, and you should really be using Vector2 because then you can easily scale and normalise it, which would be useful for a velocity.

It is really the same thing for the Player class and for the Bullet class since you have the same screen-space therefore you can reuse your current code by making it into a function that takes any sprite. This function would be defined as follows:

void fitOnScreen(Sprite sprite) {
    if (sprite.getX() > Gdx.graphics.getWidth()){
        sprite.setX(-64f);
    } else if(sprite.getX()<-64f){
        sprite.setX(Gdx.graphics.getWidth());
    }
    if (sprite.getY() > Gdx.graphics.getHeight()){
        sprite.setY(-64);
    } else if(sprite.getY() < -64f){
        sprite.setY(Gdx.graphics.getHeight());
    }
}

You would call this function on the Player as well as loop over every bullet, such as:

fitOnScreen(Player1);
for (Bullet b: bullets) fitOnScreen(b.b);

Well, what you are asking is how can you run through each instance of the bullets and change their properties.

I have to agree @Zac's algorithm would work fine for one bullet, but you need to put it in a loop to go through each and every bullet in order to be effective.

Here is an example of what you could do in your render() method of your game screen:

Iterator<Bullet> it = bullets.iterator();
while(it.hasNext()) {
    Bullet bullet = it.next();
    // Now you do the position correction
    bullet.b.setPosition(bullet.b.getX()%Gdx.graphics.getWidth(), bullet.b.getY()%Gdx.graphics.getHeight());
}

Of course there are other ways of doing this, but this is probably the easiest way. You could also recycle the code you used for you player in this loop too.

Also, note that with this method, the more bullets, the more laggy the application becomes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM