GxDT Posted December 19, 2016 Share Posted December 19, 2016 Hello people of this forum. Here is what you normally do when you wish to add a callback function for a keyboard key: aKey = game.input.keyboard.addKey(Phaser.KeyCode.A); aKey.onDown.add(AKeyPressed, this); In my case I wish to add same callback function when a new game frame is updated. (Yes, I know there is a reserved update function for this stuff, but this is not how I want to use it) Link to comment Share on other sites More sharing options...
drhayes Posted December 19, 2016 Share Posted December 19, 2016 Why don't you want to use the update function in your state? That's exactly what it's for. Alternatively, you could make a Phaser.Plugin, add it to the PluginManager, and use its update function if you can't use your state's update function for some reason. Link to comment Share on other sites More sharing options...
GxDT Posted December 19, 2016 Author Share Posted December 19, 2016 Using update function works perfectly too. I just was exploring the API and ways to work with it. Guess I owe an explanation on what is going on. Here I'm working on 2D platformer game AI. function CrabBehaviour(crabSprite) { this.status = { patrol: false, patrolLeft: false, patrolRight: false, attacking: false } this.leftRallyPoint = 0; this.rightRallyPoint = 0; this.patrol = function(leftPoint, rightPoint) { this.leftRallyPoint = leftPoint; this.rightRallyPoint = rightPoint; this.status.patrol = true; if (!this.status.patrolLeft && !this.status.patrolRight) { this.status.patrolLeft = true; this.patrolLeft(); } } this.patrolRight = function () { this.status.patrolLeft = false; this.status.patrolRight = true; game.physics.arcade.moveToXY(evil_crab, evil_crab.body.position.x + 1, evil_crab.body.position.y, 40); } this.patrolLeft = function () { this.status.patrolLeft = true; this.status.patrolRight = false; game.physics.arcade.moveToXY(evil_crab, evil_crab.body.position.x - 1, evil_crab.body.position.y, 40); } this.frameEvent = function () { if (this.status.patrol) { if (crabSprite.body.position.x <= this.leftRallyPoint && this.status.patrolLeft) { this.patrolRight(); } if (crabSprite.body.position.x >= this.rightRallyPoint && this.status.patrolRight) { this.patrolLeft(); } } } } The this.frameEvent function is responsible for tracking of what is going on 'live' so it should be called every time a new frame comes up. (In this case - change movement direction when left or right rally point is reached. Link to comment Share on other sites More sharing options...
drhayes Posted December 20, 2016 Share Posted December 20, 2016 Ah, okay, that makes sense. I do something like that in a game I'm working on, where the AI/behaviors for certain sprites come from a bunch of separate objects (StayOnPlatform, WatchesHealth, etc). They use a combination of every-frame stuff (by overriding update) and event-driven stuff (did I just get hit, did my animation complete). Link to comment Share on other sites More sharing options...
Recommended Posts