NGUI by pressing button to control character continuous walking

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.