Catching Long Click:
|
In this tutorial, I will present easy way how to catch long clicks (touch event) We can do it easily, because AndEngine have such feature built in.
Long click might be extremely useful while developing User Interfaces. Follow code example posted below to understand how can we easily implement it on your scene. |
- First of all, we need to implement IHoldDetectorListener interface. You will be forced by compiler, to add 3 unimplemented methods:
@Override public void onHoldStarted(HoldDetector pHoldDetector, int pPointerID, float pHoldX, float pHoldY) { } @Override public void onHold(HoldDetector pHoldDetector, long pHoldTimeMilliseconds, int pPointerID, float pHoldX, float pHoldY) { } @Override public void onHoldFinished(HoldDetector pHoldDetector, long pHoldTimeMilliseconds, int pPointerID, float pHoldX, float pHoldY) { }
- Create new field for ContinousHoldDetector object:
private ContinuousHoldDetector continuousHoldDetector;
- Initialize this object, and register it to your scene as a Update Handler (register it to the scene, where you want long click to be detected)
Please note, that we use default constructor in this example, which means that default value will be used for long click (time needed to pass, while holding your finger, before it will be considered as a long click) If you want to customize it, by using your own value, you can use second constructor provided in the ContinousHoldDetector class. For purposes of this article, we will use default constructor.
continuousHoldDetector = new ContinuousHoldDetector(this); yourScene.registerUpdateHandler(continuousHoldDetector);
Now, when implementing long click is done, you can put your code which should be executed in certain conditions:
- onHoldStarted() - put your code, which should be executed if player held his finger long enough.
- onHoldFinished() - put your code, which should be executed, if there was long click, and user released his finger.
HTML Comment Box is loading comments...