Which type of Game maker do you use?

Monday, June 9, 2014

Moving With Code

Moving... pretty much required in games right? Well the first tutorial I showed you you needed 8 different events. Well after watching a few youtube videos I found one that had tought me how to move with only 1-2 event(s). The step event has finally proved itself to me. This tutorial requires coding, but you could just copy & paste if you aren't fast at typing which I'm not. Alright lets get started.
STEPS (no pun intended)
1.)You need 2 objects, Obj_guy(player. I like to call it obj_guy because it's shorter) and an obj_land.
2.)obj_land = solid
3.)Under obj_guy make a step event. Execute code.
Type this

if place_free(x,y+.5){gravity = 0.5}                                            //gravity
else{gravity = 0} gravity_direction = 270

if vspeed > 10 {vspeed = 10}                                                    //limit speed

if keyboard_check(vk_up) && !place_free(x,y+.5) {vspeed -= 10}               //move controls
if keyboard_check(vk_left) && place_free(x-5,y){x-=5}
if keyboard_check(vk_right) && place_free(x+5,y){x+=5}


Okay so the first 2 lines,(before "//limit speed") this is setting the gravity. so if there is no object below you in .5 pixels to set the gravity to 0.5(kinda real life gravity). 2nd line: if there is any thing below you in .5 pixels to set the gravity to 0(no gravity) The gravity_direction = 270 part is just telling the gravity to go downwards.

if vspeed > 10 {vspeed = 10}  This is just setting a speed falling limit so your character doesn't go like 300,000 mph down a hole and break through the ground (it's possible, trust me).

Okay lets start with going right.
the first part, if keyboard_check(vk_right)  this is telling gamemaker to see if the right arrow key is pressed then do the following actions and if not pressed, then stop. In gamemaker, when you want to add or tie something together, you use 2 & signs. the second part, && place_free(x+5,y){x+=5}     this part has the place_free command also. This time it asking if there is anything 5 pixels to the right then move right 5 pixels. Also if you didn't know adding the + or - sign next to the = sign makes it relative. It's Pretty close to the opposite direction just change the + signs to minus and of course add vk_left.

The jumping is also close. the first part is the same. the && signs. something that is different is the ! sign. This won't work without the ! sign!
(x,y+.5) {vspeed -= 10} the first part of this is asking if there's anything below you in .5 pixels, if so vspeed -=10(jumping).

Now that was only the 1st event, the second event is collision with obj_land.

1.) execute code:

if vspeed >= 0 && !place_free(x,y+vspeed) {move_contact(270)} vspeed=0
Okay this code is a little confusing, I still don't know 100% of it.
If vspeed >= 0 and if there is a place free under you while jumping then go downwards(270) and vspeed = 0.

Well that's about it, You should compare this moving with my old moving. much less crowded. Hope you learned something and if you did, tell your friends or anyone that plays gamemaker. I also have a sandbox.yoyogames.com account named pop12601. I put up some games I'm working on and some old games I found. Thanks for reading. Bye!
Happy gaming!

No comments:

Post a Comment