Distance_to_object() help

Discussion in 'Programming' started by Jacob Thrasher, Oct 6, 2019.

  1. Jacob Thrasher

    Jacob Thrasher Member

    Joined:
    Apr 24, 2019
    Posts:
    4
    I don't know if this is a problem with how I am writing the distance_to_object command or if it is another issue somewhere else in my code, but I am thinking it is the former.

    Essentially the way I programmed my enemy AI is to check if the player is within a certain distance of it, then it will act accordingly. There is a check for if the player is far away, near, and very close to the object. These cause the enemy to move idly, follow the player, and attack respectively. The code is as followed (extremely simplified, but I can just post the whole thing if need be. Just let me know).

    if ((distance_to_object(obj_player) <= 5) && place_meeting(x,y+1,obj_wall))
    {
    //Perform attacking code
    }
    else if (distance_to_object(obj_player) > 5) && (distance_to_object(obj_player) <= 150)
    {
    //Perform follow code
    }
    else
    {
    //Perform idle code
    }

    What's happening is that the bear is getting caught up trying to transition from the following code to the attacking code. The game said the distance_to_object is reading 20 (shown on the rock), but it is still running the code under the first if branch. Here is a video:

     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,409
    A bigger snippet of code would help, but, if I was to guess, you don't have a mask index assigned for the bear object, thus causing the distance to change when the sprite changes.
     
    ParodyKnaveBob likes this.
  3. Jacob Thrasher

    Jacob Thrasher Member

    Joined:
    Apr 24, 2019
    Posts:
    4
    Wow yeah you were exactly correct. The problem was fixed immediately when I messed with the mask. Thanks so much, that was getting frustrating.
     
    ParodyKnaveBob likes this.

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