I was working on a project and testing it. I added a section called “enemy sprite movement”. When I added the code for that section, much of my earlier code stopped functioning properly, yet no errors are shown in the code. I am including 2 links to the project- one with the new code and one without. I can’t figure out how the new code is breaking the old since they barely have any relationship.
Link to the project or level:
Project with new code (broken)
Project without new code (works)
What I expect to happen:
- When you run the game, you should be able to click on the central character (others are not ready yet). When you do so, the text “Choose Your Character” should disappear, a heart should appear beneath the character, and you should be able to move the character around the screen.
- The enemy sprite should begin moving toward the character.
What actually happens:
Before adding the code in the “Enemy Sprite Movement” section, it did as expected. After adding the “Enemy Sprite Movement” section, none of that happens. The text remains, no heart appears, can’t control the character, and the enemy sprite does not move.
What I’ve tried:
When I began the “Enemy Sprite Movement” section, I was using conditional “if” statements instead of “while” statement. It seemed to work at first. The character choice still worked, and the enemy sprite would move toward the player. However, it seemed to only acknowledge the player’s initial position, because once I began moving the player sprite, the enemy sprite just moved to the player’s original spot and stopped. Trying to fix that, I simply replaced some of the “if” statements with “while” statements. Once I did that, the entire thing seemed to fail. I’m not sure why it would break the player choice section at the beginning without any errors being shown.
I am very new to coding (although I have some experience with spreadsheet formulas). I know there may be much easier ways to get this to work and if that is the only way to make it work, I understand. However, I am also trying to learn and increase my understanding, so even if there is an alternative, an explanation about why this is causing problems would be extremely helpful. Also, if the “while” conditional is not what I should use, any advice on how it would be used properly would be appreciated. (I find the examples on code.org don’t do a very good job of highlighting the specific differences in similar commands like “for”, “if”, and “while”, or provide clear situational explanations of why one of these would be more appropriate.
You are doing really well for being new to coding! It is obvious you are skilled in logical and computational thinking.
Can you post the link to the game that is broke? When I click on either of the links above, they both go to the project that works and does not yet have enemy movement. I’d like to see what logic (conditions) you are using when coding for enemy movement.
Sorry about that. Here’s the correct link to the broken version:
So - I was able to get the game going again by:
- Removing the playerX and playerY variable and just using guardian.x and guardian.y when needing to test the enemy position. This is because the variables were not updating because the if statement they resided in was not true at all times.
- Changing all the while loops to if statements. While loops cause the code to continually loop INSIDE the loop until there is a false condition that allows it to break out. They are tricky because they can often cause an endless loop which is what happened in your program. An if statement is really what you had intended because you just want to test the condition for true or false and then change the velocity of the enemy and then allow the code to go to the next line in the program. In GameLab, I have used a for loop (a section of code that loops within itself a given number of times vs a while loop that loops within itself until a condition is met) outside of the drawLoop to draw a certain number of enemies.
Here is my remix version. You’ve built a great complex game and should celebrate that!
Thanks for the detailed reply. After reading your comments, I saw that the playerX and playerY variables were only being checked when the user clicked on the character.
Wanting to see if it was still possible to use the playerX and playerY variables (in case a situation arises where I need to indicate position for any character regardless of what was chosen at the beginning) and just to experiment to learn more, I decided to set the playerX and playerY variables to guardian.x and guardian.y in the movement section so it would update whenever a player pressed an arrow key.
I removed the enemy movement code for now, but I did change the “while” to “if” statements (which had been my first instinct, though I will explore to make sure I understand why your enemy sprite updates the character location, while mine did not when using “if” statements).
I did some checking with the debug tools, and it seems like moving the variable assignment works (at least the playerX and playerY variables show the correct updated values in the debugger).
(I thought I had an issue with text not appearing, but later realized I had simply forgotten to move the text section into the draw loop- I ended up creating a temporary variable called “TEXT_TEST” to use the debugger to check if the true or false condition was working correctly, which was a nice bit of effort to make sure I facepalmed as hard as possible when I realized the real issue.)
Here’s a link to the current situation if you’re interested, but you’ve helped with all I needed at the moment.
Updated playerX and playerY variables
As a side note, I also got some strange unexplained errors at one point and had to erase and start over with my backup. I had 2 nearly identical “if” statements and one was throwing up an unexpected token error while the other one seemed fine despite the syntax being identical and there were no missing variables. Additionally, I had an error appear on a “close” curly bracket that said "was expecting ‘(end)’ but got ‘}’ or something to that effect- however removing the bracket broke the “if” statement. It was strange, but putting in the backup code seemed to fix it.
Thanks again for taking the time to help! Your feedback has been excellent!
You bet! Glad it helped. It is a really neat project.