Tuesday 10 September 2013

Basic movement and jumping

Movement
The image below shows a basic code of movement to the right:
Code for Movement

Now on to reading and explaining the code. The first tile denotes when will the code start running, in our case, after the green flag is clicked to start the game. If... Then creates a condition for the following commands to happen. Our condition is ''Right Arrow key pressed'' meaning that the right arrow key in the keyboard will be the on responsible for starting the movement to the right sequence. Point in N Direction rotates our sprite so it is moving to the correct way. The 90 degrees indicated is in relation with the Y in a 2 dimensional axis. Move N Steps tells the sprite how much it is going to move in the X axis. Wait N secs tells the code to wait any number of seconds before executing the next line of code, which is Next Costume. This scrolls through the different costumes of the sprite. These costumes are basically our sprite in a different pose so it creates a nice and smooth movement animation. And finally Forever tells the game to repeat this process for the duration of the whole game. 

Notes:
  • The angle of direction chosen will dictate which direction your character will move
  • Setting a ''wait time'' before changing costume creates a more fluid movement. Experiment with various numbers to find a fitting time
  • Changing the costume is very important, it changes the shape your sprite is going to take after you press the preset key to move
The process to make the character move left is the same, the only difference being the direction it will be pointing, -90.

Movement to the left

Jumping

The coding for jumping utilizes the same tools as the movement with a new condition, our character sprite has to be touching the ground to enable the jump. The Ground is a sprite created as a simple black ground to simulate a real environment . Again we set a key to execute this action, the Up Arrow key. The Switch Costume to command has been added only for the cosmetic purpose of using a costume where the sprite seems like he is jumping, or in the air. The "real jump" happens on Change Y by N, this moves the character to the position 150 on the Y axis, putting him in the air. After that happens, we need to set a "rule for gravity" so our character comes back to the ground. That is where Change by N comes in and slowly resets our character back in the Y axis. Again we add a Forever command so it is present for the whole game

Jump block

You can play this project here:

No comments :

Post a Comment