beginner cant seem to get code to work

P

pablowski

Guest
So ive recently started using game maker studios and have been watching a few tutorials. I have been able to make my player power up and kill enemies by jumping on them but his sword swing attack doesn't work. Any help would be greatly appreciated. Here is the code ( and the code in /// is just code i tried that didn't work too).


///get the players input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);
//key_attack = keyboard_check_pressed(vk_space)

//react to inputs
//left and right
move = key_left + key_right;
hsp = move * movespeed;
if (vsp<10) vsp += grav;

//jump
if (place_meeting(x,y+1,obj_wll))
{
jumps = jumpmax;
}

if (key_jump) && (jumps > 0)
{
jumps -= 1;
vsp = -jumpspeed;
}

//attck

///if (key_attack)
///{

/// sprite_index = spr_plyr_attck;
/// hsp = 0;
/// if (image_index >=3) && (image_index <= 5)
/// {
/// with(instance_create(x,y,obj_hitbox))
/// {
/// image_xscale = other.image_xscale;
/// with(instance_place(x,y,par_enemy))
/// {
/// instance_destroy();
/// }
/// }
/// }
///}

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


//Vertical Collision
if (place_meeting(x,y+vsp,obj_wll))
{
while(!place_meeting(x,y+sign(vsp),obj_wll))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

// animation

if (alarm[0] <= 0)
{
if (move!=0) image_xscale = move;

///keyboard_check_pressed(vk_space) //or
if (keyboard_check_pressed(vk_space))
{
sprite_index = spr_plyr_attck;
image_speed = 0.1;
hsp = 0;
if (image_index >=3) && (image_index <= 5)
{
with(instance_create(x,y,obj_hitbox))
{
image_xscale = other.image_xscale;
with(instance_place(x,y,par_enemy))
{
instance_destroy();
}
}
}
}

if (place_meeting(x,y+1,obj_wll))
{
if (move!=0)
{
sprite_index = spr_plyr_wlk;

}
else sprite_index = spr_plyr;
}
else
{
if (vsp < 0) sprite_index = spr_plyr_jmp; else sprite_index = spr_plyr_fll;
}
}
//if powered up
if (alarm[0] > 0)
{
{
if (move!=0) image_xscale = move;
if (place_meeting(x,y+1,obj_wll))
{
if (move!=0)
{
sprite_index = spr_plyr_wlk_ghost;
image_speed = 0.25;
}
else sprite_index = spr_plyr_ghost;
}
else
{
if (vsp < 0) sprite_index = spr_plyr_jmp_ghost; else sprite_index = spr_plyr_fll_ghost;
}

}
}
 

TDSrock

Member
Welcome to the GMC!

First off. Wrong section of the forum I hope a mode will come round to move this topic soon. This should have gone to the programming section ;)

So lets start by getting your code formatted on here.
Code:
///get the players input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);
//key_attack = keyboard_check_pressed(vk_space)
//react to inputs
//left and right
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

//jump
if (place_meeting(x, y + 1, obj_wll)) {
    jumps = jumpmax;
}

if (key_jump) && (jumps > 0) {
    jumps -= 1;
    vsp = -jumpspeed;
}

//attck
///if (key_attack)
///{
/// sprite_index = spr_plyr_attck;
/// hsp = 0;
/// if (image_index >=3) && (image_index <= 5)
/// {
/// with(instance_create(x,y,obj_hitbox))
/// {
/// image_xscale = other.image_xscale;
/// with(instance_place(x,y,par_enemy))
/// {
/// instance_destroy();
/// }
/// }
/// }
///}
//horizontal collision
if (place_meeting(x + hsp, y, obj_wll)) {
    while (!place_meeting(x + sign(hsp), y, obj_wll)) {
	x+= sign(hsp);
    }
    hsp = 0;
}
x += hsp;


//Vertical Collision
if (place_meeting(x, y + vsp, obj_wll)) {
    while (!place_meeting(x, y + sign(vsp), obj_wll)) {
	y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;

// animation

if (alarm[0] <= 0) {
    if (move != 0) image_xscale = move;

///keyboard_check_pressed(vk_space) //or
    if (keyboard_check_pressed(vk_space)) {
	sprite_index = spr_plyr_attck;
	image_speed = 0.1;
	hsp = 0;
	if (image_index >= 3) && (image_index <= 5) {
	    with(instance_create(x, y, obj_hitbox)) {
		image_xscale = other . image_xscale;
		with(instance_place(x, y, par_enemy)) {
		    instance_destroy();
		}
	    }
	}
    }

    if (place_meeting(x, y + 1, obj_wll)) {
	if (move != 0) {
	    sprite_index = spr_plyr_wlk;
	} else
	    sprite_index = spr_plyr;
    }
    else {
	if (vsp < 0) sprite_index = spr_plyr_jmp;
	else sprite_index = spr_plyr_fll;
    }
}
//if powered up
if (alarm[0] > 0) { {
	if (move != 0) image_xscale = move;
	if (place_meeting(x, y + 1, obj_wll)) {
	    if (move != 0) {
		sprite_index = spr_plyr_wlk_ghost;
		image_speed = 0.25;
	    } else
		sprite_index = spr_plyr_ghost;
	}
	else {
	    if (vsp < 0) sprite_index = spr_plyr_jmp_ghost;
	    else sprite_index = spr_plyr_fll_ghost;
	}
    }
}
Now everyone can easily read your code.

I will post this post as is currently and read the code. After that I will edit this page, the edit will be underneath this line clarified with a bold EDIT line.

EDIT
Honestly your commented attack section looks alright. What exactly was going wrong?
Do your obj_hitbox objects have a sprite? If not give them a sprite so you can actually see them, later you can set the obj to destroy immediately after it's done so it won't be seen anyway.
If none of that help. Start doing some checks on what the variables are. You are boldly doing with(create_instance()){ }
I would instead just store the instance ID in a separate value first, that way you can check what it is. That is more important with the section after it.
 
T

TimothyAllen

Guest
Obiously a keyboard_check_pressed only occurs for one step. So your attack sprite never has time to animate to the approprate image_index so that your hit box will be created. This problem is two fold because even if you took the attack portion out of the key check, you still have other code that is constantly changing your sprite which will still f up your animation. This is where finite state machines (fsm) come in handy. Even if the fsm is only implemented on the animation portion.
 

TDSrock

Member
Obiously a keyboard_check_pressed only occurs for one step. So your attack sprite never has time to animate to the approprate image_index so that your hit box will be created. This problem is two fold because even if you took the attack portion out of the key check, you still have other code that is constantly changing your sprite which will still f up your animation. This is where finite state machines (fsm) come in handy. Even if the fsm is only implemented on the animation portion.
Nice catch. I forgot to look for a finite state machine here.
 
P

pablowski

Guest
Obiously a keyboard_check_pressed only occurs for one step. So your attack sprite never has time to animate to the approprate image_index so that your hit box will be created. This problem is two fold because even if you took the attack portion out of the key check, you still have other code that is constantly changing your sprite which will still f up your animation. This is where finite state machines (fsm) come in handy. Even if the fsm is only implemented on the animation portion.
I am still getting the hang of using fse. Is there any advice you would have for how to lay out the attack script?
 
Top