Skip to content Skip to sidebar Skip to footer

Uncaught TypeError: Cannot Read Property 'set' Of Undefined

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

Solution 1:

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:

function Character(game, x, y) {   
    Phaser.Sprite.call(this, game, x, y, 'sprite key');   
    // define other properties for your character
}
Character.prototype = Object.create(Phaser.Sprite.prototype);
Character.prototype.constructor = Character;

And then you would add all of your Character's methods onto the prototype.


Solution 2:

Your constructor character does not have a property anchor, thus, Char1.anchor does not exist, and neither does Char1.anchor.set.


Solution 3:

'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


Post a Comment for "Uncaught TypeError: Cannot Read Property 'set' Of Undefined"