ekeimaja Posted November 10, 2015 Share Posted November 10, 2015 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 More sharing options...
Skeptron Posted November 10, 2015 Share Posted November 10, 2015 What's Peli? Shouldn't you pass the game instead? http://phaser.io/docs/2.3.0/Phaser.Sprite.html new Sprite(game, x, y, key, frame) Link to comment Share on other sites More sharing options...
ekeimaja Posted November 10, 2015 Author Share Posted November 10, 2015 "peli" is game in finnish. I tried with changing it and says same "game is not defined". And I am not using spritesheet, so frame is not needed. Problem is that game variable needs to define globally, but I don't know how. Link to comment Share on other sites More sharing options...
Skeptron Posted November 10, 2015 Share Posted November 10, 2015 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 More sharing options...
ekeimaja Posted November 10, 2015 Author Share Posted November 10, 2015 Now I renamed some files and variables "this.onCreateCallback.call is not a function". Link to comment Share on other sites More sharing options...
Skeptron Posted November 10, 2015 Share Posted November 10, 2015 Without seeing your code... I can't do much dude... Link to comment Share on other sites More sharing options...
ekeimaja Posted November 10, 2015 Author Share Posted November 10, 2015 in player.jsvar 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.jsvar HardDrive = HardDrive || {};HardDrive.game = function () {};HardDrive.game.prototype = { create: function () { new HardDrive.Player(game, 500, 380); }; Link to comment Share on other sites More sharing options...
jmp909 Posted November 10, 2015 Share Posted November 10, 2015 it's Phaser.Sprite.call(this, game, x, y, 'car');http://jsfiddle.net/Le819L5x/2/ this is demonstrated in the exampleshttp://phaser.io/examples/v2/sprites/extending-sprite-demo-1 see here as wellhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/callhttps://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 More sharing options...
jmp909 Posted November 10, 2015 Share Posted November 10, 2015 I tried to explain it a bit for you herehttp://jsfiddle.net/6opa5893/5/ Link to comment Share on other sites More sharing options...
ekeimaja Posted November 10, 2015 Author Share Posted November 10, 2015 I tried already to add this. keyword first, but still not working. Does inheritance even work between separate files? Link to comment Share on other sites More sharing options...
jmp909 Posted November 10, 2015 Share Posted November 10, 2015 Javascript would see it as one long chunk of code, so yes. unless you'd modularized your scope somehow Link to comment Share on other sites More sharing options...
jmp909 Posted November 10, 2015 Share Posted November 10, 2015 also where are you setting this.car ? is "this" in that case pointing to where you think it is? Link to comment Share on other sites More sharing options...
ekeimaja Posted November 11, 2015 Author Share Posted November 11, 2015 I will look it again later today when I have better time for it. Link to comment Share on other sites More sharing options...
Recommended Posts