Jump to content

Can I pass arguments to functions called by onDown?


Recommended Posts

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


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...