3
\$\begingroup\$

I have set up a DigitalOnScreenControl in my game but the knob does not move. The base and the knob are drawn on the screen but the latter doesn't move as per the AndEngine example here.

Here is a snippet of my code:

this.mDigitalOnScreenControl = new DigitalOnScreenControl(300, 300, ResourceManager.getCamera(), ResourceManager.joystickControlBase,
                ResourceManager.joystickControlKnob, 0.1f, ResourceManager.getActivity().getVertexBufferObjectManager(),
                new IOnScreenControlListener() {

            @Override
            public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX,
                    final float pValueY) {
                    Log.v(TAG, "onControlChange");
            }
        });

        final Sprite controlBase = this.mDigitalOnScreenControl.getControlBase();
        final Sprite knob = this.mDigitalOnScreenControl.getControlKnob();
        controlBase.setAlpha(0.5f);
        controlBase.setOffsetCenter(0, 0);
        controlBase.setScaleCenter(10, 10);
                    controlBase.setScale(1.25f);
        knob.setScale(1.25f);
        this.mDigitalOnScreenControl.refreshControlKnobPosition();
        ResourceManager.getEngine().getScene().attachChild(this.mDigitalOnScreenControl);

The mDigitalOnScreenControl is a DigitalOnScreenControl object defined previously. I use a ResourceManager class to obtain texture regions and other game resources such as the camera amongst others. Note: Since the base and the knob are drawn, the ITextureRegions returned by the ResourceManager are valid. When I execute the program, there are no errors in DDMS.

The knob just doesn't move. Does anybody have an idea?

Thank you!

\$\endgroup\$
10
  • \$\begingroup\$ Do you ever call refreshControlKnobPosition() in your update code? Is the knob logging onControlChange? \$\endgroup\$
    – House
    Feb 25, 2013 at 19:12
  • \$\begingroup\$ 1) If I put a Log.v(TAG, "msg") inside the onControlChange(), I get the verbose log at every screen update. This means that onControlChange() is being called. 2) To which update code are you referring? Thank you for your support. \$\endgroup\$
    – Shailen
    Feb 25, 2013 at 20:06
  • \$\begingroup\$ Your main loop, or update loop. I'm not sure if you're required to update the knob position or it's done automatically. \$\endgroup\$
    – House
    Feb 25, 2013 at 20:10
  • \$\begingroup\$ I proofread my code. The whole code snippet posted (and therefore the refreshControlKnobPosition() function) is run in the main loop (onLoadScene()). This bug is very strange. \$\endgroup\$
    – Shailen
    Feb 25, 2013 at 20:22
  • 1
    \$\begingroup\$ @shailenTJ It's actually pretty sensible as a child scene. The on screen controls relies on having its own relative coordinate system, so it knows how far you're pushing the knob from a fixed point, regardless of the outside scene coordinates. I think you should consider posting an answer to your question and accepting it. \$\endgroup\$ Feb 26, 2013 at 22:29

1 Answer 1

1
\$\begingroup\$

The DigitalOnScreenControl cannot be attached as a child to a scene. It must be attached as a childscene to the Engine's current Scene.

ResourceManager.getInstance().engine.getScene().setChildScene(joystickControl);

In the above code, joystickControl extends a DigitalOnScreenControl object. After that, depending on how your game is organised, you may need to sort the scenes, in case the DigitalOnScreenControl does not appear.

ResourceManager.getInstance().engine.getScene().sortChildren();

Also, read Cameron Fredman's comment why it is sensible to add the DigitalOnScreenControl object as a childscene.

\$\endgroup\$
1
  • \$\begingroup\$ Almost done. Now click the check mark next to your answer! :) \$\endgroup\$ Feb 28, 2013 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .