答案 0 :(得分:3)
您需要覆盖Sprite的onAreaTouched方法,如下所示。您可以从pSceneTouchEvent,pTouchAreasLocalX和pTouchAreaLocalY变量获取有关触摸事件的信息,并使用它们来确定移动球的方式。
您不希望对OnAreaTouched方法内部的物理主体应用任何力,但因为应使用更新处理程序对物理主体进行更改。我建议让onAreaTouched方法设置一个标志和一些其他变量,以便下次更新处理程序运行时它可以使用这些值。
更新:我添加了一些代码来帮助您找出方向。 if语句中的注释应该解释它们何时被调用。基本上,您获得初始触摸位置(向下操作),计算您移动到的位置(动作移动)并使用方向在更新处理程序中应用力(操作向上)。
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() {
}
答案 1 :(得分:0)
jteezy的答案还不够,因为你也需要处理Sprite中的touchevents。要首先处理场景中的所有触摸事件,您的类必须实现IOnSceneTouchListener。然后你应该覆盖公共布尔onSceneTouchEvent(Scene pScene,最终的TouchEvent pSceneTouchEvent)。然后在场景中调用并调用该方法yourscene.setOnSceneTouchListener(this);
以下是代码:
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;
}
编辑:我忘了说你可以增加你存储的touchevents的数量,但最好这样做,让玩家在有限的时间内滑动。 你也可以用更高的值乘以velX和velY来加快速度。