无法在andEngineGLES2-AC中为精灵设置.setAnchorCenter()和.setRotationCenter()

时间:2015-01-06 15:30:18

标签: android rotation andengine anchorpoint

我有一个精灵,我想继续进行touchevent。应该用一种指向计划运动方向的箭头。该运动应该在Action_Up 9it上工作)。问题在于旋转中心和锚点中心。它应该是这样的:当你触摸精灵时,会出现箭头和蓝色圆圈(之后箭头的数量将取决于手指离精灵显示运动强度的距离)。第一个箭头附加到精灵,另一个箭头附加到第一个,依此类推。因此,当您移动手指时,第一个箭头底部的中心应围绕我的精灵中心旋转。 (我希望它清晰,它与Angy Birds的概念相似,你可以用手指瞄准并设置力量)。问题是我无法设置坐标。例如,旋转中心或锚点中心的设置坐标(50f,50f)使我的箭头围绕远离精灵中心的点移动。我认为将坐标除以32会有所帮助,但不会。甚至像(0.5f,0.5f)这样的坐标也会使我的箭头超过0.5像素。我正在使用andEngine GLES2-AnchorCenter

@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){


    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){

        arrow = new Sprite(0, 0, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow2 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow3 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow4 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        circle = new Sprite(50, 50, 300, 300, ResourceManager.getInstance().mBlueCircleRegion, getVertexBufferObjectManager());

        arrow.setAnchorCenter(25.0f/32, -10.0f/32);
        arrow.setRotationCenter(0, 0);

        arrow.setAlpha(0.4f);
        arrow2.setAlpha(0.6f);
        arrow3.setAlpha(0.8f);
        arrow4.setAlpha(1.0f);
        circle.setAlpha(0.3f);

        this.attachChild(circle);       
        this.attachChild(arrow);
        arrow.attachChild(arrow2);
        arrow2.attachChild(arrow3);
        arrow3.attachChild(arrow4);



    }

    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
        arrow.setRotation(0 + pSceneTouchEvent.getX());


    }

    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
        arrow.detachSelf();
        circle.detachSelf();
        arrow.dispose();
        circle.dispose();

        body.setLinearVelocity(-((pSceneTouchEvent.getX()/32 - body.getPosition().x) * 10), -((pSceneTouchEvent.getY()/32 - body.getPosition().y) * 10));



    }


    return true;
}

修改

使用时有效:

arrow = new Sprite(50, 100, ...)
arrow.setRotationCenter(0.5f, -0.25f);

并且没有设置锚点中心。

为什么我需要X和Y的值低于旋转中心的1f?

2 个答案:

答案 0 :(得分:1)

setRotationCenter通常应在0.0到1.0的范围内,因为它们是对象宽度/高度的一部分,例如:setRotationCenter(0.5f, 0.5f)会使对象围绕其中心旋转。

GLES2-AC中的setRotationCenter与GLES2中的setRotationCenter不同

了解更多信息我建议您关注此链接:

Entity class

在此链接中,您有以下功能:

protected void updateLocalRotationCenter() {
        this.updateLocalRotationCenterX();
        this.updateLocalRotationCenterY();
    }

    protected void updateLocalRotationCenterX() {
        this.mLocalRotationCenterX = this.mRotationCenterX * this.mWidth;
    }

    protected void updateLocalRotationCenterY() {
        this.mLocalRotationCenterY = this.mRotationCenterY * this.mHeight;
    }

如您所见,rotationCenter值(X,Y)乘以实体的宽度和高度,这就是为什么它们应该在0.0-1.0之间

答案 1 :(得分:1)

AnchorCenter和RotationCenter(x,y)值介于0.0f和1.0f之间:

  • 0f 0f表示实体的左下顶点
  • 0,5f 0,5f实体中心(主播中心)
  • 1f 1f for entity's right right vertice
  • 等...

所以如果你想要实现像愤怒的小鸟一样的东西,我会将我的鸟锚中心设置为1f 0.5f,然后对身体应用一些旋转逻辑。我认为它会起作用。

相关问题