1


我有石头精灵类,我在其中定义了触摸、拖动和投掷动作。当我在屏幕上触摸并拖动它时,我的石头精灵移动得很好。我的期望是精灵应该只在有限的圆形区域(可见/不可见)内移动。
这是我下面的代码

@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
    if (pSceneTouchEvent.isActionMove() || pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionOutside())
    {

        this.setX(pSceneTouchEvent.getX() - this.getWidth() / 2);
        this.setY(pSceneTouchEvent.getY() - this.getHeight() / 2);

    }
    else if (pSceneTouchEvent.isActionUp())
    {
        mPhysicsHandler.setVelocityX((originX - getX()) * 5);
        mPhysicsHandler.setVelocityY((originY - getY()) * 5);
        mPhysicsHandler.setAccelerationY(100);  
    }
    return true;
}


请帮忙。

4

1 回答 1

1

在拖动时应用移动之前,请检查触摸事件是否落在圆圈内。为此,请测量距圆心的距离。如果距离小于圆的半径,则移动对象。如果没有,什么也不做。@Override public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { if (pSceneTouchEvent.isActionMove() || pSceneTouchEvent.isActionDown() ||

pSceneTouchEvent.isActionOutside())
    {
        // Use Pythagorean theorem to get distance between 2 points;
        // center is a point you declare
        // radius is a value you declare that is the radius of the circular area
        Float distance = (pSceneTouchEvent.getX() - center.x)*(pSceneTouchEvent.getX() - center.x) + (pSceneTouchEvent.getY() - center.y) * (pSceneTouchEvent.getY() - center.y);
        distance = Math.sqr(distance);
        if(distance < radius){
             // Only move if touch event is inside the circle area    
             this.setX(pSceneTouchEvent.getX() - this.getWidth() / 2);
             this.setY(pSceneTouchEvent.getY() - this.getHeight() / 2);
        }

    }
    else if (pSceneTouchEvent.isActionUp())
    {
        mPhysicsHandler.setVelocityX((originX - getX()) * 5);
        mPhysicsHandler.setVelocityY((originY - getY()) * 5);
        mPhysicsHandler.setAccelerationY(100);  
    }
    return true;
}
于 2013-10-14T20:34:57.167 回答