2014-06-25 139 views
0

有沒有辦法讓精靈拖動,但只有當精靈本身被觸摸?目前,我有我的遊戲,它使用anengine,設置精靈跟隨你的手指在場景被觸及時的位置。如果你觸摸場景的另一面,精靈會「傳送」,這是我不想要的。讓精靈可以拖動當觸摸

我試着覆蓋精靈的onAreaTouched方法,並將它的座標設置爲你的手指目前的位置,但這不太適用。如果你突然動作,拖延性就會消失。

有沒有簡單的方法來完成這個?

回答

0

回答我的問題......我用這個代碼,它完美地工作:

 draggableSprite = new Sprite(CAM_WIDTH/2, CAM_HEIGHT/2, 
     spriteTextureRegion, mVertexBufferObjectManager){ 

     @Override 
     public boolean onAreaTouched(TouchEvent pSceneTouchEvent, 
       float pTouchAreaLocalX, float pTouchAreaLocalY) { 

       if(pSceneTouchEvent.isActionMove()){ 
        spriteIsTouched = true; 
       } 
       else{ 
        spriteIsTouched = false; 
       } 
      return super 
        .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY); 
     } 

    }; 
    scene.attachChild(draggableSprite); 
    scene.registerTouchArea(draggableSprite); 

    scene.setOnSceneTouchListener(new IOnSceneTouchListener() { 

     @Override 
     public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 
      // TODO Auto-generated method stub 

      if(spriteIsTouched){ 
       draggableSprite.setPosition(pSceneTouchEvent.getX() - (draggableSprite.getWidth()/2), pSceneTouchEvent.getY() - (draggableSprite.getHeight()/2)); 
       //This sets the position of the sprite and then 
       //offsets the sprite so its center is at your finger 
      } 


      return false; 
     } 
    });`