Jump to content

changing animation frames of sprite in array


casey
 Share

Recommended Posts

Populating a grid row of sprites and trying to change animation frame on click. I'm missing something obvious here, I know.... ????

preload: function(){
		this.game.load.spritesheet('colours', 'images/colours.png', 52, 52, 7);

	},

create: function(){
//set positions of x and y for row 
      this.positions =[{x: 0, y:0}, {x:55, y:0}, {x:110, y:0}, {x:165, y:0}];
//row is my array of coloured sprites, with 7 possible colours
	  this.row = [];
		for(var i in this.positions){
			this.pos = this.positions[i];
			this.empty = this.add.sprite(this.pos.x, this.pos.y, 'colours', 6);
			this.empty.inputEnabled = true;
			this.empty.events.onInputDown.add(this.changeFrame, this);
			this.row.push(this.empty);
		}

},


//this part is the bit that doesn't work
//I want to set it so every time the sprite is clicked, the animation frame goes up 1, changing the colour of the sprite. 

changeFrame: function(){
		this.empty = this;
		this.empty.frame + = 1;

	},

 

Link to comment
Share on other sites

1. Assure that the input handler on the sprite is actually working (e.g. console.long in changeFrame).

2. How many total frames in the sprite are there? You're starting on frame 6. Does adding a frame set the number greater than the total number of frameS?

3. I always wince a little when people use 'this'. I know it's convention, but I disagree with convention. the problem with using this everything is that sometimes you mix up what 'this' actually means. assure that 'this' means what you think it means. or better yet, name it something intelligible based on context so it will be easier to debug your code. 

4. this this is the game.world, are you not defining this.empty many times over, for each position?

Link to comment
Share on other sites

48 minutes ago, 3man7 said:

Without reading too much..

 

Thanks, nope. It still only works on the last item in the array.   

How would one normally set a property like this onto items in an array? It seems like this should be a simple thing to do!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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