docx

advertisement
University of Southern California
Create the Character Sprite
If we’re going to add a character, we need to setup the sprite that will be visible
in the game.
 Right click on Sprites and select New Sprite.
 Give the sprite a name so it can be referenced later. Try using
spr_player.
 Make sure that the box labeled Precise Collision Checking is NOT
checked.
 Click on Load Sprite and pick character_right.png.
Create the Object for the Character
To include the character into the world we need to create an object for it.
 Start by right clicking on Objects and selecting New Object. Name the
object obj_player.
Add Events
If the character is going to doing anything interesting, There needs to be ways
for the player to interact with it and ways for it to interact with the
environment. To do this, we’re going to add events that are triggered when the
player performs some action or some in game conditions are met. For this
game, the arrow keys will be use to move the character around. In addition, our
character will also need to do collision detection checks so it doesn’t fall through
the floor or run through walls. All of this is still done in the object creation
popup.
Pedagogical GAMES @ www.isi.edu/pedtek/pedgames
Sponsored by the U.S. National Science Foundation
Copyright © 2012 University of Southern California
1
Add Left Arrow Event
This event will handle the user pressing the left arrow. It will move the
character to the left if the space is available.
 First, lets add the event for when the user presses the left
arrow key. Click on the Add Event button, then select
Keyboard, and finally choose Left.
 Go to the control tab on the right. Locate the Check Empty
action and drag it into the Actions pane.
 In the new window that opens, enter -4 for the x value.
 Check the box labeled Relative. This ensures that our
calculations are relative to the current objects coordinates
instead of the global game coordinates.
 Go to the move tab on the right and drag the Jump to
Position icon into the Actions pane.
 In the new window, set the x value to -4 and check the
Relative box.
 Now, whenever the user presses left the game will do two
actions. First, it will check if the location four units left is
filled with an object. If it isn’t, then it will move the player into
the location four units to the left.
Add Right Arrow Event
 We also need an event for moving the character to the right. Create the
right arrow event the same way as the left arrow event. The only difference
is that Right should be exchanged for Left and 4 should be exchanged for
-4.
Pedagogical GAMES @ www.isi.edu/pedtek/pedgames
Sponsored by the U.S. National Science Foundation
Copyright © 2012 University of Southern California
2
Add Up Arrow Event
Pressing the up arrow will make the character jump. This will be done by first
checking for an obstacle above the character and then setting the vertical speed
to some value if the space is clear. Remember that a positive vertical value make
something move down the screen. So a negative value will be needed to make
the character jump.
 Start by clicking on Add Event > Keyboard > Up.
 In the control tab, drag out the Test Expression icon.
 For the expression type the following: vspeed == 0
 Now go to the control tab and drag the Check Collision icon
out to the Action pane.
 Set the y value to 1 and check the Relative box.
 Now go to the move tab and drag the Speed Vertical icon to
the Action pane.
 Set the vert. speed option to -10. If you want to adjust how
high the character can jump, try changing this value.
Save and Run Your Game
Test the left and right arrow keys first. Try running into a wall. What happens?
Then test the up key. What does the player do? What does the real world have
that we don’t have in our game?
Pedagogical GAMES @ www.isi.edu/pedtek/pedgames
Sponsored by the U.S. National Science Foundation
Copyright © 2012 University of Southern California
3
Add Collision Detection
Our character is going to run into obstacles when we move it around. To check
for obstacles, walls, and platforms we need to add a collision event. Remember
that we marked obstacles, walls, and platforms with obj_block. Our event will
check for collisions with obj_block and make sure we don’t move through them.
 Start by clicking Add Event > Collision > obj_block.
 Go to the move tab and drag out the Move to Contact icon.
 Set the direction option to direction. Enter 12 for the
maximum.
 Find the Speed Vertical icon under the move tab and drag it
into the Actions pane.
 Set the vert. speed to 0.
Add Step Events
There are certain checks that should be run every frame of our game. For
instance, gravity needs to be applied each time we draw a frame in our game.
Let’s add the step events now.
 Add a new step event by clicking Add Event > Step > step.
 Go to the control tab and drag the Check Empty icon out to the
Actions panel.
 For y, enter 1. Make sure that the Relative box is checked.
 Go to the move tab and drag in the Set Gravity icon.
 For direction, enter 270 and 0.5 for gravity. Can you think of
why we would set the direction to 270 for gravity?
 In the control tab, drag out the else icon.
 Go to the move tab and drag the Set Gravity icon again.
 For direction, enter 270 and 0 for gravity.
Pedagogical GAMES @ www.isi.edu/pedtek/pedgames
Sponsored by the U.S. National Science Foundation
Copyright © 2012 University of Southern California
4
 Go to the control tab and drag the Test Variable icon out.
 For variable, enter vspeed. For value, enter 12. For
operation, enter larger than.

 Go to the move tab and drag the Speed Vertical icon out. For
value, enter 12. This will make sure that the player doesn’t
exceed a maximum speed from falling. Otherwise, the character
would constantly accelerate when falling.
 Now go to your room and select the objects tab. Find obj_player
in the dropdown and place it somewhere in your room. This will
be the player’s starting location.
Run Your Game
 You should now have a character that you can move around with the arrow
keys. Pressing the up arrow should make your character jump. Your
character shouldn’t fall through the floors or be able to move through
walls.
Pedagogical GAMES @ www.isi.edu/pedtek/pedgames
Sponsored by the U.S. National Science Foundation
Copyright © 2012 University of Southern California
5
Download