问题
I'm having a ball sprite on screen,when i touch and swipe on that sprite,It must move in particular direction of swipe.
I have added a physics to that ball.
I wanted to do some thing like paper toss
Can any one help me out. Thanks in advance.
回答1:
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.
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.
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).
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() {
}
回答2:
jteezy's answer isn't enough because you need to handle touchevents out of the Sprite too. To handle all touch events on scene first your class must implement IOnSceneTouchListener. Then you should override public boolean onSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent). Then call and call that method on your scene yourscene.setOnSceneTouchListener( this );
Here is the code:
public class GameManager implements IOnSceneTouchListener{
private boolean mSelected = false;
private boolean mThrown = false;
private float mX;
private float mY;
private float velX;
private float velY;
private TouchEvent mTouchEvent[] = {null, null, null, null, null};
callThatMethodSomeWhere(){
mScene.setOnSceneTouchListener( this );
mİşaretçiSprite = new Sprite( 0, 0, mİşaretçiTextureRegion, mEngine.getVertexBufferObjectManager() );
}
@Override
public boolean onSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent)
{
if( pSceneTouchEvent.isActionDown() && mİşaretçiSprite.contains( pSceneTouchEvent.getX() , pSceneTouchEvent.getY())){ //This part is so important that you check if the touch w
mX = pSceneTouchEvent.getX();
mY = pSceneTouchEvent.getY();
mSelected = true;
}
else if( pSceneTouchEvent.isActionMove() && mSelected ){
if( mTouchEvent[0] == null ){
mTouchEvent[0] = pSceneTouchEvent;
}else if( mTouchEvent[1] == null ){
mTouchEvent[1] = pSceneTouchEvent;
}else if( mTouchEvent[2] == null ){
mTouchEvent[2] = pSceneTouchEvent;
}else if( mTouchEvent[3] == null ){
mTouchEvent[3] = pSceneTouchEvent;
}else if( mTouchEvent[4] == null ){
mTouchEvent[4] = pSceneTouchEvent;
}else{
velX = mTouchEvent[4].getX() - mX;
velY = mTouchEvent[4].getY() - mY;
mThrown = true;
mSelected = false;
Body.applyForceToCenter( velX*10, velY*10);
}
}
else if ( pSceneTouchEvent.isActionUp() && mSelected ){
velX = pSceneTouchEvent.getX() - mX;
velY = pSceneTouchEvent.getY() - mY;
mBody.applyForceToCenter( velX*10, velY*10);
mThrown = true;
}
return false;
}
Edit: I forgot to say that you can increase the number of touchevents you store but it is better to do it like that to give the player to swipe in a limited time. also you can multiply velX and velY with higher values to throw it faster.
来源:https://stackoverflow.com/questions/20540858/how-to-throw-fling-the-ball-by-swiping-on-it-using-andengine