Character (Sprite) behaviours

J

Jedd Lupoy

Guest
Hello,

I am currently making a top-down game and I've already made my sprites and controls for the player. I have one problem. So I press 'space' and it changes the sprite to an attacking position so if the enemy touches that specific sprite, they die and if the enemy touches the player without the attacking sprite, the player dies. How do I do that? I'm quite new to Gamemaker :)

Thanks,
Jedd Lupoy
 
R

rui.rosario

Guest
You have a variable called sprite_image which represents the index of the current sprite assigned to the current instance. You can use this variable to both change and check the current sprite of the player.

Something like
Code:
/// PLAYER STEP EVENT

if (keyboard_check(vk_space)) {
    sprite_index = spr_player_attack;
} else {
    sprite_index = spr_player;
}

/// PLAYER COLLISION WITH ENEMY

if (sprite_index == spr_player_attack) {
    // deal damage to enemy
} else {
    // the player takes damage
}
Hope this helps you. Also check the documentation page for sprite_index.
 

wamingo

Member
Code:
// create
attacking=false;

// step

if keyboard_check_pressed(vk_space)
{
    attacking=!attacking;
    if (attacking)
        sprite_index = spr_attacking;
    else
        sprite_index = spr_idle;
}

var enemy = instance_place(x,y, obj_enemy);
if (enemy!=noone)
{
    if (attacking){
        enemy.hp -= 1;
    }else{
        hp -= 1;
    }
}
 
J

Jedd Lupoy

Guest
Thank you, @wamingo ! What is it doing to the enemy?

Code:
var enemy = instance_place(x,y, obj_enemy);
if (enemy!=noone)
{
    if (attacking){
        enemy.hp -= 1;
    }else{
        hp -= 1;
    }
}
 

wamingo

Member
player obj checks if there's collision with enemy obj and depending on whether attacking is true or false it reduces the enemy's or the player's healthpoints.
 
J

Jack

Guest
Hey dude, are you having trouble with your sprites animation? i have a silmilar code to yours but mine isnt working :/
 
J

Jedd Lupoy

Guest
@wamingo I replaced the code and I ran the game and popped up the error:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_enemy:

Unable to find any instance for object index '0' name 'obj_player'
at gml_Object_obj_enemy_StepNormalEvent_1 (line 2) - mp_potential_step(obj_player.x,obj_player.y,2.5,true);
############################################################################################

I should have told you earlier about the enemy's code:

Code:
//Head Towards Player
mp_potential_step(obj_player.x,obj_player.y,2.5,true);
 

wamingo

Member
Code:
//Head Towards Player
if (instance_exists(obj_player)){
   mp_potential_step(obj_player.x,obj_player.y,2.5,true);
}
I recommend you go through some tutorials. :)
 

chance

predictably random
Forum Staff
Moderator
I merged these two duplicate threads, apparently posted by accident. So there may be some duplicate and/or orphaned posts above.
 
Top