views:

142

answers:

1

I am using the Android onTouchEvent to register touch and the associated movement. I then want to iterate a sprite along that path.

I need to store the path traced by the finger for use later on.

The problem is that after the finger is lifted off the screen the ArrayList (pArray) is completely populated with the X & Y position of the ACTION_UP coordinates.

From the Log i can see that it is adding the current X & Y position to the Arraylist while the finger is moving which is great but then the Arraylist gets overwritten by the data in Arraylist.size() - 1...

Can someone please offer a suggestion.

public boolean onTouchEvent(MotionEvent event){

synchronized (mHeartbeat.getSurfaceHolder()){

 getXX = event.getX();
 getYY = event.getY();

 pathArr.setX(getXX);
 pathArr.setY(getYY);

 pArray.add(pathArr);


if(event.getAction() == MotionEvent.ACTION_DOWN){

 }else if(event.getAction() == MotionEvent.ACTION_MOVE){

 }else if(event.getAction() == MotionEvent.ACTION_UP){


 }

  return true;
 }
}
A: 

It's because you are not creating new pathArr variables and are adding the same one to the array each time. It also appears to be an instance field. You need to change it to a local variable and create a new one for each event.

Qberticus
Thank you, it works. Still a little confused as i would be overwriting the content of pathArr each time and then copying into the array and wouldn't creating a new pathArr just eat memory?
Matt