2016-12-06 17 views
0

我想編碼一個LibGDX遊戲。LibGdx下落的對象

目前我正在試圖添加一個圖像,從屏幕的頂部下降到底部,然後再次出現在屏幕上並再次下降,我還需要找到一種方式,可以檢測與玩家的碰撞(我將不得不學習如何正確添加下一個玩家)。

問題是我沒有任何Libgdx這部分的經驗,我還是比較新的。

這是我到目前爲止已經試過,

Image image; 

public Beam(Image image) { 
    this.image = image; 
    image.setPosition(LevelSmash.HEIGHT, LevelSmash.WIDTH); 
} 

public void update(){ 
    image.setY(image.getY() - 1); 
} 

public void draw(SpriteBatch sb){ 
    sb.begin(); 
    //Draw image? 
    sb.end(); 
} 

我已經知道這是非常不好的,我不知道哪個LibGDX類使用,我想知道,如果它是身體還是什麼?

編輯

更新的代碼

public class Beam extends Sprite{ 

Image image; 

Body body; 

public Beam(World world) { 
    BodyDef bodyDef = new BodyDef(); 
    bodyDef.type = BodyDef.BodyType.DynamicBody; 
    bodyDef.position.set(100, 300); 

    body = world.createBody(bodyDef); 

    CircleShape circle = new CircleShape(); 
    circle.setRadius(20f); 

    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = circle; 
    fixtureDef.density = 0.5f; 
    fixtureDef.friction = 0.4f; 
    fixtureDef.restitution = 0.6f; 
    Fixture fixture = body.createFixture(fixtureDef); 
    circle.dispose(); 

    body.setUserData(this); 

    setRegion(new Texture("beam.png")); 
} 

public void update(float dt){ 
} 

public Body getBody(){ 
    return body; 
}} 
+0

什麼是使用box2d(夾具)的原因?如果你不確定如何移動一個精靈並進行碰撞,我認爲你真的不需要開始搞亂物理引擎。 – IronMonkey

回答

2

我認爲你現在應該遠離box2d。在這個項目中使用它似乎有點矯枉過正。

這裏是你如何能解決這個問題的例子:

Beam.class:

public class Beam{ 
    private Rectangle bound; 
    private TextureRegion texture; 
    private float weight = 10; //or gravity or however you want to think about it 

    public Beam(float x, float y, float width, float height){ 
     texture = new Texture(Gdx.files.internal("beam.png")); 
     bound = new Rectangle(x,y,width,height); 
    } 

    public void update(float delta){ 
     bound.y -= weight*delta; 
    } 

    public Rectangle getBound(){ 
     return bound; 
    } 

    public Texture getTexture(){ 
     return texture; 
    } 
} 

您可以通過只是改變了bound.y

得到束到屏幕頂部
beam.getBound().y = newValue; 

你可以在對象本身上做到這一點,或者通過它在遊戲中有意義。

然後,當你添加你給玩家對該對象的約束太多,你可以做簡單的碰撞:

if(player.getBound().overlaps(beam.getBound())){ 
    //collision 
} 

的邊界是從libgdx Rectangle對象。它包含一個x和y座標以及一個寬度和高度。使用矩形使移動物體和碰撞檢測非常簡單。

編輯:

借鑑渲染()質地:

public void render(float delta){ 
    ... 
    beam.update(delta); 

    spriteBatch.begin(); 
    spriteBatch.draw(beam.getTexture(), beam.getBound().x, beam.getBound().y); 
    spriteBatch.end(); 

} 

或者有梁繪製自己:

public void render(float delta){ 
    ... 
    beam.update(delta); 

    spriteBatch.begin(); 
    beam.draw(spritebatch); 
    spriteBatch.end(); 
} 

在光束類:

public void draw(SpriteBatch spriteBatch){ 
    spriteBatch.draw(texture, bound.x, bound.y); 
} 
+0

我將如何添加矩形後面的紋理? – Kingbluesapphire

+0

梁類有一個紋理。你只要把它畫在約束的地方。 我編輯了答案,並添加了兩個示例如何繪製它。 – IronMonkey

+0

謝謝!到目前爲止,這是工作,我遇到的一個問題是你的代碼我使用texture.getHeight和texture.getWidth在構造函數中時,我手動添加它們的作品! – Kingbluesapphire

1

你在正確的軌道上。有兩種方法可以做到這一點 - 一種是使用Box2D(物理系統;你可以找到documentation here on the libGDX wiki),或者建立自己的移動和碰撞檢測(這並不難,對於簡單的遊戲實際上可能會更好)。

我建議您花一些時間仔細閱讀libGDX wiki文章,以便更好地瞭解該庫。有教程,教你如何繪製圖像和處理運動。這就是說,這裏有一些提示:

在你的更新方法中,你的圖像每幀向下移動1個像素。這將成爲一個問題,因爲不同的計算機將以不同的速度執行幀,這意味着您的遊戲在一臺計算機上的速度會比另一臺計算機快。不用說image.setY(image.getY() - 1);,您可能會考慮類似image.setY(imsage.getY() - speed * Gdx.graphics.getDeltaTime());的東西 - 這將確保您每秒鐘移動「速度」像素/單位,而不管計算機有多快或多慢。

+0

非常感謝幫助!我現在有一個圈子的身體,但我不明白如何將紋理附加到該身體..? [代碼:http://pastebin.com/d1B3dHdf] – Kingbluesapphire

+0

看看我發佈的維基鏈接;它有一節討論瞭如何將一個精靈附加到一個正文,然後讓它們保持同步:https://github.com/libgdx/libgdx/wiki/box2d#sprites-and-bodies – John

+0

這就是我所嘗試的似乎沒有工作? http://pastebin.com/XjpbSUZu – Kingbluesapphire