Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Stopping infinite loop when taking damage

Years of Service
User Offline
Joined: 14th Mar 2016
Posted: 31st May 2016 14:13

I am attempting to write my first program, up to this point I have only done guided tutorials and the like to learn the basics of computer science in general, and I feel like I have a passing grasp on what I am doing. But I have gotten stuck and try as I might I can't find the answer searching the forums, so here I am.

I am not at home right now so I will put the code as best I can remember here

This is just part of the whole code, but the problem that I am having is that when the damage is dealt it continues to loop every cycle.

I know that I can exit out of the do/loop but without a next loop it just exits the program, and I want to be able to get back to this do/loop after the damage is resolved so that you can choose your next attack.

Any help would be greatly appreciated.

29 games
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 5th Jun 2016 18:00
The reason the way the player's and boss's health keeps decreasing is because you set "player_choice = 1" and "boss_choice = 1" when the button is released but there's nothing that resets either of these so they always equal 1. At this point the if statement "if player_choice = 1 and boss_choice = 1" will always be true so the health values will always decrease.

You need to reset player_choice and boss_choice to some other values - say zero - within the loop and before the button press.

I've modified your code so you can see what I mean, use the left mouse button.

Hover Car Race Challenge! - available now on Google Play
Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 5th Jun 2016 18:31 Edited at: 5th Jun 2016 18:33
I don't get this part of it:

boss_roll = random(1, 100)
if boss_roll > 1 and boss_roll <= 100

First create a random value between 1 and 100, and then if it is between 1 and 100 do stuff?! The likelyhood of that happening is exactly 1:1, so might as well drop the if statement. though of course, you could be using the boss_roll result at some other part of your code we're not seeing.

Also, I am assuming at some time you reach an end-state in your program, where you'd like it to exit to some welcome screen or menu screen. The Do-Loop is not entirely suited for this, you'd be better off with a While - endWhile construct. For instance, let's say you exit upon player health being 0, it would look something like this:

Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 7th Jun 2016 15:34 Edited at: 7th Jun 2016 15:35
Quote: "The likelyhood of that happening is exactly 1:1,"

No it isn't.
There is a 1 in 100 chance that the random number could equal 1 and in that case the check would fail.
AGK V2 user - Tier 1 (mostly)
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 10th Jun 2016 07:42
LOL, you're right. I must not have gotten my recommended dose of coffee when I wrote that

Login to post a reply

Server time is: 2023-06-05 04:23:02
Your offset time is: 2023-06-05 04:23:02