Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to override the onAreaTouched method of Sprite as shown below. You can get the information on the touch event from your pSceneTouchEvent, pTouchAreasLocalX, and pTouchAreaLocalY variables and use them to determine which way to move the ball. </p> <p>You don't want to apply any forces to the physics body inside of the OnAreaTouched method though because changes to physics bodies should be made using an update handler. I would suggest having the onAreaTouched method set a flag and some other variables so that the next time the update handler runs it can use those values.</p> <p>Update: I added some code to help you figure out the direction. The comments inside the if statements should explain when they are called. Basically, you get the initial touch position (action down), calculate where you moved to (action move) and use the direction to apply the force in the update handler (action up).</p> <pre><code>mSprite = new Sprite(x, y ,mRegion, mEngine.getVertexBufferObjectManager()){ @Override public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { //set flag member variable that sprite has been touched if (pSceneTouchEvent.isActionDown()) { //here is where the touch was initiated so you //can store the x,y location. You obtain it by using pSceneTouchEvent.getX() // and pSceneTouchEvent.getY() } if (pSceneTouchEvent.isActionMove()) { //This will be called when you slide your finger, so you //can get the new coordinates by again using pSceneTouchEvent.getX() // and pSceneTouchEvent.getY() } if (sSceneTouchEvent.isActionUp()) { //this will be called when you release the sprite // and tell the update handler to apply the force } } }; this.registerUpdateHandler(new IUpdateHandler(){ @Override public void onUpdate(float pSecondsElapsed) { //if flag is set apply a force to the physics body //set flag to false to wait for next touch event } @Override public void reset() { } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload