简体   繁体   English

如何使精灵在屏幕的侧面反弹

[英]How to make sprite bounce of the sides of the screen

I'm using libgdx in java android studio. 我在Java Android Studio中使用libgdx。 i have just started. 我才刚开始。 i'm working on android phone. 我正在使用Android手机。 i not using any cameras. 我没有使用任何相机。 all i want is a sprite bounce of all four sides of the screen without tapping. 我想要的只是屏幕所有四个侧面的精灵反弹而无需点击。 i tried many codes i thought would work but nope. 我尝试了很多我认为会起作用的代码,但没有。 i hope u guys can help me. 我希望你们能帮助我。 I'm expecting an answer soon as possible. 我希望尽快得到答案。 Thanks 谢谢

this is what i have: 这就是我所拥有的:

SpriteBatch batch;
Texture background;
Sprite backgroundsprite;
Sprite ballsprite;
Texture line;
Texture ballimg;
BitmapFont credits;
BitmapFont input;
BitmapFont play;
float dt;
String string = "";
float ballx;
float bally;


float speedx;
float speedy;

Rectangle screenrect;
Rectangle ballrect;
float screenLeft ;
float screenBottom ;
float screenTop ;
float screenRight ;


@Override
public void create() {
    batch = new SpriteBatch();
    speedx = 5f * dt;
    speedy = 5f * dt;
    createsprite();
    createbackground();
    createtext();

    ballx = ballsprite.getX();
    bally = ballsprite.getY();

}
@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    dt = Gdx.graphics.getDeltaTime();



    ballsprite.setPosition(ballx + speedx,ballsprite.getY());
    ballsprite.translateX(speedx);

    float left = ballrect.getX();
    float bottom = ballrect.getY();
    float top = bottom + ballrect.getHeight();
    float right = left + ballrect.getWidth();



    if(left < screenLeft) {
        string = "left";
        speedx = 5f*dt;
    }
    if(right > screenRight)
    {
        string = "right";
        speedx = -5f*dt;
    }

    if(bottom < screenBottom)
    {
        string = "bottom";

    }
    if(top > screenTop)
    {
        string = "top";

    }



    batch.begin();
    backgroundsprite.draw(batch);
    ballsprite.draw(batch);
    rendertext();

    batch.end();
}

public void createbackground() {

    background = new Texture("images/BackgroundGodwin.jpg");
    backgroundsprite = new Sprite(background);
    backgroundsprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    screenrect = new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    screenLeft = screenrect.getX();
    screenBottom = screenrect.getY();
    screenTop = screenBottom + screenrect.getHeight();
    screenRight = screenLeft + screenrect.getWidth();

}

public void createsprite() {

    ballimg = new Texture("images/SpriteGodwin.png");
    ballsprite = new Sprite(ballimg);
    ballsprite.setScale(0.65f);
    ballsprite.setPosition(Gdx.graphics.getWidth()/3,Gdx.graphics.getHeight()/2);
    ballrect = new Rectangle(ballsprite.getBoundingRectangle());
}

@Override
public void dispose() {
    batch.dispose();
    ballimg.dispose();
    background.dispose();
    credits.dispose();
    play.dispose();
    input.dispose();
    line.dispose();
}

public void createtext(){
    play = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    play.setColor(com.badlogic.gdx.graphics.Color.GOLD);

    credits = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    credits.setColor(com.badlogic.gdx.graphics.Color.GOLD);

    input = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    input.setColor(com.badlogic.gdx.graphics.Color.OLIVE);
}

public void rendertext() {
    credits.draw(batch, "Maded", Gdx.graphics.getWidth() / 7 - 50, Gdx.graphics.getHeight() / 9);
    play.draw(batch, "Touch the Screen to play!!", Gdx.graphics.getWidth() / 2 - 175, Gdx.graphics.getHeight() - 80);
    input.draw(batch, string, Gdx.graphics.getWidth() / 2 - 160, Gdx.graphics.getHeight() - 120);
}

} }

I made a very simple version of what you want: 我对您想要的内容做了一个非常简单的版本:

public class BouncyGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture ball;
    float speedX = 3f;
    float speedY = 3f;
    int x;
    int y;

    @Override
    public void create () {
        batch = new SpriteBatch();
        ball = new Texture("ball.png");
        x = Gdx.graphics.getWidth()/2;
        y = Gdx.graphics.getHeight()/2;
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //When the ball's x position is on either side of the screen.
        //The width of the sprite is taken into account.
        if (x > Gdx.graphics.getWidth() - ball.getWidth()/2 || x < 0 + ball.getWidth()/2) {
            //Here we flip the speed, so it bonces the other way.
            speedX = -speedX;
        }
        //Same as above, but with on the y-axis.
        if (y > Gdx.graphics.getHeight() - ball.getHeight()/2 || y < 0 + ball.getHeight()/2) {
            speedY = -speedY;
        }

        //Move the ball according to the speed.
        x += speedX;
        y += speedY;

        batch.begin();
        //Draw the ball so the center is at x and y. Normally it would be drawn from the lower left corner.
        batch.draw(ball, x - ball.getWidth()/2, y - ball.getHeight()/2);
        batch.end();
    }
}

It will result in the following: 结果如下:

http://gfycat.com/TatteredCarefreeHapuku http://gfycat.com/TatteredCarefreeHapuku

There are numerous ways to improve this code, you could for example use vectors, and I wouldn't recommend using it in your final product, but it might help you figure out how to do something like this for your own project. 有很多方法可以改进此代码,例如可以使用向量,但我不建议在最终产品中使用它,但是它可以帮助您弄清楚如何为自己的项目做类似的事情。

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

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