Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411431 Posts in 69363 Topics- by 58417 Members - Latest Member: gigig987

April 20, 2024, 03:57:25 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)help needed with player states/drawing hitboxes
Pages: [1]
Print
Author Topic: help needed with player states/drawing hitboxes  (Read 745 times)
vobster
Guest
« on: November 22, 2014, 02:16:17 AM »

so i'm trying to put together this simple platformer, and there's 2 things that i just can't wrap my head around: player states and hitboxes. spoiler alert: i don't really have any experience with programming aside from very basic GML and some AS3 so these questions might be really dumb.

please note that i'm using game maker 7 so i can't access a lot of the dummy engines that explain these things.

let's start with player states. i found this tutorial which seemed pretty useful (and i think i do kind of understand how it works), but game maker doesn't seem to register the "enum" function for me at all? i'm not really sure where i'm supposed to put it either, to be honest. anyway, here's my code so far:

Code: (obj_player create event)
//initialize variables
grav = 1;

hsp = 0;
vsp = 0;

frc = 1;

movespeed = 4;
jumpspeed = 10;

crouchspeed = 2;

//state machine
myState_physical = state_physical.ground;
myState_combat = state_combat.idle;

Code: (obj_player step event)
//receive player input
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);
key_crouch = keyboard_check(vk_down);
key_attack = keyboard_check(ord('Z'));

//initialize state machine
scr_playerState();

//check for ground
if (place_meeting(x, y+1, obj_solid)) {
myState_physical = state_physical.ground;

vsp = 0;

//jumping
if (key_jump) {
vsp = -jumpspeed;
}

//crouching
if (key_crouch) {
sprite_index = spr_player_crouch;
}
} else {
//gravity
myState_physical = state_physical.air;

if (vsp < 10) {
vsp += grav;
}

//controlled jumping
if (keyboard_check_released(vk_up) && vsp < 0) {
vsp /= 2;
}
}

//moving right
if (key_right) {
if (hsp < movespeed) {
hsp += frc;
}
hsp = movespeed;
}

//moving left
if (key_left) {
if (hsp > -movespeed) {
hsp -= frc;
}
hsp = -movespeed;
}

//atacking
if (key_attack && myState_combat != state_combat.attack) {
myState_combat = state_combat.attack;
}

//check for not moving
if ((!key_left && !key_right) || (key_left && key_right)) {
if (hsp != 0) {
if (hsp < 0) {
hsp += frc;
} else {
hsp -= frc;
}
}
}

//horizontal collisions
if (place_meeting(x+hsp, y, obj_solid)) {
while (!place_meeting(x+sign(hsp), y, obj_solid)) {
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//vertical collisions
if (place_meeting(x, y+vsp, obj_solid)) {
while (!place_meeting(x, y+sign(vsp), obj_solid)) {
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

Code: (scr_playerState)
switch (myState_physical) {
case state_physical.ground:
{
//scr_physicalState_ground();
}; break;

case state_physical.air:
{
//scr_physicalState_air();
}; break;
}

switch (myState_combat) {
case state_combat.idle:
{
//scr_combatState_idle();
}; break;

case state_combat.attack:
{
scr_combatState_attack();
}; break;
}

i'm sorry guys, it's a mess. but yeah, then come hitboxes. i'm just quite confused when it comes to the whole collision mask thing. i made a placeholder spritesheet thing that looks like this, but now it's just kind of, you know - now what?

Logged
Cheezmeister
Level 3
***



View Profile
« Reply #1 on: November 22, 2014, 12:54:56 PM »

Hitbox a.k.a. collision mask is what gets used to check for collisions. It never gets drawn. Unlike sprites, which are what gets drawn, but never used for collisions. This separation is how you get stuff like invisible checkpoints or pass-through walls.

Player State can mean a lot of things depending on context and how you choose to use it, but you might use it to track things like power-ups, stances, or hats.

A state machine is a weird idea that you don't really need to grok, but the picture in your tutorial pretty much sums it up. Sort of a more pedantic form of "state" where you don't have individual variables. It underlies much of computing and you can read the wikipedia page if you are so inclined.

I don't know a blasted thing about GML, so I can't help you there.
Logged

෴Me෴ @chzmstr | www.luchenlabs.com ቒMadeቓ RA | Nextris | Chromathud   ᙍMakingᙌCheezus II (Devlog)
nox
Level 0
***



View Profile WWW
« Reply #2 on: November 22, 2014, 01:05:24 PM »

As far as the enum issue--Game Maker did not support enums until recently, so they're definitely not in GM7. However, in the case of GM, enums are just batches of named constants. So there are at least two things that you can do to replicate the functionality in GM7: you can either use strings, "idle", "grounded", "jumping", OR you can just define constants in the global scope through the GM GUI, assuming GM7 has this functionality. If it doesn't, you could kind of manually build an enumeration like this:

Code:
PLAYER_GROUNDED = 1;
PLAYER_JUMPING = 2;
PLAYER_ATTACKING = 3;


The strings solution would be the simplest for you, and any performance impact would be negligible.

Now, if you don't go with the strings solution, you mention that you wouldn't know where to put this stuff. I would just make a spriteless "controller" object and place it in the room before anything else with the globally effective setup code in the create event. I usually have an object called objMainController that does this kind of global set up.

For the hitboxes, I would toss that spritesheet and just load your player sprite separately from your sword sprite. Then you can conduct hit-testing on the sword separate from the hit-testing on the player.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic