简体   繁体   English

使子弹穿过屏幕的相对侧

[英]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. 现在,播放器1是一个精灵,但是,项目符号是使用arraylist和自制项目符号类创建的。 As a result, I cannot use the code for Player1 that I did for the bullet. 结果,我无法使用对子弹头所做的Player1的代码。 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. 另外,我注意到您为速度使用了2个浮点,并且您确实应该使用Vector2因为这样您就可以轻松缩放和标准化它,这对速度很有用。

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. 对于Player类和Bullet类而言,这实际上是相同的,因为您具有相同的屏幕空间,因此您可以通过将当前代码转换为带有任何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: 您可以在Player上调用此函数,还可以遍历每个项目符号,例如:

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. 我必须同意@Zac的算法对于一颗子弹可以正常工作,但是您需要将其放入循环中以遍历每颗子弹才能生效。

Here is an example of what you could do in your render() method of your game screen: 这是您可以在游戏屏幕的render()方法中执行的操作的示例:

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. 另外,请注意,使用这种方法时,项目符号越多,应用程序就越拖延。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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