1
我正在創建精靈並將它添加到隨機場景中的場景中。我只想檢查新創建的Sprite是否會與現有的Sprite產生衝突。有沒有簡單的方法來檢查它?Andengine - 添加精靈避免與現有的碰撞
我正在創建精靈並將它添加到隨機場景中的場景中。我只想檢查新創建的Sprite是否會與現有的Sprite產生衝突。有沒有簡單的方法來檢查它?Andengine - 添加精靈避免與現有的碰撞
當你創建一個新的精靈,用戶數據添加到它:
sprite.setUserData("sprite");
然後,您創建了一個定位精靈後,您將其添加遍歷現有的精靈前:
int count = scene.getChildCount();
for(int i = 0; i < count; i++) {
IEntity entity = scene.getChild(i);
if(entity instanceof Sprite) {
if(entity.getUserData().equals("sprite"))
if(((Sprite)entity).collidesWith(newSprite))
//Don't add the new sprite.
}
用戶數據可以是任何你想要的,它不一定是一個字符串。
謝謝 - 我做了這樣的循環,只是工作,如果有比檢查所有其他對象的collisision更有效的方法。 – piotrpo 2012-01-29 14:30:32