I've made a constructor (and put it above preload function) like this
character = function(CharX,CharY,CharSpeed){
this.x = CharX ;
this.y = CharY;
this.speed = CharSpeed;
this.AddSpriteSheet = function (SprX,SprY,key) {
this.character = game.add.sprite(SprX,SprY,key);}
};
Later in create function I added
var Char1 = new character(game.world.width*0.5,game.world.height*0.5,5);
Char1.AddSpriteSheet(game.world.width*0.5,game.world.height*0.5,'character');
Char1.anchor.set(50,50);
Console reads
"Uncaught TypeError: Cannot read property 'set' of undefined"
What have i done wrong?
Edit : Make error more visible
'set' of undefined" bca "set" is Pixi's compare to "setTo" Phaser's
Char1.anchor.set(50,50); replace by Char1.anchor.setTo(0.5);// 0.5, 0.5 will point to center of the sprite's square, if that is the intention. this is also truth for .scale.setTo();
also if you create new prototype object out of create function i would recommend to follow this example https://phaser.io/examples/v2/games/tanks
Your constructor
character
does not have a propertyanchor
, thus,Char1.anchor
does not exist, and neither doesChar1.anchor.set
.You're making a custom class to represent a character, but it's not a Phaser Sprite, and therefore it doesn't have any of the methods/properties a Sprite does unless you define them yourself. If you want to create your own class to extend Phaser.Sprite, I suggest you look at this forum post and also this example. Just googling "phaser extend sprite" will help you find some other resources as well.
Essentially, you need to do something like this:
And then you would add all of your Character's methods onto the prototype.