2013-03-18 43 views
0

我必須沿着繪製onTouch.For,我使用的路徑和PathModifier路徑移動精靈添加動態路徑雪碧

case MotionEvent.ACTION_UP: 

     int historySize = pSceneTouchEvent.getMotionEvent().getHistorySize(); 
     pointX = new float[historySize]; 
     pointY = new float[historySize]; 

     for (int i = 1; i < historySize; i++) { 

      pointX[i] = pSceneTouchEvent.getMotionEvent().getHistoricalX(i); 
      pointY[i] = pSceneTouchEvent.getMotionEvent().getHistoricalY(i); 

     } 
     path = new path(pointX,pointY); 
     PathModifier pathModifier = new PathModifier(2.5f, path); 
     pathModifier.setRemoveWhenFinished(true); 
     sprite1.clearEntityModifiers(); 
     sprite1.registerEntityModifier(pathModifier); 

     break; 

它給了我錯誤的路徑需要至少2個航點。 任何想法爲什麼這樣?

回答

1

通常這不應該發生,因爲運動事件往往不僅僅是一個座標。也許你應該測試historySize是否真的大於2.此外,你可以添加開始座標的精靈,否則精靈會「跳」到第一個接觸點(但這不是你的問題)。

這是不實際的不同 - 只是另一種可能性:

path= new Path(historySize); 
for (int i = 0; i < historySize; i++) { 
     float x = pSceneTouchEvent.getMotionEvent().getHistoricalX(i); 
     float y = pSceneTouchEvent.getMotionEvent().getHistoricalY(i); 
     path.to(x,y); 
} 

此外,我注意到你int i=1啓動for循環,所以如果你的historySize是2,循環迭代只有一個時代!

編輯

我找不到這個問題,但我發現了一個解決方案:
而不是使用motionEvent history,保存toucheEvent的座標在旅途中爲touchEvent發生:

ArrayList<Float> xCoordinates; // this is where you store all x coordinates of the touchEvents 
ArrayList<Float> yCoordinates; // and here will be the y coordinates. 

onSceneTouchEvent(TouchEvent sceneTouchEvent){ 
    switch(sceneTouchEvent.getAction()){ 
     case (TouchEvent.ACTION_DOWN):{ 
      // init the list every time a new touchDown is registered 
      xCoordinates = new ArrayList<Float>(); 
      yCoordinates = new ArrayList<Float>();    
      break; 
     } 
     case (TouchEvent.ACTION_MOVE): { 
      // while moving, store all touch points in the lists 
      xCoordinates.add(sceneTouchEvent.getX()); 
      yCoordinates.add(sceneTouchEvent.getY()); 
      break; 
     } 
     case (TouchEvent.ACTION_UP): { 
      // when the event is finished, create the path and make the sprite move 
      // instead of the history size use the size of your own lists 

      Path path = new Path(xCoordinates.size()); 
      for (int i = 0; i < xCoordinates.size(); i++) { 
       path.to(xCoordinates.get(i), yCoordinates.get(i)); // add the coordinates to the path one by one 
      } 

      // do the rest and make the sprite move 
      PathModifier pathModifier = new PathModifier(2.5f, path); 
      pathModifier.setAutoUnregisterWhenFinished(true); 
      sprite1.clearEntityModifiers(); 
      sprite1.registerEntityModifier(pathModifier); 
      break; 
     } 
    } 

我在手機上測試了它(它不能在調試模式下運行)並且工作正常。但要確保不會拋出異常,您應該始終測試xCoordinates列表是否大於1.雖然它很可能是。

那麼我希望它可以幫助至少繞過你原來的問題。我注意到一些方法命名不同(例如setAutoUnregisterWhenFinished(true);)我猜你正在使用AndEngine GLES1?我用GLES2,所以當一個方法在我的代碼中的另一個名字,別擔心,只是尋找等值GLES1(因爲,代碼工作我並沒有將其重命名,因爲它是)

  • 克里斯托夫
+0

我幾乎得到了工作,但僅在調試mode.When在調試模式歷史記錄大小是給我10多尺寸,但是當我通常運行它,它只是給我尺寸1.任何想法什麼導致問題。 – voidRy 2013-03-19 07:46:41

+0

剛剛做了一個編輯向你展示我的解決方案,但我無法弄清楚是什麼導致了這個問題 - 我希望你可以在你的程序中適應它 – GameDroids 2013-03-19 13:47:42

+0

我不確定,但如果我在ACTION_MOVE中保持中斷,歷史記錄不會保存任何東西在裏面。所以我總是得到0或1,這是我最初的問題。是的,我正在使用GLES1! :)。 – voidRy 2013-03-20 06:00:48