coco2d-x v3.x run specific code when touch screen is pressed is not released

43 Views Asked by At

I am developing a mobile game using coco2d-x framework with C++ language. The platforms I target are IOS and Android.

I want to run some code when an user is pressing a sprite as long as he doesn't release the press without moving the finger. I went through the documentations and looked at the different callbacks (onTouchBegan, onTouchMoved, onTouchEnded) but couldn't find a way to solve my problem.

In fact i have sprites drawn on the screen to simulate directional controller. I want to move a character as long as the user is pressing on a directional sprite.

Is there a way to run some code as long as a sprite is pressed ?

1

There are 1 best solutions below

1
On BEST ANSWER

I would try something like this

onTouchBegan(...){
    //let player move
    //e.g. set some flag moving=true;
}
onTouchMoved(...){
   //query x and y distance of finger movement
   //cancel player movement if finger moved above a certain treshold
   //or alternatively check if the finger is now 
   //outside of the bounding box of your sprite
   //if so then
      //moving=false;
}
onTouchEnded(...){
    //stop movement if not already stopped before
    //moving=false;
}

in your update function you can then simply check moving and execute your code as long as moving is true. Is that what you meant?