我不知道這是不是最好的辦法,但還挺解決方案可能是這樣的:
1.添加,讓你知道你將有多少對象允許在移動不變同時:
private static final int MAX_TOUCHES = 4;
2.添加一個集合具有固定大小,這樣,你會管理當前是可能將所有的精靈:
現在10
,在你的類,你正在處理觸摸,實施touchDown()
,touchDragged()
和touchUp()
:
/**
* In the touchDown, add the sprite being touched
**/
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// Just allow 4 sprites to move at same time
if(pointer >= MAX_TOUCHES) return true;
// Get the sprite at this current position...
Sprite sprite = getSpriteAtThisPosition(screenX, screenY);
// If sprite found, add to list with current pointer, else, do nothing
if(sprite != null) {
sprites.set(pointer, sprite);
}
return true;
}
getSpriteAtThisPosition()
就是這樣在那個位置返回第一電流精靈的方法,可能會返回null
。
/**
* In the touchDragged, move this sprite
**/
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// Just allow 4 sprites to move at same time
if(pointer >= MAX_TOUCHES) return false;
// Get the sprite with the current pointer
Sprite sprite = sprites.get(pointer);
// if sprite is null, do nothing
if(sprite == null) return false;
// else, move sprite to new position
sprite.setPosition(screenX - sprite.getWidth()/2,
Gdx.graphics.getHeight() - screenY -
sprite.getHeight()/2);
return false;
}
/**
* In the touchUp, remove this sprite from the list
**/
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// Just allow 4 sprites to move at same time
if(pointer >= MAX_TOUCHES) return true;
// remove sprite at pointer position
sprites.set(pointer, null);
return true;
}
您是否想要一次移動多個精靈? –
是的,這是我的意圖 – Boom
謝謝!@DavidAnderton – Boom