Search the Community

Showing results for tags 'inheritance'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • HTML5 Game Coding
    • News
    • Game Showcase
    • Coding and Game Design
  • Frameworks
    • Phaser
    • Pixi.js
    • Babylon.js
    • Panda.js
    • melonJS
    • Haxe JS
    • Kiwi.js
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Found 9 results

  1. Hi, I have been going through a tutorial on recreating FruitNinja (unimportant maybe). I am a beginner in JS with experiance in Java. I need some help or reference where to get it - I have 2 states like this: var FruitNinja = FruitNinja || {}; FruitNinja.JSONLevelState = function () { "use strict";; }; FruitNinja.JSONLevelState.prototype = Object.create(Phaser.State.prototype); FruitNinja.JSONLevelState.prototype.constructor = FruitNinja.JSONLevelState; (...) FruitNinja.JSONLevelState.prototype.create = function () { this.create_prefab(prefab_name, this.level_data.prefabs[prefab_name]); }; FruitNinja.JSONLevelState.prototype.create_prefab = function (prefab_name, prefab_data) { if (this.prefab_classes.hasOwnProperty(prefab_data.type)) { do stuff... }; (...) and var FruitNinja = FruitNinja || {}; FruitNinja.LevelState = function () { "use strict";; this.prefab_classes = { "background": FruitNinja.Prefab.prototype.constructor, (...) }; }; FruitNinja.LevelState.prototype = Object.create(FruitNinja.JSONLevelState.prototype); FruitNinja.LevelState.prototype.constructor = FruitNinja.LevelState; FruitNinja.LevelState.prototype.create = function () { "use strict";; (...) } Somehow JSONLevelState can access LevelState this.prefab_classe object. I don't understand how. Can I somehow print it to chrome console? Or can someone explain it or point me to some external reference ? I have watched couple of videos on youtube on inheritance in JS but I still don't get it. Please help, Ok Solved. ";" allows dunc from JSONLevelState be called in LevelState object (having access to its variables. Watch for a complete explanation.
  2. Hi everyone, I'm trying to create two types of enemies, the first is a robot that has 2 methods: sleep and patrol. My second enemy is a flyingEnemy. The aim is to inherit the sleep method from the robot but amend the patrol method. Can anyone show me how my flyingEnemy can inherit from robot whilst amending the patrol method? Below is my code. When I create the flyingEnemy, its patrol method overwrites the robot's patrol method and ALL enemies have the same behavior. var SuperSmash = SuperSmash || {}; SuperSmash.flyingEnemy = function(game, x, y, key, velocity, tilemap, player) { "use strict";, game, x, y, key); = game; this.tilemap = tilemap; this.player = player; this.animations.add("fly", [0]); }; SuperSmash.flyingEnemy.prototype = Object.create(SuperSmash.Enemy.prototype); SuperSmash.flyingEnemy.prototype.constructor = SuperSmash.flyingEnemy; SuperSmash.flyingEnemy.prototype.patrol = function() { "use strict";, this.player, 200); };
  3. Hi people, I'm extending the Sprite class with a class named "Being", which I further extend with two child classes, "Monster" and "Player". But it doesn't work. Here is first the code for my classes. function Being(x,y,key){, game, x,y,key); game.add.existing(this); } Being.prototype = Object.create(Phaser.Sprite.prototype); Being.prototype.constructor = Being; function Player(x,y,key){,y,key); } Player.prototype = Object.create(Being.prototype); Player.prototype.constructor = Player; function Monster(x,y,key){,y,key); } Monster.prototype = Object.create(Being.prototype); Monster.prototype.constructor = Monster; ("game" is the Phaser.Game instance and is a global variable, I didn't forget to pass it as an argument.) I have looked at the examples and forum topics, and it seems to me that it's the proper way to do. Now if I create a new instance of Being, which inherits from Sprite, this works like a charm. var B = new Being(300,300,'player'); But if I try to make a new instance of Player, which inherits from Being which itself inherits from Sprite, the it doesn't work. The code is: var P = new Player(200,200,'player'); And in the console I get the following error: "phaser.js:15246 Uncaught TypeError: this.onTextureUpdate is not a function". This error makes the entire game crash. I'm really puzzled by the fact that it works for Being, but not for Player, which seems to indicate that all this code is mostly correct, except that I must be doing something wrong with the "second-level" inheritance. Any help would be greatly appreciated!
  4. Hello, recently I found code that is defining objects inheriting from Phaser.Sprite (if I google'd it and understood correctly). Can someone explain a little more about function? I haven't found it anywhere in docs. Also, what are the possible parameters? I found an example of code on some flappy bird tutorial : Pipe = function (game, x, y, speed) {, game, x, y, "pipe"); game.physics.enable(this, Phaser.Physics.ARCADE, this); this.body.velocity.x = speed; this.giveScore = true; }; Pipe.prototype = Object.create(Phaser.Sprite.prototype); Pipe.prototype.constructor = Pipe; Pipe.prototype.update = function() { if(this.x+this.width<bird.x && this.giveScore){ score+=0.5; intro.prototype.updateScore(); this.giveScore = false; } if(this.x<-this.width){ this.destroy(); } }; I understand that "this" in the code is object (Pipe) right? I also have a question if I can (and how) create another sprite inside that Pipe - for example if I can have Pipe object that have two sprites (pipe1 and pipe2 which will have different properties). Or should I just define two objects and spawn both of them instead one with 2 sprites?
  5. Can the 'create' function in Phaser.Group be successfully over-ridden? Is there an alternative? I am looking for a way to have a function called at the start of my object's life-cycle and still be able to access the functions I have built into the class. I am used to Unity3D where there is always an option to do this. There is certain code that I need to execute at the start of my object's life-cycle, and additionally whenever the level ID is set in my function. Am I being clear, or have I worded this too confusingly? Thanks much! FriarDude.FallingShitFactory = function (game) { // Class variables this.levelID = 0; = false; this.spawnTimer = null; this.percentShitQuantities = new Array(); this.HORIZONTAL_MARGIN = 64; // // The constructor can take an optional parameter (levelID, 0 by default) if (arguments.length == 2) { this.levelID = arguments[1]; } // // Constructor, game);, this.spawnShit, this); console.log('construct'); return this; //};FriarDude.FallingShitFactory.prototype = Object.create(Phaser.Group.prototype);FriarDude.FallingShitFactory.prototype.constructor = FriarDude.FallingShitFactory;FriarDude.FallingShitFactory.prototype.create = function() { // Load Level Data console.log('create'); this.setLevelID(this.levelID); //}
  6. 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) {, 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);
  7. Hello, I'm still learning Phaser. I'm familiar with OOP Perspective. But from what I learnt from many tutorial in Phaser, Phaser does not have obvious OOP-perspective. So the tutorial use a workaround to it. For example, if I want to have a customized draggable sprite (with spesific dragDrop eventhandler). I need to create an object, which have a properties (pointer) to sprite. I can then use the sprite as the object itself (sort of wrap the sprite with my custom object method and properties). This approach is simple when used in simple object, but I can see this might create some problem for more complex object. So I am thinking maybe the reason most tutorial use this approach to do this is because they are tutorial (which tries to shows the simplest way to do it). My question is : Is this the correct practice when using phaser? Using the pseudo-inheritance workaround, as extending a base class. Or is there a better practice and cleaner approach to do this for more complex object? Thank you.
  8. Hi everyone, I'm trying to create a clean HUD class using Phaser 2.0.7 but I am getting a weird error: "Uncaught TypeError: Cannot read property 'x' of undefined " I created a separate hud.js file with the following code: function HeadUpDisplay(game, parent, name, useStage){, game); var fpsCounter = new Phaser.Text(0,0, "test", { font: "16px Arial", fill: "#ff0044" }); fpsCounter.fixedToCamera = true; this.add(fpsCounter); this.update = function() { fpsCounter.setText(game.time.fps); };};HeadUpDisplay.prototype = Object.create(Phaser.Group.prototype);HeadUpDisplay.prototype.constructor = HeadUpDisplay; So basically my "class" HeadUpDisplay is extending's group. Any idea what is wrong? (and/or what could be improved) Thanks!
  9. Apologies for the repost, the other thread is very old and marked as 'answered' so I think my question is not being seen by the people who know... Is Sprite inheritance broken in the current Main branch? I'm getting "error: undefined is not a function" on line 1719 of Phaser.js: this.onTextureUpdate(); from this code: Person = function(game) {, game, 0, 0, 'person'); }; Person.prototype = Object.create(Phaser.Sprite.prototype); Person.prototype.constructor = Person; invoked by: var person = new Person(game); The reason I want to do this is to make a Group of type Person (to simplify collisions) and it needs to be a display object to work with Group. Other good solutions welcomed!