1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

(Platformer) Camera scrolls down when player crouches

Discussion in 'Programming' started by Jar, May 16, 2019 at 5:18 AM.

  1. Jar

    Jar Member

    Joined:
    Jan 1, 2019
    Posts:
    6
    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
     
  2. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,350
    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)
     
  3. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    122
    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; 
    }
    
     
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,098
    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: May 16, 2019 at 8:42 AM
  5. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    122
    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)
     
  6. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,098
    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.
     
  7. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    122
    right... var xxxxx stays in the event thats calling it... vs xxxx in the create event stays in the object....


    my mistake ;o)
     
    RefresherTowel likes this.
  8. Jar

    Jar Member

    Joined:
    Jan 1, 2019
    Posts:
    6
    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 :)
     
    RefresherTowel likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice