官术网_书友最值得收藏!

  • GameMaker Cookbook
  • Brandon Gardiner
  • 541字
  • 2021-07-30 09:52:49

Creating 2D movement

In using GameMaker's drag and drop interface. It involved a lot of blocks. Next, we will create more sophisticated movement code in only a handful of code blocks.

Getting ready

You'll need a character object (obj_player) and a room where you can place it. We won't be discussing the object's sprite or animation here; this is purely about moving your character in two dimensions on the screen. For this reason, we're using a simple block to represent our player character.

How to do it...

  1. In obj_player, add a Create event.
  2. Drag and drop an Execute Code block (under the Control tab) into the Actions box.
  3. Open it and enter the following code:
    ///set variables
    max_spd = 12;
    accel_spd = 1;
    decel_spd = 2;
  4. Close this code block and add a Step event.
  5. Drag a code block to the Actions box and enter the following code:
    ///control movement
    if (keyboard_check(vk_right)) and not (keyboard_check(vk_left))
    {
    hspeed += accel_spd;
    hspeed = min(hspeed, max_spd);
    } 
    if hspeed>0 and not (keyboard_check(vk_right))
    {
    hspeed -= decel_spd;
    }
    if (keyboard_check(vk_left)) and not (keyboard_check(vk_right))
    {
    hspeed -= accel_spd;
    hspeed = max(hspeed, -max_spd);
    } 
    if hspeed<0 and not (keyboard_check(vk_left))
    {
    hspeed += decel_spd;
    }
    if (keyboard_check(vk_left)) and (keyboard_check(vk_right)) or not (keyboard_check(vk_left)) and not (keyboard_check(vk_right))
    {
    hspeed = 0;
    }
  6. Close this code block and you're done.

Note

Placing /// in front of the first line of code in a code block not only works like a comment (the compiler ignores it), it becomes the title of the code block and even appears in the Actions box. This is a great way to keep track of what each code block does.

How it works...

In the Create event, you need to set up a few variables that will be used only by your player character. These variables are then used in the control movement code block. This code tells GameMaker to check (every step) which keys are being pressed. If the right arrow key is being pressed but the left arrow key is not, the horizontal speed becomes equivalent to itself plus the value of acceleration (accel_spd) until the maximum speed (max_spd) is reached. Once the maximum speed is reached, if the player continues to hold the right arrow key, the character will continue to move at this speed. If the right arrow key is released, as long as the horizontal speed is greater than 0, the value of deceleration (decel_spd) is subtracted. This allows the character's acceleration and deceleration when moving, which is more realistic than jumping from 0 to top speed in an instant and vice versa. The last bit of code is simply used to make sure that if both left and right arrow keys are pressed simultaneously, the character will not move at all.

There's more...

The values used for each variable in this recipe are arbitrary, and you can choose your own values for each. If you're calculating a movement like this, I recommend that you play around with these values and playtest the results in order to make the character move in the way you want it to move.

See also

A basic character movement using GameMaker's drag and drop interface is discussed in Chapter 1, Game Plan – Creating Basic Gameplay.

主站蜘蛛池模板: 肃北| 内江市| 晋宁县| 鲜城| 安西县| 河池市| 松溪县| 漠河县| 新源县| 济宁市| 陆丰市| 安塞县| 泸溪县| 阳原县| 宜川县| 门源| 乌拉特前旗| 抚宁县| 稻城县| 宁海县| 通江县| SHOW| 平陆县| 嵊州市| 东兰县| 中江县| 固始县| 广东省| 上蔡县| 慈利县| 徐闻县| 双牌县| 宁阳县| 靖安县| 泰安市| 吴桥县| 邯郸市| 离岛区| 吉木萨尔县| 灵台县| 凯里市|