Sprite Movement not working?

Hi all

I trying to help out someone, but can’t seem to understand why “sprite2” doesn’t move. When I press the right key down it moves the sprite but goes back to its original spot.

@Tim

There could be a few things going on here, but it’s a little hard to tell without having the program to play with. You can always click on the “share button” and get a URL that you could send us to look at and then we can tell for sure what is going on, but a few observations.

  1. Creating sprites inside the draw loop can definitely cause problems. You are doing that in several different spots inside the draw loop. It’s best practice to create them outside of the draw loop and then make them invisible, but then make them visible when you want them to come into play. Creating a sprite inside the draw loop can cause the same sprite to be created hundreds of times and then the behavior isn’t always predictable. My guess is that you create the sprite, it moves and then it is being recreated at the starting point again due to it being inside the draw loop.

  2. The drawSprites( ) block is pretty high in your code. That may not be the issue, but that would be the 2nd thing I would look at, personally, if #1 above doesn’t fix the problem. It usually works best towards the bottom of the draw loop (after your conditional blocks). Again, there are definitely exceptions to that, but it’s hard to tell without being able to experiment with the code.

So, if you play with those two things and still don’t get it working the way you think it should, definitely click on the share button and send us a link and we are happy to look it over.

thanks!

Mike

@mwood
Thanks for the help. Here is the link to game.

@Tim,

Creating the sprites before the draw loop and setting their visible property to false and then making them visible where you are now creating them will work.

Good luck!

Mike