• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Proximity based pop-up graphic

B

bobula13

Guest
Hey all,

I've been busting my head over this one and could use some help. I'm trying to set my game up so that when the obj_player gets closer to an object (in this case a signpost) a small icon appears above it that tells which button to press to initiate dialogue, which is a little animated icon with a "T" saying to press the T key down.

I've tried several methods on this, from making the icon an object and try to have it pop up when the player gets close enough using a range variable, to just adding code in the sign object itself that states if obj_player <= range instance_create (x,y, obj_talk_button).

I've been looking all over the web for any help and in these forums and can't seem to get anywhere. Anyone got any tips for me?

Appreciate the help!
 

YoSniper

Member
In the Draw Event of the signpost object, try something like this:
Code:
draw_self();  //Ensure that the sprite for the signpost still gets drawn

if instance_exists(obj_player) {
    if distance_to_object(obj_player) < 48 {
        draw_sprite(spr_icon, 0, x, y - 32);
    }
}
//NOTE: all x and y offsets are estimated
 
B

bobula13

Guest
You are a godsend! I'm still new to Gamemaker and the biggest obstacle I keep running into is trying to remember and find the proper commands to make things work. Is there a way to make the sprite animate as well, not just have the image index of the sprite with the draw_sprite command? I tried replacing the sprite with the actual object that has the image speed, and tried to add the image_speed command into the above code but that didn't work either.

Thanks again for your help!
 

YoSniper

Member
Is there a way to make the sprite animate as well, not just have the image index of the sprite with the draw_sprite command?
Yes. It's a little more complicated than just setting image_speed (the animation speed in frames per step). Basically, you need to add something like this:

Include this in Create Event
Code:
prompt_index = 0;
prompt_speed = 4 / room_speed; //This is an example. This means that the prompt would animate 4 frames per second.
prompt_images = 4;  //Set this value to the number of subimages in your prompt sprite
Draw Event
Code:
draw_self();  //Ensure that the sprite for the signpost still gets drawn

if instance_exists(obj_player) {
    if distance_to_object(obj_player) < 48 {
        draw_sprite(spr_icon, prompt_index, x, y - 32);
        prompt_index += prompt_speed;  //Animates the prompt
        if prompt_index >= prompt_images {
            prompt_index -= prompt_images;  //Keeps the animation cycling
        }
    }
}
Please don't hesitate to ask if any of this does not make sense to you. I'm doing my best to be as straightforward with my comments as possible.
 
B

bobula13

Guest
This makes perfect sense! I love getting examples like this from people because it helps me understand how to use variables to make work arounds on the basic commands. Once again I really appreciate your help on this.

THanks!!
 
Top