Use prototype inheritance to solve the mouse problems in Game Development

Source: Internet
Author: User

Author: Flash knife prodigal son www.iflashigame.com http://hi.baidu.com/mr_ziqiang
Reprinted please indicate the source

Today, when playing games, the game has a simple requirement:

There is a character. When you move the mouse over it, you need to display the hand pointer.
If (mouseevent. mouse_over) mouse. cursor = "button ";

However, after a player clicks the attack key, it needs to hide the mouse and replace the mouse with a sight MC. At this time, when the sight is moved to the body of a person, no manual pointer is displayed.
Mouse. Hide ();
Mymouse. Visible = true;
Mymouse. enterframe -- follow the mouse

Now the problem arises. Because mouse. cursor = "button" is encapsulated in the category class, we need to let him know that the mouse has been hidden and no more hand-shaped display is needed. It is definitely not good if the cursor class is in the mouse state outside by destroying the encapsulation. The mouse class does not have any attribute to hide or display the mouse.

What should I do ???

At this time, we can usePrototype inheritance(What is prototype inheritance)

For this problem, we only need to be able to record whether the mouse is hidden to solve the problem, in order not to destroy the encapsulation. We use the mouse class as our own static class.

First, add mouse. Prototype. ishide = true when executing mouse. Hide;
When executing the mouse. Show () command, add mouse. Prototype. ishide = false;

Then, add a condition judgment to the Mouseover listening event of the character.
If (! Mouse. Prototype. ishide)
Mouse. cursor = "button"

The problem is solved.

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.