1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Windows Player Movement: Shooting

Discussion in 'Programming' started by Freedom2Fight, Jul 8, 2016.

  1. Freedom2Fight

    Freedom2Fight Member

    Joined:
    Jun 25, 2016
    Posts:
    94
    Hello, again. I would like to request your help.

    I am making a platformer. I am relatively new to this and I am very eager to learn.

    So here is the situation:

    I want the playable character to able to shoot projectiles in the direction they are facing.

    Whenever I press the [Attack Button] the character will [shoot] and a [bullet will launch] in the [direction they are facing]. Please see illustration below.

    [​IMG]

    It also doubles as a rorschach test

    The code I provided below allows the player shoot. However there is a problem, please see illustration below:

    [​IMG]


    There are also a number of issues I have yet to deal with such as the bullet destroying itself upon collision with walls and enemies. Right now, I just want the bullet to travel to the correct direction.

    Here is the code:

    Attack is a variable I set up as False in the Create Event.

    Attack = keyboard_check_pressed(ord"Z"))

    Code:
    if Attack
    {
    sprite_index = spr_rangedattack;
    
    if sprite_index = spr_rangedattack
    {
    with(instance_create(x,y,obj_bullet))
       {
           image_xscale = other.image_xscale;
           obj_bullet.speed = 15
           with(instance_place(x,y,obj_enemy))
    
         //Below checks if it is hitting an enemy
            {
             if(hit==0)
             {
                hit = 1;
                vsp = -3;
                hsp = sign(x - other.x)*4;
                image_xscale = sign(hsp)
             }     
    
    
          } 
    
      }
    
    
    }
    }
    The code above seem to only let the projectile travel to the right, if the bullet is facing left it still travels to the right.

    I am fairly certain that the speed, direction of the bullet and the bullet itself should be generated through the player. Although, I am not entirely sure how to go about it.


    How would one go about executing this?

    As always, looking forward to your responses.
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,688
    First, you are setting the speed but not the direction. If you've got image_xscale already, you may as well do something with it.
    Code:
    with (instance_create(x, y, obj_bullet))
    {
      image_xscale = other.image_xscale;
      hspeed = sign(image_xscale)*15;
    }
    Second, the enemy collision code is in the wrong place. That should be checked constantly by being in the bullet's Step code, not once by being here.
     
    Freedom2Fight likes this.
  3. tserek

    tserek Guest

    You just need to keep track which direction your character is facing. You could use a variable called dir to track it.

    Create:
    Code:
    dir = 1; // facing to east as default
    Step:
    Code:
    attack = keyboard_check_pressed(ord("Z"));
    
    // facing dir will be set either -1 or 1
    if keyboard_check(vk_right) +(-keyboard_check(vk_left)) !=0
    {
    dir = keyboard_check(vk_right) +(-keyboard_check(vk_left));
    image_xscale = dir;
    }
    
    // shoot
    if attack
    {
    inst = instance_create(x,y,obj_bullet);
    inst.hsp = dir *4;
    inst.image_xscale = dir;
    }
    
    Bullet step:
    Code:
    x+=hsp;
     
    Freedom2Fight likes this.
  4. Freedom2Fight

    Freedom2Fight Member

    Joined:
    Jun 25, 2016
    Posts:
    94
    Thank you for helping me out. Because of the two of you, I was able to adjust the code to work as intended. However I am experiencing some problems because the code is getting a bit unwieldy. I think it is time to use state machines but thats for another matter.

    Once again thank you for your time and assistance.
     
  5. crazybloodmonkey

    crazybloodmonkey Member

    Joined:
    Jul 12, 2016
    Posts:
    70
    i having this same problem it's only shooting right expect i didn't code the shooting i just have
    in obj_player
    key press K
    ( create instance obj_bullet x = 0 y=-16 and it's relative)

    in obj_bullet
    a create event and in it
    move fixed -->
    speed 8 and not relative.

    so any ideas oh how to fix that with drag and drop stuff or am i going to have to code everything in the a execute code to fix this problem lol
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,122
    It only moves right because you set the speed and direction of the bullet inside the bullet itself and not inside the player. You need to have the player set the bullet Direction based on the player's own Direction. You may have to use the code action, because I don't know if you can even specify a Target instance to effect in Drag and Drop otherwise. I mean, there is a way, but you pretty much have to use gml no matter what as far as I can tell.
     
  7. Freedom2Fight

    Freedom2Fight Member

    Joined:
    Jun 25, 2016
    Posts:
    94
    @crazybloodmonkey

    If you are using drag and drop then this video might help you:



    Its made with an old version of GM but it should still work with a few adjustments.

    Sooner or later you're going to need to use GML.
     
  8. crazybloodmonkey

    crazybloodmonkey Member

    Joined:
    Jul 12, 2016
    Posts:
    70
    yeah i've done some coding for character moment and gravity and stuff. i'm just trying to cut down on it because i've been making this game for over a year. well actually i work on the game for 4 months and then took like a 6 month break because majoring in computer science didn't work out so i lost all motivation to continue to work on the game. anyway i just need to finish this before summer ends so i can say i at least made one game so i'm just trying speed some things up i know i'm definitely going to have to use some GML for the enemies lol
     
  9. crazybloodmonkey

    crazybloodmonkey Member

    Joined:
    Jul 12, 2016
    Posts:
    70
    lol okay so after many failed attempts of the drag and drop videos and a couple reddit threads i decided to just take the advice of the two people here that help you and well it worked i can now shoot both ways....................................but now my character doesn't face left when he walks left he sort moon walks lol i just can't win. i sure this is a really easy fix it's probably just 1 or 3 more lines of code. anyways anyone want to hook me up with the fix i would search it but i think i've been working on the game for like 2 hours or more and i'm sort of tired now so i 'm calling it a day. if not then i guess i'll just figure it out tomorrow by some other means

    edit: never mind i figured it out just had to put a xscale -1
     
    Last edited: Jul 15, 2016

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