MishaShapo Posted June 15, 2015 Share Posted June 15, 2015 Greetings fellow Game Devs, For some reason, all the set-up code that I do in the 'create' function becomes nullified once I call an external function from 'update'. Basically, I am checking in 'update' if the skeleton sprite is colliding with the tilemap layer, and when it does, I call an external function called 'moveSkeleton' to set the skeleton velocity. Except the console.log of 'update' says [object Object] while the console.log of 'moveSkeleton' says undefined.GameStates.Game = function (game) { this.map; this.backgroundLayer; this.blockLayer; this.bg; this.skeleton; this.enemySpeed = 40;};GameStates.Game.prototype = { create: function () { this.bg = this.add.tileSprite(0,0,640,640, 'bg'); this.map = this.add.tilemap('myTileMap'); this.map.addTilesetImage('scifi_platformTiles_32x32', 'myTileSet'); this.backgroundLayer = this.map.createLayer('background'); this.blockLayer = this.map.createLayer('blocklayer'); this.setUpEnemy(); this.physics.arcade.gravity.y = 300; this.map.setCollisionBetween(781,786,true,'blocklayer'); this.map.setCollisionBetween(463,464,true,'blocklayer'); this.map.setCollision(779,true,'blocklayer'); }, setUpEnemy: function() { this.skeleton = this.add.sprite(400,400,'skeleton'); this.physics.enable(this.skeleton, Phaser.Physics.ARCADE); this.skeleton.body.collideWorldBounds = true; this.skeleton.body.setSize(16,40,0,-3); this.skeleton.anchor.setTo(0.5,1); this.skeleton.animations.add('move-enemy-right', [148,149,150,151], 10, true); this.skeleton.animations.add('move-enemy-left', [118,119,120,121], 10, true); this.skeleton.animations.play('move-enemy-tight',10,true); console.log("setUpEnemy says this.skeleton : " + this.skeleton); }, moveSkeleton: function() {console.log('moveSkeleton says this.skeleton : ' + this.skeleton); this.skeleton.body.velocity.x = this.enemySpeed; }, update: function () { console.log('update says this.skeleton : ' + this.skeleton); this.game.physics.arcade.collide(this.skeleton, this.blockLayer,this.moveSkeleton); },}; I ran this exact minimal code and even tried to open and close Brackets. And I got nothing. Thank you for reading and helping out. You guys and gals are the best. =) Link to comment Share on other sites More sharing options...
rich Posted June 15, 2015 Share Posted June 15, 2015 The issue is one of scope. The moveSkeleton callback is being called in a scope different to the game. See the callback context parameter of 'collide' to specify it. MishaShapo 1 Link to comment Share on other sites More sharing options...
MishaShapo Posted June 15, 2015 Author Share Posted June 15, 2015 Thank you so much, rich. That fixed it! =) I will be sure to be more mindful of scope and search the documentation more often. Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts