GameMaker Noob Problems: Platformers and Wall Jumping [Solved]

EricPB

Member
SO, I am a dedicated artist who has decided to try and learn programming myself so i can at least try to make something worthwhile with both skills sets... and so far... i am actually having a lot of fun learning and problem solving!

But there are some issues I cant seem to wrap my head around by trying to look into it myself, and thus I turn to trying and asking the community a direct question hoping to get a direct answer. So here we go...

I followed a shaun spawlding video tutorial to learn the basics, and managed to cobble together my own platformer where I was able to figure out a few things on my own from there (which im proud of being able to do, yay!) but I get stuck at trying to create a more fleshed out wall jump.

I have made the ability to jump up walls, and had i the desire to i could tie it to a limited number of jumps but that function is no longer in the code. The character more or less directly shimmies up the wall where what I want is for them to stick to the wall a bit, slow down and by simple hitting jump again they pop up and out away from the wall. From everything ive read there needs to be a new system made with friction and acceleration which my original coding doesnt have... which leads to my question.

Will I need to make a new system (start over) to allow this kind of movement, or can I work off what I have already in as non-destructive of a way as possible and integrate friction and acceleration- OR is there a way for me to properly achieve this without rewriting anything-?

my code in its entirety is found below, keep in mind some parts ARE commented out because as a noob... i was experimenting.


Create:
hsp = 0;
vsp = 0;
grv = 1.8;
walksp = 8;
//fric = 1;
//Can_Jump = true;
Step:
/// Player Movement and Collision
//

//Get player input
key_left = keyboard_check (ord("A"));
key_right = keyboard_check (ord("D"));
key_up = keyboard_check (ord("W"));
key_jump = keyboard_check_pressed(vk_space);
key_jrelease = keyboard_check_released(vk_space);
//key_jumphold = keyboard_check_direct(vk_space

//Check Collisions Shortforms
Grounded = (place_meeting(x,y+1,oWall));
InAir = (place_meeting(x,y-1,oWall));
Wall_R = (place_meeting(x+1,y,oWall));
Wall_L = (place_meeting(x-1,y,oWall));

//Additional Player Factors
Clung = Wall_R || Wall_L;
//jumpsp = vsp += Accel * grv;
//jump_number = 2; //How many jumps the player can make
//jump_current = 0; //How many jumps the player has remaining

//Calculate movement
var move = (key_right - key_left);
hsp = move * walksp;
vsp = vsp + grv;

//++Variable jumping
if Grounded && key_jump && Can_Jump
{
vsp = -20;
Can_Jump = false;
}

if key_jrelease and (vsp) < 0
{
vsp = vsp * 0.2;
}


//Double Jumping
//if key_jump && jump_current > 0
//{
// jump_current = jump_number - 1;
// vsp = -20;
//}

//Horizontal Collision for Walls
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall))
{
x += sign(hsp);
}
hsp = 0;
Can_Jump = true
}

x += hsp;

//Veritcal Collision for Walls
if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall))
{
y += sign(vsp);
}
vsp = 0;
Can_Jump = true
}

y += vsp;


//++Wall Clings?
if Clung
{
vsp = vsp * 0.8;
}

//Trying to make it so that holding the key in the direction of the wall youre
//colliding with, you will descend slower
//if Clung && vsp > 0 && !key_right || !key_left
//{
// vsp = vsp * 3 ;
//}

//Animations
if (!Grounded)
{
sprite_index = s_PlayerF
image_speed = 1;
if (sign(vsp) > 0) image_index = 0; else image_index = 1;
}

else

{
image_speed = 1;

if (hsp == 0)
{
sprite_index = s_Player
}
else
{
sprite_index = s_PlayerR
}
}

if (hsp != 0) image_xscale = sign(hsp);

Thanks so much for any help you may want to contribute, rly, i couldnt be learning so easily if it wasnt for other peoples patience.
 
M

MatthewL019

Guest
From the code you have presented it looks like you've already implemented a friction based movement of sliding down a wall?
 

Simon Gust

Member
Before you can start writing more code, it would be essential to organize your code. There is a general rule to this.
first, input
second, motion
third, collision
fourth, animation

Animation is good, but some collision related variable are before motion.
You could structure these into code blocks
Code:
//Get player input
key_left = keyboard_check (ord("A"));
key_right = keyboard_check (ord("D"));
key_up = keyboard_check (ord("W"));
key_jump = keyboard_check_pressed(vk_space);
key_jrelease = keyboard_check_released(vk_space);
//key_jumphold = keyboard_check_direct(vk_space
I wrote a simple wall slide / jump mechanic
Code:
// Calculate movement
move = (key_right - key_left);
hsp = move * walksp;

// Variable jumping
if (Grounded && key_jump)
{
 vsp = -20;
}

if (key_jrelease && vsp < 0)
{
 vsp *= 0.2;
}

// Wall Clings
max_grv = 40.0; // set gravity to normal
if (Clung && !Grounded)
{
 max_grv = 14.0; // set gravity lower

 // wall jump
 if (move != 0) // hold movement key towards the wall for a proper wall jump
 {
  max_grv = 9.0; // set gravity even lower
  if (key_jump) // press jump
  {
   hsp = -8 * move;
   vsp = -20;
  }
 }

 vsp = min(vsp, max_grv); // reduce vsp on the wall
}

// apply gravity
if (vsp < max_grv)
{
 vsp += grv;
}
if (hsp != 0) image_xscale = sign(hsp);
Code:
//Horizontal Collision for Walls
if (place_meeting(x+hsp,y,oWall))
{
 while (!place_meeting(x+sign(hsp),y,oWall))
 {
  x += sign(hsp);
 }
 hsp = 0;
}
x += hsp;

//Veritcal Collision for Walls
if (place_meeting(x,y+vsp,oWall))
{
 while (!place_meeting(x,y+sign(vsp),oWall))
 {
  y += sign(vsp);
 }
 vsp = 0;
}
y += vsp;

//Check Collisions Shortforms
Grounded = (place_meeting(x,y+1,oWall));
InAir = (place_meeting(x,y-1,oWall));
Wall_R = (place_meeting(x+1,y,oWall));
Wall_L = (place_meeting(x-1,y,oWall));
Clung = Wall_R || Wall_L;
Code:
//Animations
if (!Grounded)
{
 sprite_index = s_PlayerF
 image_speed = 1;
 if (vsp > 0) image_index = 0;
 else image_index = 1;
}
else
{
 image_speed = 1;
 if (hsp == 0)
 {
  sprite_index = s_Player
 }
 else
 {
  sprite_index = s_PlayerR
 }
}
Oh, and don't forget to add some of these variables in the create event.
 

EricPB

Member
So after reading over your stuff Simon and implementing it, it gives me preeeettty much what I want. It's kind of snappy and not as smooth, like the character doesnt seem to follow an arc but a diagonal but its a start! So thanks this is awesome.

One more question on the point of animation, you can see here below that ive changed the sprite work so that when moving downward, Image 0 from s_PlayerF is shown, and when moving up I am trying to get an animated (8 frames, so 0-7) sprite to play for when the player is moving up, but when i jump only the first frame (or in a different version of this code, the first two frames) actually show. What am I doing wrong?

btw the animated sprite is s_PlayerJr

Code:
//Animations
if (!Grounded)
{
 sprite_index = s_PlayerF
 image_speed = 1;
 if (vsp > 0) image_index = 0;
 else sprite_index = s_PlayerJr;
}
else
{
 image_speed = 1;
 if (hsp == 0)
 {
  sprite_index = s_Player
 }
 else
 {
  sprite_index = s_PlayerR
 }
}
 

Slyddar

Member
If s_PlayerF only has one frame what may be happening is you are setting s_PlayerF every frame, so the image index, which is being increased by 1 every step, is then getting reset to 0 when you reassign the sprite with sprite_index = s_PlayerF.

Try this;

Code:
 if (vsp > 0)
 {
   sprite_index = s_PlayerF
 }
  else
 {
   sprite_index = s_PlayerJr;
   image_speed = 1;
 }
 

EricPB

Member
Yep this all works, I see now how I was clumping code together weirdly, thanks all hopefully anything else I get stuck on ill be able to figure out myself, thanks for the help!!
 
N

NeonBits

Guest
Before you can start writing more code, it would be essential to organize your code. There is a general rule to this.
first, input
second, motion
third, collision
fourth, animation
Let say I've wrote something correctly but didn't think of that, what follows?
 

Simon Gust

Member
Let say I've wrote something correctly but didn't think of that, what follows?
Not all to much, mostly the animations might not be displayed correctly. It's mostly under the hood.
for example you had animation before collision and in your collision you check if the player is on the ground.
Now in the animation it should display the idle or running animation but it only does so next frame because at the time Grounded registers, the animation has already been done for that frame.
 
Top