Jump to content

Engines using the 'Simple JavaScript Inheritance' implementation.


tyson
 Share

Recommended Posts

Hey all,   I'm invested using Cocos2-HTML5 and while it's a great engine, I wondering on how to port my code to another engine some day.

 

Are there many other engines using John Resig's implementation? http://ejohn.org/blog/simple-javascript-inheritance/

 

For example, a simple touchable sprite:

var PirateSprite = cc.Sprite.extend({    ctor: function() {       this.initWithFile("images/pirate.png");    },    init : function() {        this._super();    },    onEnter : function() {        this._super();        // add touch listener        cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 1, true);    },    onExit : function() {        this._super();        // remove touch listener        cc.Director.getInstance().getTouchDispatcher().removeDelegate(this, 1, true);    },    onTouchBegan : function(touches, event) {       if (this.containsTouchLocation(touches)) {           GameUtils.explodeFireworksAt(cc.Rect.CCRectGetMidX(this.getBoundingBox()), cc.Rect.CCRectGetMidY(this.getBoundingBox()));           this.removeFromParent(true);       }    },    containsTouchLocation : function(touch) {         var p = this.getParent().convertTouchToNodeSpace(touch);        return cc.Rect.CCRectContainsPoint(this.getBoundingBox(), p);    }});

Thanks!

 

Tyson

Link to comment
Share on other sites

Why not use prototype for inheritance ? It's really simple.

For the touch events you could create your own "input manager" that checks when a touch event is triggered. After that check if the x and y coords are inside the bounding box of the element, if so then fire a custom event for that element.

Link to comment
Share on other sites

There are multiple engines using this type of syntax, though it is more common for ones to have a function extend(parent, fields). However, you can even save yourself from changing these little bits of code by adding a extend() method into the Function prototype itself:

Function.prototype.extend = function extend(fields) {	function inherit() {};	inherit.prototype = this;	var proto = new inherit();	for (var name in fields) proto[name] = fields[name];	return proto;}
To be executed before everything else.

Implementation courtesy of Haxe JavaScript generator.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...