Character (Sprite) behaviours

Discussion in 'Programming' started by Jedd Lupoy, Jun 25, 2016.

  1. Jedd Lupoy

    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
     
  2. rui.rosario

    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.
     
  3. wamingo

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    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;
        }
    }
    
     
  4. Jedd Lupoy

    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;
        }
    }
     
  5. wamingo

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    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.
     
  6. Jedd Lupoy

    Jedd Lupoy Guest

    @wamingo How do I set the lives for the player and enemy object?
     
  7. wamingo

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    same way as the attacking variable. in create event, add a code segment and add this:

    hp=10;
     
  8. Jedd Lupoy

    Jedd Lupoy Guest

  9. Jack

    Jack Guest

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

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    ok I was under the impression that you had some experience.
    replace this code:
    Code:
        if (attacking){
            with (enemy) instance_destroy();
        }else{
            instance_destroy();
        }
    
    it's a bit crude but... maybe you'll realize how the code works.
     
  11. Jedd Lupoy

    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);
     
  12. wamingo

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    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. :)
     
  13. Jedd Lupoy

    Jedd Lupoy Guest

    @wamingo *sighs* yeah hahaha, thanks :)
     
  14. chance

    chance predictably random Forum Staff Moderator

    Joined:
    Apr 22, 2016
    Posts:
    804
    I merged these two duplicate threads, apparently posted by accident. So there may be some duplicate and/or orphaned posts above.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice