plicatibu Posted March 6, 2014 Share Posted March 6, 2014 Hi. I know this question has been made a lot ( and I read all I found out on this forum ) but I still have problems with that (I'm learning JavaScript at the same time I'm learning Phaser). I'm trying to port a class I've made in Haxe into JavaScript to use with Phaser. Basically, the class has 2 images and they are changed according to function calls (either showValue() and hiddeValue()). I mean, just one image is show at a time. In Haxe/ Actionscript it's just a matter of embed a sprite (called holder) and switch images visibility. You can see how simple is the code below:class Tile extends Sprite { public var type:Int; private var holder:Sprite; private static var images:Array < String > = [ "image/image0.png", "image/image1.png", "image/image2.png", "image/image3.png", "image/image4.png", "image/image5.png", "image/image6.png", "image/image7.png", "image/image8.png", "image/image9.png", "image/imagep.png", "image/imagea.png", "image/imageb.png" ]; public function new () { super (); this.type = type; holder = new Sprite(); var img:Bitmap = new Bitmap (Assets.getBitmapData (Setup.IMG_PLACE)); img.smoothing = true; holder.addChild(img); img = new Bitmap (Assets.getBitmapData (images[type])); img.smoothing = true; holder.addChild(img); hiddeValue(); addChild (holder); filters = [ new BlurFilter (0, 0) ]; mouseChildren = false; buttonMode = false; graphics.beginFill (0x000000, 0); graphics.drawRect (-5, -5, 66, 66); } public function showValue():Void { holder.getChildAt(Setup.IMG_HIDDEN).visible = false; holder.getChildAt(Setup.IMG_VALUE).visible = true; buttonMode = false; } public function hiddeValue():Void { holder.getChildAt(Setup.IMG_VALUE).visible = false; holder.getChildAt(Setup.IMG_HIDDEN).visible = true; buttonMode = true; } } // end classFollowing the Phaser sample extending sprite demo 1 I got this:var PlicatibuTile = function (game, x, y, type) { this.type = 'n' + type; this.holder = new Array(2); this.holder[Main.Setup.GAME_STATE_IMG_HIDDEN] = Phaser.Sprite.call(this, game, x, y, this.type); this.holder[Main.Setup.GAME_STATE_IMG_VALUE] = Phaser.Sprite.call(this, game, x, y, Main.Setup.IMG_PLACE); this.holder[Main.Setup.GAME_STATE_IMG_VALUE].kill();};PlicatibuTile.prototype = Object.create(Phaser.Sprite.prototype);PlicatibuTile.prototype.constructor = PlicatibuTile;PlicatibuTile.prototype.showValue = function() { this.holder[Main.Setup.GAME_STATE_IMG_HIDDEN].kill(); this.holder[Main.Setup.GAME_STATE_IMG_VALUE].revive(); }PlicatibuTile.prototype.hiddeValue = function() { this.holder[Main.Setup.GAME_STATE_IMG_VALUE].kill(); this.holder[Main.Setup.GAME_STATE_IMG_HIDDEN].revive(); }I create an object as follow:this.tile0= PlicatibuTile(game, 0, 0, 2);But then I get the errorUncaught TypeError: Object [object global] has no method 'onTextureUpdate'PIXI.SpritePhaser.SpritePlicatibuTileMain.HelpScreen.initPhaser.StateManager.setCurrentStatePhaser.StateManager.preUpdatePhaser.Game.updatePhaser.RequestAnimationFrame.updateRAF_onLoopCould someone tell me what am I missing? Thanks. Edited:I changed the code from the incorrect callthis.tile0= PlicatibuTile(0, 0, 2);tothis.tile0= PlicatibuTile(game, 0, 0, 2);but I get the same error. Link to comment Share on other sites More sharing options...
plicatibu Posted March 6, 2014 Author Share Posted March 6, 2014 Reading the Classical inheritance with Object.create from Mozilla, I saw that the class is called using new (yes, I missed that from the sample) so I changed the code tothis.tile0= new PlicatibuTile(game, 0, 0, 2);Also realized that my class definition is wrong. I can't do the following:this.holder[Main.Setup.GAME_STATE_IMG_HIDDEN] = Phaser.Sprite.call(this, game, x, y, this.type);this.holder[Main.Setup.GAME_STATE_IMG_VALUE] = Phaser.Sprite.call(this, game, x, y, Main.Setup.IMG_PLACE);because call doesn't return a sprite. My bad. It seems I need a container class. I still don't know how to do it. Link to comment Share on other sites More sharing options...
Hsaka Posted March 6, 2014 Share Posted March 6, 2014 If I understand correctly, you're trying to change the sprite's image with those show and hide functions. You can do that by changing the sprite's frame or if that's not possible, you can use the loadTexture function. So in the constructor you'd have:Phaser.Sprite.call(this, game, x, y, 'n'+type);And in the hide/show functions you can either switch the frame or use loadTexture. plicatibu 1 Link to comment Share on other sites More sharing options...
plicatibu Posted March 6, 2014 Author Share Posted March 6, 2014 @Hsaka, that's exactly what I want to do. I didn't know I could use the loadTexture function to change images loaded separately (All samples I saw the function loadTexture being used to chnage images from atlas). Now it's working like a charm. Thank you so much for your help. Link to comment Share on other sites More sharing options...
Recommended Posts