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: [Help Req] Why the hell...?  (Read 1488 times)

0 Members and 1 Guest are viewing this topic.

Dark-Hylian

Silence
[Help Req] Why the hell...?
« on: January 21, 2011, 08:47:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 370
     In a small project of mine, I'm attempting to code a side scrolling shooterific game for a friend. I've coded to the best of my ability, but... the code seems to have incredible issues with my choice of arrangement.

     In the code below, state doesn't change for anything except ducking, in which it changes properly. All of ducking works fine, save that you can still jump while doing it, which is a byproduct of the .state not changing. Likewise, the jump sprite is displayed for 1/32 of a second when you do jump, but the state doesn't change. While you walk, it doesn't set the sprite, and for the life of me, I can't figure out why. It's probably some dumb mistake with if else statements that I missed, but I'm completely stumped. Would any of you care to take a shot at it?

Player Step Event
Code: [Select]
if paused=false
{
// --------------- JUMPING ----------------
gravity_direction = 270;// Sets the direction of the pull of gravity. 270 = down.
//
if place_free(x,y+1)//
    {
    gravity = 0.5;// set gravity to .5
    }
else //otherwise
    {
    gravity = 0;// set gravity to 0
    }

//  ------- JUMP SPEED --------
if vspeed > 10
    {
    vspeed = 10;
    }
//
// -------- JUMP KEY ---------
//
if keyboard_check(vk_up) and !place_free(x,y+1) and state = 0 // If you're pressing down, and not busy...
    {
    state = 1; // State = 1
    jumping = true; // And jumping = true
    vspeed = -6;
    }
else // Otherwise...
    {
    state = 0;// You're not busy
    jumping = false;// You're not jumping.
    }//
if state = 0
{
jumping = false;
}
//
if jumping = true // If you are jumping...
    {   
        if facing = 90 // and if you're facing Right...
            {
            sprite_index = sprPlayerJumpR; //Sprite = jump Right
            }
        else if facing = 180 // Otherwise if you're facing Left...
            {
            sprite_index = sprPlayerJumpL; // Sprite = jump Left
            }
}

//
// --------------- MOVING RIGHT AND LEFT ----------------
//
if keyboard_check(vk_right) and place_free(x+4,y)// If you're pressing Right, and there's nothing to your right...
    {
    x += 4; // Move to the right some.
    facing = 90; // Facing becomes Right.
    moving = true; // And you start moving.
    }
else // Otherwise...
    {
    moving=false; // You're not moving.
    }
    //
if keyboard_check(vk_left) and place_free(x-4,y)// If you're pressing Left, and there's nothing to your Left...
    {
    x -= 4; //Move Left.
    facing = 180; //Facing becomes Left.
    moving = true; // And you start moving.
    }
else // Otherwise...
    {
    moving = false; // You're not moving.
    }
    //
if moving = true and state = 0 // If you're facing Right, and Moving, see above ^^^, and not busy...
    {
        if facing = 90 // If you're facing Right...
            {   
            sprite_index=sprPlayerWalkR; // Show the walking Right sprite.
            }
        else if facing = 180 // But if you're facing Left...
            {
            sprite_index=sprPlayerStandL; // Show that you're standing Left.
            }
}
if moving = false and state = 0 //Otherwise ^^^^^^^^ if you're not moving, and not busy (ie, jumping or crouching)...
    {
        if facing = 90 //And if you're facing Right...
            {
            sprite_index = sprPlayerStandR; // Sprite = standing right.
            }
        else if facing = 180 //Otherwise if you're facing Left...
            {
            sprite_index = sprPlayerStandL; // Sprite = standing left.
            }
}
//
// --------------- DUCKING -----------------
//
if keyboard_check(vk_down) and state = 0 // If you're pressing down, and not busy...
    {
    state = 2; // State = 2
    ducking = true; // And ducking = true
    }
else // Otherwise...
    {
    state = 0;// You're not busy
    ducking = false;// You're not ducking.
    }//
if state = 0
    {
    ducking = false;// State = not busy
    }
//
if ducking = true // If you are ducking...
    {   
        if facing = 90 // and if you're facing Right...
            {
            sprite_index = sprPlayerDuckR; //Sprite = duck Right
            }
        else if facing = 180 // Otherwise if you're facing Left...
            {
            sprite_index = sprPlayerDuckL; // Sprite = duck Left
            }
}
}// --- Paused }

Collision w/ objFloor

Code: [Select]
if vspeed >=0
{
vspeed=0
}

if vspeed=0
{
jumping=false;
}
Logged
  • Dawning Hour

Mamoruanime

@Mamoruanime
Re: [Help Req] Why the hell...?
« Reply #1 on: January 21, 2011, 09:03:51 pm »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
It looks pretty warblegarble; honestly I would set up your state system way different than the way it is :s Right now it looks a bit like states are an afterthought. Constants for state labels would also probably be helpful. Some issues like "jumping" and "moving" being their own variables instead of states is kind of bad, since both of those are things that might not be available to certain states.

My advice is to restructure this whole thing, and make some form of state validation:
(pseudocode)

Code: [Select]
if state = previousstate
{
 return true;
}else{
 return false;
}

and then set up a select block for your states. It makes them much more accessible and visible:

(also pseudocode)
Code: [Select]
Switch (state)
{
case Stand:
scrPlayerStand()
break;
case Walk:
scrPlayerWalk()
break;
case Jump:
scrPlayerJump()
break;
}

or something.

These kinds of errors don't usually happen when things are set up properly :P
Logged
Re: [Help Req] Why the hell...?
« Reply #2 on: January 21, 2011, 09:17:05 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
I wouldn't even use a switch statement to select state code. Just use scripts to represent states. Then do state dependent code inside the scripts and state independent code in the events of the object. You can set up the state as a single variable and pass the event as the first argument or you can make the state an array and have separate scripts for each index/event.
Logged
Re: [Help Req] Why the hell...?
« Reply #3 on: January 23, 2011, 09:45:08 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
The problem is that computers are too fast.  Here, basically what you're doing in this code is saying check every step for when a key is pressed.  If it's pressed, flip the jump state on, if not then flip it off.  However, the step event runs 30 times a second (it's basically running once per frame).  So when you press the key, it detects it and flips the state on, but then the next frame comes around and you're no longer holding the key down, so it flips it off.  That's why you're only seeing the jump frame for a very short time.  You need to restructure this into a system that is dependent on the steps, but at the same time, dependent on some kind of interval, such as a timer or an animation.

Also, you don't appear to be using your states all the time, so that's something else you should consider in your design as well.
Logged



i love big weenies and i cannot lie

Mamoruanime

@Mamoruanime
Re: [Help Req] Why the hell...?
« Reply #4 on: January 23, 2011, 11:02:48 pm »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
I wouldn't even use a switch statement to select state code. Just use scripts to represent states. Then do state dependent code inside the scripts and state independent code in the events of the object. You can set up the state as a single variable and pass the event as the first argument or you can make the state an array and have separate scripts for each index/event.

That seems to be just about as efficient (I don't really see it any more or less efficient really; organization is key I guess); I also forget that in GM you can dynamically choose a script to run without extensive coding due to it not compiling :P
« Last Edit: January 23, 2011, 11:05:27 pm by Mamoruアニメ »
Logged
Pages: [1]   Go Up

 


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



Page created in 0.014 seconds with 43 queries.