2013-03-18 81 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