metalNumb Posted December 8, 2014 Share Posted December 8, 2014 Hey all.So i have been facing this annoying issue and i can't solve it because i am very new to prototyping and it is probably simple but here it goes. Here's what's inside my Player "class" ( i think i should not refer to it as class but idk )...// Constructor functionfunction Player(game, spriteTag,x,y){ this.avatarSprite = null ; this.avatarSprite = game.add.sprite(x, y, spriteTag ); game.physics.arcade.enable(this.avatarSprite);}// Adding membersPlayer.prototype = { constructor: Player, moveRight: function () { this.avatarSprite.body.velocity.x = +500; }and when i try to invoke the moveRight function from the main game file after creating an object of it called "avatar" like this ; create(){avatar = new Player(this,'ball', 100, 300 );....}update(){// if button pressedavatar.moveRight();} when i press on the button (Right cursor in my case), gives this error...TypeError: avatar.moveRight is not a functionWhat am i doing wrong ???Please help me. Thanks. Link to comment Share on other sites More sharing options...
fidgetwidget Posted December 9, 2014 Share Posted December 9, 2014 I would need to see more context to know for sure, but are you sure that the avatar object is accessible in the update function? because what you are showing here SHOULD work. there must be something we are missing. Link to comment Share on other sites More sharing options...
xerver Posted December 9, 2014 Share Posted December 9, 2014 Just the code you posted should be fine, there is something else going on here. Need more context to see where the error is Link to comment Share on other sites More sharing options...
metalNumb Posted December 9, 2014 Author Share Posted December 9, 2014 Here's the complete main file// This state file is named Trignox.jsmyTrialGame.Trignox = function(game){};var avatar ;var cursors;myTrialGame.Trignox.prototype = { create: function () { avatar = new Player(this,'ball', 100, 300 ); cursors = this.input.keyboard.createCursorKeys();}, update:function() { if (cursors.right.isDown){ avatar.moveRight(); }}; Link to comment Share on other sites More sharing options...
xerver Posted December 9, 2014 Share Posted December 9, 2014 I'm more concerned with the Player implementation; though ebst would just be a running example where this error is happening so I can use my debugger. Link to comment Share on other sites More sharing options...
hex13 Posted December 9, 2014 Share Posted December 9, 2014 maybe you declared twice Player by accident or something like that. Just paste more code.. // Adding membersPlayer.prototype = {constructor: Player,moveRight: function () {this.avatarSprite.body.velocity.x = +500;}this is all your code? Because there is no closing bracket }, but maybe you just pasted it in this way.. Link to comment Share on other sites More sharing options...
san40511 Posted December 9, 2014 Share Posted December 9, 2014 myTrialGame.Trignox = function(game){ this = game; this.avatar = new Player(this,'ball', 100, 300 ); this.cursors = this.input.keyboard.createCursorKeys();}myTrialGame.Trignox.prototype.update = function() { this.cursors.right.isDown?avatar.moveRight():null;}; Your code is wrong. My variant should be working but it wrong to. Your code doesn't work because your object have base context (without phaser methods). You must init your constructor inside phaser-extended objects metalNumb 1 Link to comment Share on other sites More sharing options...
san40511 Posted December 9, 2014 Share Posted December 9, 2014 game.physics.arcade.enable(this.avatarSprite); - wronggame.physics.enable(this.avatarSprite); - right metalNumb 1 Link to comment Share on other sites More sharing options...
metalNumb Posted December 10, 2014 Author Share Posted December 10, 2014 Well, game.physics.arcade.enable(this.avatarSprite); used to work normally... i will give it a try but...actually the error is not there, it appears when i press a button and invoke the moveRight function. Link to comment Share on other sites More sharing options...
hex13 Posted December 10, 2014 Share Posted December 10, 2014 write, before line that causes error:console.log('av', avatar);and run, check (and write here) in console what will appear. Check what properties avatar has.and, similarily:console.log('pl', Player);andconsole.log('pl pro', Player.prototype);andconsole,log('pl inst", avatar instanceof Player);andconsole.log('meth', avatar.moveRight); // probably undefined but who knows... and so on. Without proper debugging it is only a guessing game... metalNumb 1 Link to comment Share on other sites More sharing options...
metalNumb Posted December 10, 2014 Author Share Posted December 10, 2014 @san40511How can i edit my object to be phaser-extended ? What does it mean exactly ?Isn't it enough to just pass a reference to the game object to be able to call phaser methods from inside the object ? Please clarify.Thanks ! Link to comment Share on other sites More sharing options...
metalNumb Posted December 10, 2014 Author Share Posted December 10, 2014 @hex13Well, here is the output of the debugging console. phaser.js:22011 Phaser v2.0.7 | Pixi.js v1.6.1 | WebGL | WebAudio http://phaser.io ♥♥♥ Trignox.js:18 pl inst trueTrignox.js:25 av Player {avatarSprite: Phaser.Sprite, constructor: function}Trignox.js:26 meth undefinedTrignox.js:27 Uncaught TypeError: undefined is not a functionThank you so much. Link to comment Share on other sites More sharing options...
metalNumb Posted December 10, 2014 Author Share Posted December 10, 2014 Trignox.js:25 av Player {avatarSprite: Phaser.Sprite, constructor: function}I guess it means that the object created does NOT have a member called moveRight() ??So that is why it's undefined ? EDIT : Well, i have put my moveRight() function in the constructor and it worked,but i don't think i should put everything in the constructor, that does not make sense to me. Link to comment Share on other sites More sharing options...
stauzs Posted December 10, 2014 Share Posted December 10, 2014 hi, your code should work, collected from all posts - and came up with something like this:var myTrialGame = {};var avatar ;var cursors;// Constructor functionfunction Player(game, spriteTag,x,y){ this.avatarSprite = null ; this.avatarSprite = game.add.sprite(x, y, spriteTag ); game.physics.arcade.enable(this.avatarSprite);}// Adding membersPlayer.prototype = { constructor: Player, moveRight: function () { this.avatarSprite.body.velocity.x = +500; }};myTrialGame.Trignox = function(game){};myTrialGame.Trignox.prototype = { preload: function(){ this.load.image("ball", "assets/box.png"); }, create: function () { avatar = new Player(this,'ball', 100, 300 ); cursors = this.input.keyboard.createCursorKeys(); }, update:function() { if (cursors.right.isDown){ avatar.moveRight(); } }};var game = new Phaser.Game(800, 600, Phaser.AUTO, document.body, myTrialGame.Trignox);you can check working example here: http://mightyeditor.mightyfingers.com/data/projects/ph1l/phaser/index.htmlmake sure you don't overwrite avatar variable somewhere in the other code.. for debugging purposes you can even rename variable to something like: avatarTest Link to comment Share on other sites More sharing options...
metalNumb Posted December 15, 2014 Author Share Posted December 15, 2014 I got this to work normally, the only thing i needed to do is to change the name of the class because apparently - for some reason - the name "Player" confuses it somehow !!!? Link to comment Share on other sites More sharing options...
Recommended Posts