piobug

Members
  • Content count

    73
  • Joined

  • Last visited

About piobug

  • Rank
    Advanced Member

Profile Information

  • Gender
    Not Telling

Recent Profile Visitors

564 profile views
  1. How to double jump with phaser ?

    Game Mechanic Explorer's website has a double jump example in Phaser https://gamemechanicexplorer.com/#platformer-5
  2. Do you mean like this? init: function(levelData) { this.game.levelData = levelData; }, create: function() { this.enemy = new Enemy[this.game.levelData.enemy] (game, this.player); this.add.existing(this.enemy); } I get a " Enemy[this.game.levelData.enemy] is not a constructor" error message
  3. Hi all, I have the game object "levelData" in "menu.js", that decides which assets will be loaded per level. In "play.js" I manage to load a different map per level, but I can't figure out how to create a different enemy per level using the new constructor. Thanks var menuState = { init: function() { this.levelData = { 1: { enemy: 'teacher', map: 'teacher', timeScore: '' }, 2: { enemy: 'seeker', map: 'base', timeScore: '' } }; }, //other stuff... var playState = { init: function(levelData) { this.levelData = levelData; }, create: function() { //this works this.map = game.add.tilemap(this.levelData.map); //this ofc doesn't this.enemy = new Enemy[this.levelData.enemy] (game, this.player); this.add.existing(this.enemy); }, }; var Enemy = {}; Enemy.Teacher = function (game,target) { //relevant code }; Enemy.Teacher.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Teacher.prototype.constructor = Enemy.Teacher; Enemy.Seeker = function (game,target) { //relevant code }; Enemy.Seeker.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Seeker.prototype.constructor = Enemy.Seeker;
  4. Access sprite's data object

    Yay, It worked! @drhayes didn't know about JS getters and setters. Thanks a lot! Here's the working code: var Player = function (game, bool) { this.isCatcher = bool; Phaser.Sprite.call(this, game, 0,0); this.data = { catcherData: { speed: 2000, attack: 200, health: 20, }, throwerData: { speed: 1000, attack: 100, health: 10, }, }; Object.defineProperty(this.data, 'stats', { get: function(){ if(this.isCatcher) { return this.data.catcherData; } else { return this.data.throwerData; } }.bind(this) }); }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; Player.prototype.update = function() { console.log(this.data.stats.speed); //outputs 2000 if 'isCatcher' is true, otherwise outputs 1000 };
  5. Access sprite's data object

    Thanks @drhayes. I'm sorry I wasn't really clear about my intentions, but you guessed them all. Yes, I want to change isCatcher dynamically after creation (I have 2 player objects that could be either a catcher or a thrower). Yes, data should change during gameplay: those are supposed to be player's stats that update on item collection or damage taken and so on... I'd like to access data with one variable depending on the bool: e.g. if player is catcher then this.data.speed is 2000, and if player is not catcher (or is thrower) then this.data.speed is 1000. I wanted to avoid if/else checks on isCathcher everytime I need to access the data object. I moved 'getData' out as function of the player object, 'this.data.speed' is still undefined. Of course 'this.data.throwerData.speed' returns 1000, but the whole point of this is to avoid using '.throwerData'. and '.catcherData' Hope my explanation is clear
  6. Sprite.Body is undefined

    Of course! Thanks a lot! I was banging my head for too long on this one!
  7. Sprite.Body is undefined

    Hi all, Why am I getting the error "Cannot read property 'velocity' of undefined" when calling the stopMoving function in Ball? Thanks var Ball = function (game) { Phaser.Sprite.call(this, game,0,0,'ball'); game.physics.enable(this, Phaser.Physics.ARCADE); this.anchor.setTo(0.5, 0.5); this.scale.setTo(1); }; Ball.prototype = Object.create(Phaser.Sprite.prototype); Ball.prototype.constructor = Ball; Ball.prototype.stopMoving = function() { this.body.velocity.setTo(0, 0); }; Ball.prototype.moveTo = function(start, destination) { game.world.add(this); //detach from parent this.reset(start.x, start.y); this.game.physics.arcade.moveToXY(this, destination.x, destination.y, 500); }; playstate var playState = { create: function() { this.createThrower(); this.createCatcher(); this.createBall(); }, update: function() { game.physics.arcade.overlap(this.ball, this.player2, this.ball.stopMoving, null, this); }, createThrower: function() { this.playerGroup = game.add.group(); this.player = new Player(this.game); this.game.add.existing(this.player); this.playerGroup.add(this.player); }, createCatcher: function() { this.player2 = new Player(this.game); this.game.add.existing(this.player2); this.playerGroup.add(this.player2); }, castSpell: function(sprite) { this.ball.moveTo(this.player, this.player2); }, createBall: function() { this.ball = new Ball(this.game); this.player.addChild(this.ball); }, };
  8. Game Board Renders black

    I'm not familiar with codepen, but the console is saying that it has issues with loading your images: "Phaser.Loader error loading file: player from URL http://robbotdev.com/wp-content/uploads/2016/11/bluesquare.png" "Phaser.Loader error loading file: food from URL http://robbotdev.com/wp-content/uploads/2016/11/Pinksquare.png"
  9. Access sprite's data object

    Thanks, make sense. A followup question: how'd then retrieve data with a boolean condition? var Player = function (game, bool) { this.isCatcher = bool; Phaser.Sprite.call(this, game, 0,0); this.data = { catcherData: { speed: 2000, attack: 200, health: 20, }, throwerData: { speed: 1000, attack: 100, health: 10, }, getData: function() { this.isCatcher? catcherData : throwerData; } }; }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; Player.prototype.update = function() { console.log(this.data.speed); };
  10. Access sprite's data object

    Thanks! It worked. Is this because 'Phaser.Sprite.call' calls the data object constructor in order for me to be able to use it?
  11. Access sprite's data object

    How do populate and retrieve data to and from the sprite's data object? Let's say I want to save Player's stats (speed, attack, health) in it and then retrieve it later. var Player = function (game) { this.data = { 'speed': 1, 'attack': 1, 'health': 1 }; Phaser.Sprite.call(this, game, 0,0); }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; Player.prototype.update = function() { console.log(this.data.speed); };
  12. Access variable in state from class?

    Thanks a lot! That was super helpful and clear! I'm changing my code now.
  13. Access variable in state from class?

    Thanks a lot @mattstyles for the thorough answer and for calling out my bad code. I'm a hacking a prototype and I could come up with the right syntax. I'd love to avoid bad code and tight coupling, should I look into implementing something like an observer pattern or is there a way to make it better in a simpler way?
  14. Hi, I have classes for single objects (players, enemies, weapons) and states. One of those, playstate, manages interaction between the objects. How do I access a variable e.g. this.killedEnemies in playstate from e.g. the enemy object? play.js var playState = { create: function() { //some code... }, update: function() { this.killedEnemies = 0; }, }; enemy.js var Enemy = function (game, position) { //some code... }; Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; Enemy.prototype.die = function() { this.killedEnemies += 1; }; Thanks