[Cocos2d-x from c ++ to js] 04: inheritance in cocos2d-x for js

Source: Internet
Author: User

For object-oriented languages, the inheritance mechanism is the basis for code reuse. Unfortunately, javascript is a prototype-based inherited language, class inheritance is not directly supported at the language level.

However, the js language is very expressive. Therefore, JavaScript users generally design their own inheritance mechanism. This mechanism must include several points, simulating private access permissions, and implementing different attributes and class attributes, support for method override and access to methods overwritten by the parent class.

Cocos2d-x, integrated two sets of Inheritance Mechanism, see the "MoonWarriors" Example of the source code SysMenu. js File

 
 
  1. var SysMenu = cc.Layer.extend({ 
  2.     _ship:null, 
  3.  
  4.     ctor:function () { 
  5.         cc.associateWithNative( this, cc.Layer ); 
  6.     }, 
  7.     init:function () { 
  8.         var bRet = false; 
  9.         if (this._super()) { 
  10.             winSize = cc.Director.getInstance().getWinSize(); 
  11.             var sp = cc.Sprite.create(s_loading); 
  12.             sp.setAnchorPoint(cc.p(0,0)); 
  13.             this.addChild(sp, 0, 1); 
  14.  
  15.             var logo = cc.Sprite.create(s_logo); 
  16.             logo.setAnchorPoint(cc.p(0, 0)); 
  17.             logo.setPosition(0, 250); 
  18.             this.addChild(logo, 10, 1); 
  19.  
  20.             var newGameNormal = cc.Sprite.create(s_menu, cc.rect(0, 0, 126, 33)); 
  21.             var newGameSelected = cc.Sprite.create(s_menu, cc.rect(0, 33, 126, 33)); 
  22.             var newGameDisabled = cc.Sprite.create(s_menu, cc.rect(0, 33 * 2, 126, 33)); 
  23.  
  24.             var gameSettingsNormal = cc.Sprite.create(s_menu, cc.rect(126, 0, 126, 33)); 
  25.             var gameSettingsSelected = cc.Sprite.create(s_menu, cc.rect(126, 33, 126, 33)); 
  26.             var gameSettingsDisabled = cc.Sprite.create(s_menu, cc.rect(126, 33 * 2, 126, 33)); 
  27.  
  28.             var aboutNormal = cc.Sprite.create(s_menu, cc.rect(252, 0, 126, 33)); 
  29.             var aboutSelected = cc.Sprite.create(s_menu, cc.rect(252, 33, 126, 33)); 
  30.             var aboutDisabled = cc.Sprite.create(s_menu, cc.rect(252, 33 * 2, 126, 33)); 
  31.  
  32.             var newGame = cc.MenuItemSprite.create(newGameNormal, newGameSelected, newGameDisabled, function () { 
  33.                 this.onButtonEffect(); 
  34.                 flareEffect(this, this, this.onNewGame); 
  35.             }.bind(this)); 
  36.             var gameSettings = cc.MenuItemSprite.create(gameSettingsNormal, gameSettingsSelected, gameSettingsDisabled, this.onSettings, this); 
  37.             var about = cc.MenuItemSprite.create(aboutNormal, aboutSelected, aboutDisabled, this.onAbout, this); 
  38.  
  39.             var menu = cc.Menu.create(newGame, gameSettings, about); 
  40.             menu.alignItemsVerticallyWithPadding(10); 
  41.             this.addChild(menu, 1, 2); 
  42.             menu.setPosition(winSize.width / 2, winSize.height / 2 - 80); 
  43.             this.schedule(this.update, 0.1); 
  44.  
  45.             var tmp = cc.TextureCache.getInstance().addImage(s_ship01); 
  46.             this._ship = cc.Sprite.createWithTexture(tmp,cc.rect(0, 45, 60, 38)); 
  47.             this.addChild(this._ship, 0, 4); 
  48.             var pos = cc.p(Math.random() * winSize.width, 0); 
  49.             this._ship.setPosition( pos ); 
  50.             this._ship.runAction(cc.MoveBy.create(2, cc.p(Math.random() * winSize.width, pos.y + winSize.height + 100))); 
  51.  
  52.             if (MW.SOUND) { 
  53.                 cc.AudioEngine.getInstance().setMusicVolume(0.7); 
  54.                 cc.AudioEngine.getInstance().playMusic(s_mainMainMusic, true); 
  55.             } 
  56.  
  57.             bRet = true; 
  58.         } 
  59.         return bRet; 
  60.     }, 
  61.     onNewGame:function (pSender) { 
  62.         var scene = cc.Scene.create(); 
  63.         scene.addChild(GameLayer.create()); 
  64.         scene.addChild(GameControlMenu.create()); 
  65.         cc.Director.getInstance().replaceScene(cc.TransitionFade.create(1.2, scene)); 
  66.     }, 
  67.     onSettings:function (pSender) { 
  68.         this.onButtonEffect(); 
  69.         var scene = cc.Scene.create(); 
  70.         scene.addChild(SettingsLayer.create()); 
  71.         cc.Director.getInstance().replaceScene(cc.TransitionFade.create(1.2, scene)); 
  72.     }, 
  73.     onAbout:function (pSender) { 
  74.         this.onButtonEffect(); 
  75.         var scene = cc.Scene.create(); 
  76.         scene.addChild(AboutLayer.create()); 
  77.         cc.Director.getInstance().replaceScene(cc.TransitionFade.create(1.2, scene)); 
  78.     }, 
  79.     update:function () { 
  80.         if (this._ship.getPosition().y > 480) { 
  81.             var pos = cc.p(Math.random() * winSize.width, 10); 
  82.             this._ship.setPosition( pos ); 
  83.             this._ship.runAction( cc.MoveBy.create( 
  84.                 parseInt(5 * Math.random(), 10), 
  85.                 cc.p(Math.random() * winSize.width, pos.y + 480))); 
  86.         } 
  87.     }, 
  88.     onButtonEffect:function(){ 
  89.         if (MW.SOUND) { 
  90.             var s = cc.AudioEngine.getInstance().playEffect(s_buttonEffect); 
  91.         } 
  92.     } 
  93. }); 
  94.  
  95. SysMenu.create = function () { 
  96.     var sg = new SysMenu(); 
  97.     if (sg && sg.init()) { 
  98.         return sg; 
  99.     } 
  100.     return null; 
  101. }; 
  102.  
  103. SysMenu.scene = function () { 
  104.     var scene = cc.Scene.create(); 
  105.     var layer = SysMenu.create(); 
  106.     scene.addChild(layer); 
  107.     return scene; 
  108. }; 

This extend inheritance method was created by John Resig. John Resig is a great player in the JS field. It is very interesting to have many fans compile paragraphs for him on the Internet.

The example uses the parent class cc. layer. extend method to start inheritance. Input an object literal {}, which can contain object attributes and object methods. Finally, extend binds the interface, returns a constructor assigned to SysMenu.

For Class methods, that is, static methods in the general sense), the most traditional method of js is used. You can directly specify attributes for the constructor.

This method of writing code is very simple and elegant. More importantly, this writing method is very compatible with the typographical aesthetics of C ++ or java programmers.

Understanding of inheritance. The prototype inheritance in js is different from the class-based inheritance method. Internally, a prototype chain is maintained, and nodes on the chain are linked. Note: No value assignment, is not a copy ). Let's take a look at how the book "authoritative guide" talks about it. Unfortunately, it's not a good illustration of prototype inheritance ...... Never implement algebraic replacement or rote memorization, so you can hardly grasp the essence of prototype chain.

In addition, we strongly recommend a series of articles on sanshenshi, "JavaScript inheritance details".

JavaScript inheritance

JavaScript inheritance (2)

Details on JavaScript inheritance 3)

JavaScript inheritance 4)

JavaScript inheritance 5)

Details on JavaScript inheritance 6)

If you have time, I will configure the article on Sansheng stone with a detailed prototype Link Description diagram, so that you can easily master the prototype chain of js.

This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/1125167

Related Article

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.