Jump to content

Why my inheriting does not work?


ekeimaja
 Share

Recommended Posts

So I try to make inheriting to my game, but it says "peli is not defined". Where and how I need to define it?

 

player.js:

var HardDrive = HardDrive || {};HardDrive.Player = function (peli, x, y) {    Phaser.Sprite.call(this, peli, x, y, 'auto');        peli.add.existing(this);};HardDrive.Player.prototype = Object.create(Phaser.Sprite.prototype);HardDrive.Player.constructor = HardDrive.Player;

game.js:

this.pelaaja = new HardDrive.Player(peli, 500, 380);
Link to comment
Share on other sites

Did you try to check the game variable before passing it to the Player constructor? My guess is that it's null beforehand.

 

And you should code in english, it's a good practice... It's gonna be much easier for whoever works with you (like us right now) to help.

 

Just sayin'...

Link to comment
Share on other sites

in player.js

var HardDrive = HardDrive || {};HardDrive.Player = function (game, x, y) {    Phaser.Sprite.call(game, x, y, 'car');        game.add.existing(this);};HardDrive.Player.prototype = Object.create(Phaser.Sprite.prototype);HardDrive.Player.constructor = HardDrive.Player;HardDrive.Player.prototype.create = function () {    this.game.physics.enable(this.car, Phaser.Physics.ARCADE);    this.car.anchor.setTo(0.5, 0.5);

and in game.js

var HardDrive = HardDrive || {};HardDrive.game = function () {};HardDrive.game.prototype = {    create: function () {        new HardDrive.Player(game, 500, 380);    };
Link to comment
Share on other sites

it's

 Phaser.Sprite.call(this, game, x, y, 'car');

http://jsfiddle.net/Le819L5x/2/

 

this is demonstrated in the examples

http://phaser.io/examples/v2/sprites/extending-sprite-demo-1

 

see here as well

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

https://msdn.microsoft.com/en-us/library/h2ak8h2y(v=vs.94).aspx

 

The call method is used to call a method on behalf of another object. It allows you to change the this object of a function from the original context to the new object specified by thisObj.

 

however if peli === game, then that's what you had before anyway (you changed your code since though)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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