tyson Posted September 25, 2013 Share Posted September 25, 2013 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 Quote Link to comment Share on other sites More sharing options...
xerver Posted September 25, 2013 Share Posted September 25, 2013 I used it for a while, but found it was difficult to integrate other libraries into it. Using ECMAScript 5 `Object.defineProperty` and `Object.create` worked out much better for me. Quote Link to comment Share on other sites More sharing options...
rich Posted September 25, 2013 Share Posted September 25, 2013 That style of syntax sends shivers down my spine Quote Link to comment Share on other sites More sharing options...
Quetzacotl Posted September 25, 2013 Share Posted September 25, 2013 That style of syntax sends shivers down my spine what flaws this syntax has? IMHO it's ok, easy to maintain and read. I know immediately what is going on in this code. Quote Link to comment Share on other sites More sharing options...
Felipe Posted September 25, 2013 Share Posted September 25, 2013 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. Quote Link to comment Share on other sites More sharing options...
Quetzacotl Posted September 25, 2013 Share Posted September 25, 2013 it does use prototype for inheritance underneath Quote Link to comment Share on other sites More sharing options...
Felipe Posted September 25, 2013 Share Posted September 25, 2013 I know, but I was saying that for if he wants to port his code to his own engine or other that doesn't use that inheritance system underneath. Quote Link to comment Share on other sites More sharing options...
PixelPicoSean Posted September 26, 2013 Share Posted September 26, 2013 There are some engines using that class system, such as Joy.js and impact.js, the syntax I think is very clear and straight forward. Especially using a module system like impact.js, it'll be much better. I dont think it's hard to port even to pure prototype inheritance. Quote Link to comment Share on other sites More sharing options...
YellowAfterlife Posted September 27, 2013 Share Posted September 27, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.