Using the API provided by Ngui, you want to use the push button to control character continuous walking:
But found that when the forward button is clicked, the character simply moves a small step in the direction of control and, for a long time, presses the Forward button, the character ignores the Click event:
This is a strange feeling that the character moves after a short push button. Then there must be a button event to be able to sense the press event. The UI Button Message (Script) was found in Ngui/interrection, where it was found to mount the press function:
The successful mount function, and then walk, the expected result does not appear, the implementation effect and the same as the previous onclick, the role of moving forward in a small step, just the timing of the implementation of the different, onpress is the button after the release button to trigger, and the onclick is actually clicked Button and released before the trigger:
And the result I want is: Hold the button, the character continues to walk, release the button, the character stops moving.
After a supper, I thought of a solution: since the timing of the onpress and onclick triggering is different, why not use this gap to achieve the goal?
The implementation method is as follows:
1. Set a flag variable in the script that controls the role to identify whether the walk button is pressed, such as bool ismove=false;2. Set the value of Ismove to True in the onpress () function, in the onclick () The Ismove value is set to False;3 in the function. Ismove is judged in the update () function, if the Ismove value is true, the corresponding mobile code is executed;
Because the update () function refreshes every frame, you can use the time difference between pressing and releasing the button to control the character movement.
The onclick () event is replaced with the Onrelease event because the long press button will cause the OnClick () event to fail to respond:
This way, no matter how long the button is pressed, the release can respond to the Onrelease event, which tends to improve.
I do not know what is the easy way, hope to share with the Garden friends:)
NGUI by pressing button to control character continuous walking