2015-10-07 47 views
2

我在java android studio中使用libgdx。我刚刚开始。我正在开发android手机。我没有使用任何相机。我想要的只是屏幕所有四边的精灵反弹而不用敲击。我试了很多代码,我认为会工作,但没有。我希望你们能帮助我。我期待尽快回答。由于如何让屏幕两侧的精灵弹跳

这是我有:

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

}

+0

你确切的问题是什么?你的代码的哪部分不起作用?更具体一点总会帮助其他人回答你的问题。 – tobloef

+0

我试过这个if语句和translatex来移动精灵,并在碰到一边时反弹回来。但现在它dosnt移动或反弹 –

+0

你有更多的代码比你迄今显示的代码? – tobloef

回答

3

我做了你想要的一个非常简单的版本:

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

这将导致以下:

http://gfycat.com/TatteredCarefreeHapuku

有很多方法可以改进这个代码,例如,你可以使用矢量,我不会推荐在你的最终产品中使用它,但它可以帮助你弄清楚如何为你自己的项目做这样的事情。

+0

你的男人。兄弟非常感谢。生病看看它是否工作 –

+0

是的,它的工作。我实际上正在为此工作3天。你在30分钟左右就做完了。哇。我吮吸。 :p –

+0

我爱它的人;) –