• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

(Platformer) Camera scrolls down when player crouches

J

Jar

Guest
Hello. I am trying to make it so that the camera scrolls down if the player holds down (crouching) for more than 1 second.

Code goes as follows:

Player Step event
Code:
//  crouch
    if ((dkey) && (onground) && (hsp==0))
{   
    sprite_index = pspcrouch;}
Camera Step event

Code:
if player.sprite_index=pspcrouch    
{ y = clamp(y+40,view_h_half,room_height-view_w_half); }
This was successful in making the camera scroll down as soon as the player crouches. My aim is to make it so that it only scrolls down if the player is holding crouch for 1 second.

This was my attempt at doing so:

Camera Step event
Code:
if player.sprite_index=pspcrouch
{   
        alarm[0] = room_speed;}
Alarm 0 event
Code:
    y = clamp(y+40,view_h_half,room_height-view_w_half)
So how do I go on about making this work?
Thank you in advance.

Cordially,
Jar
 

obscene

Member
The problem is that since this code runs each step, the alarm is continuously set to room_speed and so it is never allowed to count down. A fix might be to make sure the alarm is greater than -1. Another way might be to simply set the alarm in the player when setting the sprite (but if it's not already the crouch sprite)
 

woods

Member
would something like this work? ....keeping the homemade alarm in the step event


player step event
Code:
var  crouch_timer = 0;
if ((crouch_timer >30) and (player.sprite_index=pspcrouch))
{
crouch_timer ++;
}
else
{
with camera
    {
    y = clamp(y+40,view_h_half,room_height-view_w_half)
    }
crouch_timer = 0; 
}
 
No that wouldn't work @woods, as you are setting crouch_timer to 0 each step (also you are checking if crouch_timer is GREATER than 30, you need to check if it is lower instead). You can't use a local variable for this, you have to use an instance variable (this is in the player object):
Code:
// Create Event
crouch_timer = 0;
Code:
// Step Event
if (sprite_index == pspcrouch) {
   if (crouch_timer < room_speed*2) { // room_speed multiplied by a number gives you that number in seconds
      crouch_timer++;
   }
   else {
      with (camera_object) { // Replace camera_object with whatever your camera controller object is called
         y = clamp(y+40,view_h_half,room_height-view_w_half);
      }
   }
}
else {
   crouch_timer = 0;
}
 
Last edited:

woods

Member
could have swore i put this in create event.... dam my copier/paster must be broken
var crouch_timer = 0;

and i still have a bit of a time with > and < maybe im just catching dyslexia ;o)
 
You still cannot use var in the Create Event. The variable will only exist in the Create Event if you do that and if you try to reference it in the Step Event, GM will throw an error. It's important to understand (and remember) the difference between local, instance and global variables.
 
J

Jar

Guest
No that wouldn't work @woods, as you are setting crouch_timer to 0 each step (also you are checking if crouch_timer is GREATER than 30, you need to check if it is lower instead). You can't use a local variable for this, you have to use an instance variable (this is in the player object):
Code:
// Create Event
crouch_timer = 0;
Code:
// Step Event
if (sprite_index == pspcrouch) {
   if (crouch_timer < room_speed*2) { // room_speed multiplied by a number gives you that number in seconds
      crouch_timer++;
   }
   else {
      with (camera_object) { // Replace camera_object with whatever your camera controller object is called
         y = clamp(y+40,view_h_half,room_height-view_w_half);
      }
   }
}
else {
   crouch_timer = 0;
}
Many thanks; this worked like a charm.
I also managed to make it lower gradually by creating a local incrementdown variable which increases every frame, and make the camera clamp y+incrementdown instead of 40, then making it so that if incrementdown==40 {incrementdown--}

Thanks again :)
 
Top