Jump to content

undefined is not a function !


metalNumb
 Share

Recommended Posts

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 function

What am i doing wrong ???

Please help me. 

 

Thanks.

Link to comment
Share on other sites

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

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

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

Link to comment
Share on other sites

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);

and

console.log('pl pro', Player.prototype);

and

console,log('pl inst", avatar instanceof Player);

and

console.log('meth', avatar.moveRight); // probably undefined but who knows... ;)

and so on. Without proper debugging it is only a guessing game...

Link to comment
Share on other sites

@hex13

Well, here is the output of the debugging console.  :rolleyes:

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 function

Thank you so much.

Link to comment
Share on other sites

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 ?  :huh:

 

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

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.html

make 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

 Share

  • Recently Browsing   0 members

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