elderain Posted May 23, 2017 Share Posted May 23, 2017 What I am trying to achieve is an "initial buy" function for a unit. Basically, if the unit's buy flag is false, it show's the buy button. It's a one time button. If the unit's buy flag isn't false (aka, bought), it would delete the button and put something else in it's place. I tried to follow the example on the phaser site but it doesn't seem to want to work right. I've tried putting the destroy in it's own function, etc.. but no luck. Here is my gamestate.js - I'm looking specifically at the gameUnit1 function towards the bottom. Everything else in that function works as I want it to. var GameState = { create: function () { this.counter = 0; //styles this.headerStyle = {font: '30px Montserrat'}; this.HUDStyle = {font: '30px Montserrat'}; this.buttonStyle = {font: '15px Montserrat'}; //basic variables this.gold = 100; this.goldPerSecondBase = 1 //HUD text this.goldText = game.add.text(5, 5, "GOLD: " + this.gold, this.HUDStyle); this.goldText.anchor.setTo(0,0); this.goldPerSecondText = game.add.text(game.world.width-5, 5, "GPS: " + this.goldPerSecond, this.HUDStyle); this.goldPerSecondText.anchor.setTo(1,0); //gold button variables this.goldButtonValue = 200; //gold button this.goldButton = game.add.button(game.world.centerX, 120, 'goldCoin', function(){this.addGold(this.goldButtonValue)}, this); this.goldButton.anchor.setTo(0.5); this.goldButton.scale.setTo(0.4); this.goldButtonText = game.add.text(game.world.centerX, 175, "Click Value: " + this.goldButtonValue, this.HUDStyle); this.goldButtonText.anchor.setTo(0.5); this.goldButtonText.scale.setTo(0.4); //game unit 1 variables this.gameUnit1Name = 'Paladin Tank'; this.gameUnit1Bought = false; this.gameUnit1GoldPerSecond = 0; this.gameUnit1UpgradeLevel = 0; this.gameUnit1Button; }, //game unit 1 buttons gameUnit1: function() { if (this.gameUnit1Bought == false) { this.buyUnit1Button = game.add.button(100, 500, 'blankButton', function(){this.buyUnit1()}, this); if (this.gold <= 200) { this.buyUnit1Button.alpha = .1; } } else { this.buyUnit1Button.destroy(); this.unit1Text = game.add.text(12, 480, this.gameUnit1Name + ":", this.headerStyle); }; }, addGold: function(amount) { this.gold = (amount + this.gold); }, buyUnit1: function() { if (this.gameUnit1Bought == false) { if (this.gold >= 200){ this.gold = (this.gold - 200); this.gameUnit1Bought = true; this.gameUnit1GoldPerSecond = 1; this.gameUnit1UpgradeLevel = 1; }; }; }, calculateGoldPerSeond: function(fps) { this.goldPerSecond = ((this.goldPerSecondBase + (this.gameUnit1GoldPerSecond * this.gameUnit1UpgradeLevel))/fps); this.gold = (this.gold + this.goldPerSecond); }, update: function () { this.gameUnit1(); this.calculateGoldPerSeond(60); this.goldText.setText("Gold: " + ~~this.gold); this.goldPerSecondText.setText("GPS: " + ~~(this.goldPerSecond * 60)); } }; Link to comment Share on other sites More sharing options...
Jammy Posted May 23, 2017 Share Posted May 23, 2017 if gameUnit1 is run inside a button then isnt "this" the button and not your game state? Link to comment Share on other sites More sharing options...
samme Posted May 23, 2017 Share Posted May 23, 2017 You need to explain the problem. Is it not doing what you expect or is it giving you an error? If there's an error message, what is it? Link to comment Share on other sites More sharing options...
elderain Posted May 24, 2017 Author Share Posted May 24, 2017 1 hour ago, samme said: You need to explain the problem. Is it not doing what you expect or is it giving you an error? If there's an error message, what is it? No errors in the console, but it does nothing. Link to comment Share on other sites More sharing options...
Jammy Posted May 24, 2017 Share Posted May 24, 2017 17 minutes ago, elderain said: No errors in the console, but it does nothing. im pretty sure its because you're calling this inside the function as if it'll be gamestate when it'll actually be the button Link to comment Share on other sites More sharing options...
elderain Posted May 24, 2017 Author Share Posted May 24, 2017 2 hours ago, Jammy said: if gameUnit1 is run inside a button then isnt "this" the button and not your game state? 1 minute ago, Jammy said: im pretty sure its because you're calling this inside the function as if it'll be gamestate when it'll actually be the button I'm pretty new to coding so I'm certain it's something I just don't understand. Are you saying I shouldn't be calling it during the function? Or are you saying I am addressing it incorrectly because I am in a function. How else would I call it? Sorry, I don't quite understand what you're getting at. Link to comment Share on other sites More sharing options...
Jammy Posted May 24, 2017 Share Posted May 24, 2017 Yeah since "this" may be the button... Could try this: gameUnit1: function() { if (GameState.gameUnit1Bought == false) { GameState.buyUnit1Button = game.add.button(100, 500, 'blankButton', function(){GameState.buyUnit1()}, GameState); if (GameState.gold <= 200) { GameState.buyUnit1Button.alpha = .1; } } else { GameState.buyUnit1Button.destroy(); GameState.unit1Text = game.add.text(12, 480, GameState.gameUnit1Name + ":", GameState.headerStyle); }; }, Link to comment Share on other sites More sharing options...
samme Posted May 24, 2017 Share Posted May 24, 2017 6 hours ago, elderain said: this.goldButton = game.add.button(game.world.centerX, 120, 'goldCoin', function(){this.addGold(this.goldButtonValue)}, this); That looks correct since the passed context, `this`, is the current state. You may just have to step through this to see what the problem is. Link to comment Share on other sites More sharing options...
elderain Posted May 24, 2017 Author Share Posted May 24, 2017 9 hours ago, samme said: That looks correct since the passed context, `this`, is the current state. You may just have to step through this to see what the problem is. I tried putting console log's in as I stepped through and it always follows the logic right and I can see it even kicks off the text change which is the next step after the button destroy. And there are no console log errors either. Is there a way to debug further? I'm not familiar if so. Link to comment Share on other sites More sharing options...
RubbleGames Posted May 24, 2017 Share Posted May 24, 2017 It looks like you're adding the button more than once, in which case destroy will only destroy the last one added. Try changing your code to check if the button is already there before adding another or using a boolean to stop it being added more than once. samme 1 Link to comment Share on other sites More sharing options...
samme Posted May 24, 2017 Share Posted May 24, 2017 3 hours ago, RubbleGames said: It looks like you're adding the button more than once, in which case destroy will only destroy the last one added. That sounds right. I think you need just one button and then either hide or show it though. this.buyUnit1Button.visible = !this.gameUnit1Bought; Link to comment Share on other sites More sharing options...
Recommended Posts