(SOLVED) 2 codes written similar are working way differently

Discussion in 'Programming' started by RoyalApocalyptic, Jan 10, 2017.

  1. RoyalApocalyptic

    RoyalApocalyptic Member

    Joined:
    Jan 9, 2017
    Posts:
    2
    SOLVED!
    The solution, for anyone else having this issue was to run the arms "follow" code in the End Step event rather than the step event.



    In my game, the player's head and arms are separate from the body and point to the mouse and follow the player.

    The head in this case works 100% fine, snapping to the player perfectly and not falling behind.
    But the arms trail behind a bit, not going the same speed as the character's torso.

    It is a very minor trail and looks fine while running, but if the character picks up speed or falls at all, the arms start to trail behind significantly and looks horrible.

    Here is an example.
    Left is what happens when the character is at a high speed.
    Right is what it is SUPPOSED to be.

    upload_2017-1-9_17-58-27.png


    Code:



    Head code:


    if obj_player.sprite_index = spr_player_right
    || obj_player.sprite_index = spr_player_jump_right
    || obj_player.sprite_index = spr_player_fall_right
    {
    x = obj_player.x;
    y = obj_player.y-4;
    }
    if obj_player.sprite_index = spr_player_left
    || obj_player.sprite_index = spr_player_jump_left
    || obj_player.sprite_index = spr_player_fall_left
    {
    x = obj_player.x;
    y = obj_player.y-4;
    }


    Arms code(Yes I know it can be condensed a bit but I am very early in dev of this game at the moment.) :

    if obj_player.sprite_index = spr_player_right && mouse_x > x
    || obj_player.sprite_index = spr_player_right_walk && mouse_x > x
    || obj_player.sprite_index = spr_player_fall_right && mouse_x > x
    || obj_player.sprite_index = spr_player_jump_right && mouse_x > x
    {
    obj_arms.x = obj_player.x-4;
    obj_arms.y = obj_player.y+4;
    }



    if obj_player.sprite_index = spr_player_left && mouse_x < x
    || obj_player.sprite_index = spr_player_left_walk && mouse_x < x
    || obj_player.sprite_index = spr_player_fall_left && mouse_x < x
    || obj_player.sprite_index = spr_player_jump_left && mouse_x < x
    {
    obj_arms.x = obj_player.x+4;
    obj_arms.y = obj_player.y+4;
    }



    if obj_player.sprite_index = spr_player_right && mouse_x < x
    || obj_player.sprite_index = spr_player_right_walk && mouse_x < x
    || obj_player.sprite_index = spr_player_fall_right && mouse_x < x
    || obj_player.sprite_index = spr_player_jump_right && mouse_x < x
    {
    obj_arms.x = obj_player.x+4;
    obj_arms.y = obj_player.y+4;
    }



    if obj_player.sprite_index = spr_player_left && mouse_x > x
    || obj_player.sprite_index = spr_player_left_walk && mouse_x > x
    || obj_player.sprite_index = spr_player_fall_left && mouse_x > x
    || obj_player.sprite_index = spr_player_jump_left && mouse_x > x
    {
    obj_arms.x = obj_player.x-4;
    obj_arms.y = obj_player.y+4;
    }
     
    Last edited: Jan 10, 2017
  2. Deanaro

    Deanaro Guest

    Im guessing the arm isntance is being ran before the body. So its always in the previous coordinates. Try creating the body object first. Go to the room editor and go into creation order. Put the body before all the other bodyparts.

    Edit:
    After looking at your code a little more. You seem to be checking sprite_index alot. It would make your code look much better if you assign a direction variable (lets call it face) to -1 or 1 (left or right) and do one check:
    If face = 1
    {}
    Else if face = -1
    {}
     
    Last edited by a moderator: Jan 10, 2017
  3. Murzy

    Murzy Member

    Joined:
    Jul 28, 2016
    Posts:
    24
    You should do all of the movements in a single place to make it consistent. That is, make the body determine the location of the head and the arms relative to itself, after you have done the actual movement for the body.

    As Deanaro said, the problem is caused by the creation order of the objects. If all of this code is in the objects' step events it may be that the arms always update before the body, and thus lag one step behind. (Think all of the instances in your game. The step event for each of the instances you have, i.e. body, arms, head, is executed sequentially. So the order in which the instances are created has a big impact on how the code actually works in this case.) This can be noticeable when the body moves at a fast pace.

    An easy fix for this would be to change the arm's code to the end step event, so it is executed after the body's update, during the same frame/tick. However, I would recommend moving all the movement code to a single place :)
     
    Last edited: Jan 10, 2017
  4. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    626
    It might be one of two things:

    1) Your sprites for the various left / right states don't all have the same x/y point for their dimensions. Assuming that x/y is the center: say that one is ten pixels wide and the same in height - its center would be 5 pixels, if the next is 12 pixels width / height then its center would be 6 pixels. So the arm would be jumping one pixel further when it changes. If there's a bigger difference between the images then it will be more pronounced.

    Looking at your sprites it would seem that one is smaller in height than the other - perhaps you haven't accounted for the origin point being in a different place?

    2) It could be the step event you are running it in. Objects reach their move position in the 'step' step event. When you have another object moving to follow its position you want the code for that in the 'end' step event
     
  5. RoyalApocalyptic

    RoyalApocalyptic Member

    Joined:
    Jan 9, 2017
    Posts:
    2
    You guys are beautiful people, thank you! I have been having this issue for weeks and I couldn't find any solutions.

    I have tried making the code all in one place previously but it didn't work, but making the code run in the End Step event worked perfect.
     

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