ZHOUDAHAO Posted July 30, 2016 Share Posted July 30, 2016 I'm making a game similar to the traditional magic tower,and now I have a problem when dealing with the battles between the monster and the braver. here's my code in update(): function update(){ ... game.physics.arcade.collide(braver, monsters, battle, null, this); ... } and here's my battle(): function battle(player, monster) { var x = (player.attack > monster.defence ? player.attack - monster.defence : 0); var y = (monster.attack > player.defence ? monster.attack - player.defence : 0); battleScene.visible = true; while (1) { if (Date.now() > battleTimer) { battleTimer = Date.now() + 1000; monster.health -= x; if (monster.health <= 0) { monster.kill(); player.gold += monster.gold; player.exp += monster.exp; break; } player.health -= y; if (player.health <= 0) { player.kill(); break; } } } battleScene.visible = false; } battleScene is a group,the battle box,What makes me crazy is that battleScene doesn't show on the screen at all(even with battleScene.visible = true),I've tried game.world.bringToTop(battleScene); but it doesn't work,too.So how can I fix that bug? Link to comment Share on other sites More sharing options...
rich Posted July 30, 2016 Share Posted July 30, 2016 It's impossible to say from just the code posted above. Where is battlescene declared? It's not passed into your function, so if it isn't a global variable then it can't be accessed from within your function either. Link to comment Share on other sites More sharing options...
XekeDeath Posted July 30, 2016 Share Posted July 30, 2016 You have no draw calls between making it visible and hiding it again... You show the battleGroup, then you sit there and process the battle, then you hide it, then exit the function and continue regular program processing... All in one update call... You need to make it visible, then run several loops of the program to process the battle scene, then hide it once the battle is over... You will need to rethink using the while loop in there... ZHOUDAHAO 1 Link to comment Share on other sites More sharing options...
ZHOUDAHAO Posted July 30, 2016 Author Share Posted July 30, 2016 thank you so much!!! @XekeDeath ,you've solved my problem perfectly,indeed the right way is to process just a single round in update() function Link to comment Share on other sites More sharing options...
Recommended Posts