I have a student who is working on his game and trying to code a sprite so that whichever direction the sprite is facing, it can fire a projectile in that direction. I have no clue what he should do. I’ve tried a couple things that haven’t worked, so I thought I’d put it out to the CSD community. Can someone help me? Here is his current project: https://studio.code.org/projects/gamelab/5stCjFjG46hARHnnsmloi-kcuOc1jKWc6yBjfrXNPec
There are a couple of ways that he can do this, and it looks like he’s chosen to check the visibility property of one of the sprites in a conditional, then set the velocity of the shot accordingly (lines 56-60).
The main problem is in lines 61-63. Right now they are always run, so even though the if statement sets the velocity to -5 when the player is visible (and the space bar is pressed), lines 61-63 will always set it back to 5. Instead, lines 61-63 should only run when the condition in line 56 is not true. Your student can fix this either by using an else in the conditional and putting those lines inside, or by creating a new conditional that checks whether the other sprite is visible.
I had a related issue with a “space-invaders-style” game. I could create a projectile, but had trouble creating multiples and having each of them behave independently. I came up with a solution using groups and FOR loops to handle the issue.