1
\$\begingroup\$

when I look at the andengine examples I saw an example about rotate a sprite with joystick. Here is the code;

    final float y2 = (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) ? 0 : y1;
    final float x2 = CAMERA_WIDTH - this.mOnScreenControlBaseTextureRegion.getWidth();
    final float x1 = 0;
    final float y1 = CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight();
    final AnalogOnScreenControl rotationOnScreenControl = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
        @Override
        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
            if(pValueX == x1 && pValueY == x1) {
                face.setRotation(x1);
            } else {
                face.setRotation(MathUtils.radToDeg((float)Math.atan2(pValueX, -pValueY)));
            }
        }

        @Override
        public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
            /* Nothing. */
        }
    });

That is okay for joystick BUT I want to implement this without joystick.. I mean when user drag/slip the screen with finger than sprite will also rotate according to finger movement. By the way I only want to rotate NOT move.. And here is code that I try to;

mScene.setOnAreaTouchListener(new IOnAreaTouchListener() {

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                ITouchArea pTouchArea,final float pTouchAreaLocalX,
                final float pTouchAreaLocalY) {
            if(pTouchAreaLocalX == x1 && pTouchAreaLocalY == x1) {
                face.setRotation(x1);
            } else {
                face.setRotation(MathUtils.radToDeg((float)Math.atan2(pSceneTouchEvent.getX(), -pSceneTouchEvent.getY())));
            }
            return false;
        }
    });

and my code didn't work.. Is there any suggestion ? Thanks...

\$\endgroup\$
  • \$\begingroup\$ Can you check if you are able to detect touch events , by adding some log statements of System.out prints inside the onAreaTouched. \$\endgroup\$ – Sandeep R Aug 3 '16 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.