0

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 ?

Jewelz973
  • 3
  • 2

1 Answers1

0

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?

  • thank you. this is a (the ?) way of solving it :) . I even wonder how come I didn't think about it first. – Jewelz973 Apr 13 '15 at 07:23