I have a platform game. It doesn’t have a game name yet; the file name is CatStuff and it’s a cat and a zoomba (kinda like a cross between a trademarked robot vacuum cleaner and a drone). I have some very cool code in my pocket for this game. Jumping. for example.
In Snap, you animate by changing costume and moving the sprite. My cat has 12 costumes—eight walking and four jumping. Walking animation is pretty simple. You cycle through the costumes and change the x-coordinate by an amount. depending on how fast you want to move. Jumping involves the y-coordinate as well and you have to do both at the same time. It’s tricky. Here’s my code to walk right.
You see the variable for the number of steps to move. That can be set for all the walking the cat does (and it figures into jumping too) in one place. That’s the point of having it there. -240 and 240 are magic numbers in Snap as they are the farthest left and right x-coordinates on the standard stage.
Now for jumping. Because jumping is not an even process, I couldn’t easily abstract it into a loop like I did walking. It’s long messy code. I don’t have comments in the code I’m posting here because it makes it difficult to share the images neatly.You’ll notice there’s an if-else at the bottom. This is to deal with repeating presses of the space bar that don’t resolve fluidly. The cat will fall if too high and if somehow it gets below ground, it’ll pop back up. That’s an in-case because I’ve not observed it, but it is theoretically possible.
Jumping is faster than walking but not twice as fast and I wanted a finagle factor so there’s a place to set one. I’m math-impaired (really) so my formulas aren’t always mathematically sound. They work for the game though.
Next time, I’ll talk about scrolling the scenery.