casey Posted February 4, 2018 Share Posted February 4, 2018 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 More sharing options...
ForgeableSum Posted February 4, 2018 Share Posted February 4, 2018 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 More sharing options...
casey Posted February 5, 2018 Author Share Posted February 5, 2018 INput handlers work fine, and the frame changes on the last of the sprites in the array with the code provided, it just doesn't work on each individual sprite. Link to comment Share on other sites More sharing options...
3man7 Posted February 5, 2018 Share Posted February 5, 2018 Without reading too much.. try adding 'animations' before .frame this.empty.animations.frame += 1; Link to comment Share on other sites More sharing options...
casey Posted February 5, 2018 Author Share Posted February 5, 2018 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 More sharing options...
casey Posted February 5, 2018 Author Share Posted February 5, 2018 Oh crikey. It was simple. The line just needed to send from the original array this.empty not "this" , so this.empty.events.onInputDown.add(this.changeFrame, this.empty). Someday I'll grok this whole programming thing 3man7 1 Link to comment Share on other sites More sharing options...
Recommended Posts