我試圖讓雪碧在小於0時從屏幕邊緣反彈。現在它只是將屏幕放大到虛空。這裏是我的代碼注意,CentipedeBody是一個擴展Sprite的類。在render
方法中,我稱之爲ex.update();
,它是該類的一個對象。然後在batch.begin()
和batch.end()
之間我有batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);
讓雪碧從邊緣反彈
public class CentipedeBody extends Sprite
{
public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) {
super(new TextureRegion(image));
this.position = position;
this.size=size;
bounds=new Rectangle(position.x,position.y,8,8);
left=true;
}
public void update() {
bounds.set(getPosition().x,getPosition().y,8,8);
if (left==true) {
position.x-=(.5f);
up=false;
down=false;
right=false;
left=true;
}
if (right==true) {
position.x+=.5f;
left=false;
right=true;
down=false;
up=false;
}
if (down==true) {
position.y-=(.5f);
right=false;
left=false;
down=true;
up=false;
if(position.x<0)
{
left=false;
right=true;
}
}
}
根據你的代碼片段只有'if(left){}'執行,所以你的精靈只向左移動 – Aryan
那麼我會如何讓if(right)'execute?對於最後一場比賽,我希望精靈向左移動,擊中屏幕邊緣,稍微向下,然後向右移動到另一個邊緣,然後重複。現在我只想至少讓它從邊緣反彈。 – retchers
我們可以通過屏幕邊緣與瓦片進行某種類型的瓦片碰撞嗎? – retchers