mapacarta Posted May 24, 2018 Share Posted May 24, 2018 I am trying to inherit Phaser classes, here is what I have done so far: class Obj extends Phaser.GameObjects.Image { constructor (scene, x, y) { super(scene, x, y, 'object'); scene.add.displayList.add(this); } } Is this a proper way to inherit a class? Also is it possible to use prototypal inheritance in phaser 3 like we do in phaser 2? Link to comment Share on other sites More sharing options...
rich Posted May 24, 2018 Share Posted May 24, 2018 Here is how to do it using ES5: http://labs.phaser.io/view.html?src=src\game objects\images\custom image class.js and here is how to do it using ES6: http://labs.phaser.io/view.html?src=src\game objects\images\custom image class ES6.js mapacarta and microspace 2 Link to comment Share on other sites More sharing options...
mapacarta Posted May 25, 2018 Author Share Posted May 25, 2018 Thanks Link to comment Share on other sites More sharing options...
mapacarta Posted May 25, 2018 Author Share Posted May 25, 2018 (edited) How about arcade sprites? When I extends physics.sprite the object doesn't get a body, acts like a normal sprite. I tried to add it to the physics world like this: this.physics.world.enableBody(robot,CONST.DYNAMIC_BODY); it gives an error of course since CONST is not defined and I couldn't find what CONST.DYNAMIC_BODY equals to. "dynamic" or "dynamicBody" maybe -EDIT- Ok I found it CONST.DYNAMIC_BODY stands for 0 When I do this.physics.world.enableBody(robot,0); it gets a body. -ANOTHER EDIT- the object also didn't added to the updateList. I don't know what updateList does but do we need to manually add it to the updateList too? this.sys.updateList.add(robot); Edited May 25, 2018 by mapacarta Link to comment Share on other sites More sharing options...
mapacarta Posted October 25, 2018 Author Share Posted October 25, 2018 I still couldn't find how to add a custom arcade sprite properly I am just using some workarounds and stuffs like that. Here is what I am currently using: var customObject = new Phaser.Class({ Extends: Phaser.Physics.Arcade.Sprite, initialize: function customObject (scene, x, y) { Phaser.Physics.Arcade.Sprite.call(this, scene); scene.physics.world.enableBody(this,0); scene.sys.updateList.add(this); this.setTexture('object'); this.setPosition(x, y); } }); Is this the right way to add inheritence or is there any better way? Link to comment Share on other sites More sharing options...
GreenCloversGuy Posted October 25, 2018 Share Posted October 25, 2018 Honestly, I would use ES6. However if we do it your way, you can save yourself quite a bit of hassle by using the whole of the parent constructor. I think (not sure) that it's common policy to add some code so that your sprite is auto-loaded into the scene when constructed. var CustomObject = new Phaser.Class({ Extends: Phaser.Physics.Arcade.Sprite, initialize: function CustomObject (scene, x, y) { Phaser.GameObjects.Sprite.call(this, scene, x, y, 'object'); scene.physics.add.existing(this); } }); I would also use the GameObject version of Sprite rather than the Physics.Arcade version, but I don't actually know the difference between the two! mapacarta 1 Link to comment Share on other sites More sharing options...
samme Posted October 25, 2018 Share Posted October 25, 2018 Extend Arcade.Sprite mapacarta 1 Link to comment Share on other sites More sharing options...
mapacarta Posted October 26, 2018 Author Share Posted October 26, 2018 Thanks, it helped a lot. Link to comment Share on other sites More sharing options...
Recommended Posts