Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Double tap running  (Read 1866 times)

0 Members and 1 Guest are viewing this topic.
Double tap running
« on: May 28, 2007, 06:04:56 am »
  • The king of Awesome
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 198
How it works.
Ok I double tap the directional pad and then I hold it and the character runs and the sprite changes into a running sprite.

The walking script.

Create Event
Code: [Select]
global.facing="D"
image_speed=0.5
moving=0
movestep=0
movespeed=3   // Link's walking speed, pixels per step
Step Event
Code: [Select]
// LINK MOVEMENT SCRIPT by GOODNIGHT
var fholdd, fholdu, fholdl, fholdr, cc;

// Check keys
holdd=keyboard_check(vk_down)
holdu=keyboard_check(vk_up)
holdl=keyboard_check(vk_left)
holdr=keyboard_check(vk_right)
// Cancel opposing keys
if holdu && holdd {
    holdu=0
    holdd=0
}
if holdl && holdr {
    holdl=0
    holdr=0
}


// This is where you may want to add other conditionals which keep you from [CODE]moving,
// such as if you are using an item, or you are in a menu.
// Just add the conditions to the end of this next "if" line.
// example:
// if (holdd ||holdu || holdl || holdr) && !usingshield {

if holdd ||holdu || holdl || holdr {
    // Change sprites if you haven't already
    if !moving {
        if holdd { sprite_index=sprLinkRunD; global.facing="D" }
        else if holdu { sprite_index=sprLinkRunU; global.facing="U" }
        if holdl { sprite_index=sprLinkRunL; global.facing="L" }
        else if holdr { sprite_index=sprLinkRunR; global.facing="R" }
    }
    moving=1
    // Sprite correction
    if (holdd && !holdl && !holdr) { sprite_index=sprLinkRunD; global.facing="D" }
    else if (holdu && !holdl && !holdr) { sprite_index=sprLinkRunU; global.facing="U" }
    else if (holdl && !holdd && !holdu) { sprite_index=sprLinkRunL; global.facing="L" }
    else if (holdr && !holdd && !holdu) { sprite_index=sprLinkRunR; global.facing="R" }
    if (holdd && holdl && sprite_index!=sprLinkRunD && sprite_index!=sprLinkRunL) { sprite_index=sprLinkRunL; global.facing="L" }
    else if (holdd && holdr && sprite_index!=sprLinkRunD && sprite_index!=sprLinkRunR) { sprite_index=sprLinkRunR; global.facing="R" }
    else if (holdu && holdl && sprite_index!=sprLinkRunU && sprite_index!=sprLinkRunL) { sprite_index=sprLinkRunL; global.facing="L" }
    else if (holdu && holdr && sprite_index!=sprLinkRunU && sprite_index!=sprLinkRunR) { sprite_index=sprLinkRunR; global.facing="R" }
} else { moving=0 }

if moving {
    // Determine the delay between movement steps
    // (or how many times to move per step).
    if (holdd || holdu) && (holdl || holdr) { movestep+=(movespeed/sqrt(2)) }
    else { movestep+=movespeed }
    // Start doing the movement.
    while movestep>=1 {
        movestep-=1
        fholdd=0; fholdu=0; fholdl=0; fholdr=0;
        // Corner-cutting loop.
        // Feel free to change the initial "cc" variable for corner sensitivity.
        // Between 4 and 8 is recommended, use 0 for none.
        for (cc=6; cc>0; cc-=1) {
        if holdu {
            if !place_free(x,y-1) {
                if place_free(x-cc,y-1) && !(holdr || fholdr) { fholdl=1 }
                else if place_free(x+cc,y-1) && !(holdl || fholdl) { fholdr=1 }
            }
        } else if holdd {
            if !place_free(x,y+1) {
                if place_free(x-cc,y+1) && !(holdr || fholdr) { fholdl=1 }
                else if place_free(x+cc,y+1) && !(holdl || fholdl) { fholdr=1 }
            }
        }
        if holdl {
            if !place_free(x-1,y) {
                if place_free(x-1,y-cc) && !(holdd || fholdd) { fholdu=1 }
                else if place_free(x-1,y+cc) && !(holdu || fholdu) { fholdd=1 }
            }
        } else if holdr {
            if !place_free(x+1,y) {
                if place_free(x+1,y-cc) && !(holdd || fholdd) { fholdu=1 }
                else if place_free(x+1,y+cc) && !(holdu || fholdu) { fholdd=1 }
            }
        }
        }
        // End corner-cutting loop.
        // Determine the movement direction, and move.
        xstep=(holdr || fholdr)-(holdl || fholdl)
        ystep=(holdd || fholdd)-(holdu || fholdu)
        if place_free(x+xstep,y) { x+=xstep }
        if place_free(x,y+ystep) { y+=ystep }
    }
} else {
    // Get the right sprite when you stop moving.
    switch sprite_index {
        case sprLinkRunD: sprite_index=sprLinkStandD; break;
        case sprLinkRunU: sprite_index=sprLinkStandU; break;
        case sprLinkRunL: sprite_index=sprLinkStandL; break;
        case sprLinkRunR: sprite_index=sprLinkStandR; break;
    }
    movestep=0
}

depth=-y
// end of script

Thanks![/code]
Logged

Goodnight

Once and future Captain
Re: Double tap running
« Reply #1 on: May 28, 2007, 08:52:31 pm »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
That's just an old version of my movement script ... are you making a request? If so, there are some more details you could include:
- Do you need to tap twice in the same direction, or any 2 directions in a row?
- Can you change directions while running, or only go in a straight line?
- Can you only run for a limited time? If so, for how long?
- Will there be stamina or something that needs to regenerate before you can run again?
Logged
Re: Double tap running
« Reply #2 on: May 28, 2007, 11:37:14 pm »
  • The king of Awesome
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 198
I need to double tap the direction I'm going to run, he'll/she'll run until I let go of the button.
Logged

Goodnight

Once and future Captain
Re: Double tap running
« Reply #3 on: May 29, 2007, 11:35:20 pm »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
Okay, that sounds good. I had to ask because I've done some different requests like this before.

You'll need a variable in Create for whether or not the character is running. May as well call it running (=0).

Because there should only be a short time to double-tap within, you'll need an alarm that starts (gets set to about 1/5 of a second) when you tap an arrow the first time. Since this is pretty simple, you could even use a pseudo-alarm to keep those precious real alarms free. So make another variable in Create called runtap (=0).

A pseudo-alarm is easy, just make the variable decrease by 1 every step, and make sure it doesn't go below -1.

To detect taps and releases, you could use keyboard_check_pressed() and keyboard_check_released() in the Begin Step event, although I don't think those functions are in all versions of GM. Or you could use Key Press and Key Release events. Same difference.

When you tap a key, let's say Left for example, two things should happen:
1) If the alarm is still active (runtap>0), meaning it's been less than 1/5 of a second since you tapped before, make the running variable true. You'll also want to first make sure the previous tap was also Left - do that by checking if global.facing is "L".
2) Set the alarm (runtap=room_speed/5), so the script knows you've just made a tap.

When you release Left, running should become false, as long as you were running left. (check global.facing again)


Now to make that running variable actually do something...
movespeed is the variable that tells how many pixels per step the player moves. So in the Step event, make it a higher number if running is true, or the normal number otherwise.
Using a different sprite is easy, as long as you have the latest version of the movement script, since I optimized the code. I'll put that in my sig really soon. Just look near the bottom of the Step event where the walking sprite is chosen depending on the direction. Just make it choose from a different set of sprites if running is true.
Logged
Re: Double tap running
« Reply #4 on: May 30, 2007, 04:22:23 am »
  • The king of Awesome
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 198
Can ya make a lil demo, I'm really not understanding allot of what you writtin.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.224 seconds with 48 queries.

anything