Jump to content


Photo

Beat em' up player controls [FIXED]


  • Please log in to reply
5 replies to this topic

#1 Criminon

Criminon

    Final Element

  • GMC Member
  • 155 posts
  • Version:GM:Studio

Posted 19 March 2012 - 12:33 AM

Thanks very much to everyone that posted in this thread! For easy reference, I'm leaving up the working code incase anyone stumbles over this forum looking for player control coding. Big thanks to greep and torigara!

//////// The first part to control the player

// Movements
running = false;
if (keyboard_check(ord('A'))) {
    x -= 3;
    global.mage_left = true;
    global.mage_right = false;
    running = true;
}
if (keyboard_check(ord('D'))) {
    x += 3;
    global.mage_left = false;
    global.mage_right = true;
    running = true;
}
if (keyboard_check(ord('W'))) {
    y -= 3;
    running = true;
}
if (keyboard_check(ord('S'))) {
    y += 3;
    running = true;
}

// Attacks
if (keyboard_check(vk_right) || keyboard_check(vk_down)) {
    attacking = true;
}
else { // Stop attacking
    attacking = false;
    speed = 0;
}

// Jumping
if (keyboard_check_pressed(vk_space)) {
    // Put your jumping stuff here except setting sprites
}

//////// Then change the sprite according to those states
if (global.mage_jump) {
    if (global.mage_left) {
        sprite_index = Mage_jump_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_jump_Right;
    }
}
else if (attacking) {
    if (global.mage_left) {
        sprite_index = Magic_Punch_Left;
    }
    else if (global.mage_right) {
        sprite_index = Magic_Punch_Right;
    }
}
else if (running) {
    if (global.mage_left) {
        sprite_index = Mage_Run_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_Run_Right;
    }
}
else {
    if (global.mage_left) {
        sprite_index = Mage_Face_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_Face_Right;
    }
}

Edited by Criminon, 19 March 2012 - 10:38 AM.

  • 0

#2 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 19 March 2012 - 02:19 AM

Well, it's not so much bad as it is a bit messy, maybe when it's fixed up a bit I can figure it out. For one thing, for all those nested if statements like

if (blah)
if (blah)
if (blah)
{
{
{
}
}
}

use &&s instead. I'm supposing this is all in a single object, the player, so processing speed of the if statements is not a big deal. So the above would be

if (blah && blah && blah)
{
//blah
}

Also, you have a lot of situations where you are doubling up on code you don't need. E.g. in the D code, for both the true and false in there you set some variables to the same thing either way. Just take those out of the if statements then.

Additionally, it's not a big deal, but instead of writing 87 for W e.g. you can write ord('W')

Lastly, I would handle the sprites all at the end based on your variables. Doing the sprites in the wrong order can seriously mess things up and may be the problem.

Edited by greep, 19 March 2012 - 02:20 AM.

  • 0

#3 Criminon

Criminon

    Final Element

  • GMC Member
  • 155 posts
  • Version:GM:Studio

Posted 19 March 2012 - 03:14 AM

Well, it's not so much bad as it is a bit messy, maybe when it's fixed up a bit I can figure it out. For one thing, for all those nested if statements like

if (blah)
if (blah)
if (blah)
{
{
{
}
}
}

use &&s instead. I'm supposing this is all in a single object, the player, so processing speed of the if statements is not a big deal. So the above would be

if (blah && blah && blah)
{
//blah
}

Also, you have a lot of situations where you are doubling up on code you don't need. E.g. in the D code, for both the true and false in there you set some variables to the same thing either way. Just take those out of the if statements then.

Additionally, it's not a big deal, but instead of writing 87 for W e.g. you can write ord('W')

Lastly, I would handle the sprites all at the end based on your variables. Doing the sprites in the wrong order can seriously mess things up and may be the problem.



Most appreciated. Honestly I knew there was a way to use &&, I just couldn't figure it out. The reason I'm doing both sides is because I had issues with the sprites. I'll give it a try though, I don't care too much about messing it up anymore. Super appreciate your help!
  • 0

#4 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 19 March 2012 - 05:35 AM

Generally, the problem is that you change sprites in multiple places those interfering with each other. For instance, you always reset the sprite into either facing or running ones whenever he isn't jumping in the last block of code, cancelling attacking sprites.

You'd better separate the part to control movement and to change sprites. And, instead of mixing three types of keyboard checking (key press, keyboard and key release) it will be a lot easier just to use the keyboard one. E.g.:
//////// The first part to control the player

// Movements
running = false;
if (keyboard_check(ord('A'))) {
    x -= 3;
    global.mage_left = true;
    global.mage_right = false;
    running = true;
}
if (keyboard_check(ord('D'))) {
    x += 3;
    global.mage_left = false;
    global.mage_right = true;
    running = true;
}
if (keyboard_check(ord('W'))) {
    y -= 3;
}
if (keyboard_check(ord('S'))) {
    y += 3;
}

// Attacks
if (keyboard_check(vk_right) || keyboard_check(vk_down)) {
    attacking = true;
}
else { // Stop attacking
    attacking = false;
    speed = 0;
}

// Jumping
if (keyboard_check_pressed(vk_space)) {
    // Put your jumping stuff here except setting sprites
}

//////// Then change the sprite according to those states
if (global.mage_jump) {
    if (global.mage_left) {
        sprite_index = Mage_jump_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_jump_Right;
    }
}
else if (attacking) {
    if (global.mage_left) {
        sprite_index = Magic_Punch_Left;
    }
    else if (global.mage_right) {
        sprite_index = Magic_Punch_Right;
    }
}
else if (running) {
    if (global.mage_left) {
        sprite_index = Mage_Run_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_Run_Right;
    }
}
else {
    if (global.mage_left) {
        sprite_index = Mage_Face_Left;
    }
    else if (global.mage_right) {
        sprite_index = Mage_Face_Right;
    }
}

As a general advice, try not to pile up braces like this:

}}}

That's a red light that you have lost track of the structure of code and blindly putting "}" here and there only to balance the books. Try to indent the code properly all the time so that every "}" lines up with its corresponding control statement. That makes it a lot easier to follow and examine the code structure, saving you from many logic errors.
  • 0

#5 Criminon

Criminon

    Final Element

  • GMC Member
  • 155 posts
  • Version:GM:Studio

Posted 19 March 2012 - 10:34 AM

Generally, the problem is that you change sprites in multiple places those interfering with each other. For instance, you always reset the sprite into either facing or running ones whenever he isn't jumping in the last block of code, cancelling attacking sprites.

You'd better separate the part to control movement and to change sprites. And, instead of mixing three types of keyboard checking (key press, keyboard and key release) it will be a lot easier just to use the keyboard one. E.g.:


As a general advice, try not to pile up braces like this:

}}}

That's a red light that you have lost track of the structure of code and blindly putting "}" here and there only to balance the books. Try to indent the code properly all the time so that every "}" lines up with its corresponding control statement. That makes it a lot easier to follow and examine the code structure, saving you from many logic errors.


Okay, you are seriously my hero here. This was much better logic for the script than I was doing. This was basically exactly what I needed to see. If I could give you a hug right now, I would. For anyone else wanting to use this code for a beat em up, I recommend putting
if (keyboard_check(ord('W'))) {
    y -= 3;
    running = true;
}
if (keyboard_check(ord('S'))) {
    y += 3;
    running = true;
}
So the player looks like they are running when they move up and down.

Seriously though, I'm too appreciative. Be sure to check out my game when you get the chance!


  • 0

#6 Tricky

Tricky

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 04 March 2013 - 10:16 PM

So, what if you want to be able to jump on/over things (like a trashcan)?
I have no clue on how to do the hit detection well in that case..
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users