[IPhone] observer mode and Game Development

Source: Internet
Author: User
Introduction In March, I received an iPhone game project development. For this game, I learned the observer mode and used it in my game background in my game, the gamescene I designed serves as the upper-layer class of the game level. Background
In my games, the gamescene I designed serves as the upper level of the game level. It inherits observable and uses gamerole to practice game roles, such as player and NPC. Gamerole implements the observer and uses the observer mode to update the role in the level.

CodeAs follows:

1. Class observable;
2. Class observer: Public nsobject
3 .{
4. Private:
5. // --- add private member ---//
6. Public:
7. // --- add public member ---//
8. Observer ();
9. Virtual ~ Observer ();
10. Virtual void Update () = 0;
11 .};
12. typedef set <Observer *> set;
13. Class observable: Public nsobject
14 .{
15. Private:
16. // --- add private member ---//
17. Set m_observers;
18. Public:
19. // --- add public member ---//
20. observable ();
21. Virtual ~ Observable ();
22. Void registerobserver (Observer * O );
23. Void removeobserver (Observer * O );
24. Void policyobservers ();
25 .};
26. Void observable: registerobserver (Observer * O)
27 .{
28. m_observers.insert (O );
29 .}
30. Void observable: removeobserver (Observer * O)
31 .{
32. m_observers.erase (O );
33 .}
34. Void observable: policyobservers ()
35 .{
36. Set: reverse_iterator index;
37. Set: reverse_iterator end = m_observers.rend ();
38. For (Index = m_observers.rbegin (); index! = End; index ++)
39 .{
40. (Observer *) (* index)-> Update ();
41 .}
42 .}

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.