hackenstein Posted September 29, 2013 Share Posted September 29, 2013 I'm playing around with a small topdown tilebased rpg prototype. To make my player character walk I add some sprites to highlight the tiles he can walk to and handle clicks on them. At the moment my character can only walk up, because I haven't found a way to pass the direction to my walkToTile function. Of course I could use individual functions for each direction, but that seems kind of silly.Is there a way to pass arguments or somehow access the correct sprite from within my walkToTile? I have seen that events have a parent property, which should contain the sprite, but I don't know how to access that either.Help would be greatly appreciated createWalkTiles: function createWalkTiles(){ var x = this.player.x; var y = this.player.y; var walkTiles = this.walkTiles; var map = this.map; var i = walkTiles.length; while ( i-- ) walkTiles[i].kill(); walkTiles.length = 0; if ( map.getTileFromWorldXY(x,y-this.map.tilesize,0).collideNone ) walkTiles.push( this.game.add.sprite(x,y-this.map.tilesize,'tilemarkups') ); if ( map.getTileFromWorldXY(x,y+this.map.tilesize,0).collideNone ) walkTiles.push( this.game.add.sprite(x,y+this.map.tilesize,'tilemarkups') ); if ( map.getTileFromWorldXY(x-this.map.tilesize,y,0).collideNone ) walkTiles.push( this.game.add.sprite(x-this.map.tilesize,y,'tilemarkups') ); if ( map.getTileFromWorldXY(x+this.map.tilesize,y,0).collideNone ) walkTiles.push( this.game.add.sprite(x+this.map.tilesize,y,'tilemarkups') ); i = walkTiles.length; while(i--) { walkTiles[i].anchor.setTo(.5,.5); walkTiles[i].animations.add('none',[1]); walkTiles[i].animations.add('freeTile',[0]); walkTiles[i].inputEnabled = true; walkTiles[i].input.useHandCursor = true; walkTiles[i].events.onInputDown.add(this.walkToTile, this); } },walkToTile: function walkToTile(){ var i = this.walkTiles.length; while(i--) this.walkTiles[i].animations.play('none', 1, false); this.game.physics.moveTowardsObject(this.player,this.walkTiles[0],36); this.startWalk();}, Link to comment Share on other sites More sharing options...
Ezelia Posted September 29, 2013 Share Posted September 29, 2013 do you mean something like this :walkTiles[i].events.onInputDown.add(function(arg1, arg2) { this.walkToTile(arg1); do_something_elese(arg2);}, this); Link to comment Share on other sites More sharing options...
hackenstein Posted September 29, 2013 Author Share Posted September 29, 2013 Yeah something like that! Thanks I haven't found an explanation why this works though. How come arg1 is actually the correct reference to my sprite object? Link to comment Share on other sites More sharing options...
rich Posted September 29, 2013 Share Posted September 29, 2013 Because onInputDown sends the sprite as the parameter (along with the Pointer object that caused the event to fire). Link to comment Share on other sites More sharing options...
hackenstein Posted September 30, 2013 Author Share Posted September 30, 2013 I see, thanks rich Link to comment Share on other sites More sharing options...
Recommended Posts