Flashlight sprite Stretches to Mouse Coordinates.?

B

Buddy Theoret

Guest
Hey everyone. thanks for taking the time to help me with this very annoying problem I am having, but I can't do this without a little help from my friend's XD
so my problem is that I have a flashlight object that aims at the mouse coordinates but I want to make the sprite of the flashlight stretch from the main player's coordinates to the mouse coordinates.

Please help if you know how to accomplish this.GMC-Picture.png

So whenever the player moves the mouses position.. the light should stretch between the player & the mouse's current position.
Please & Thank's!! -- Bud.
 

shortbread

Member
Assuming your light sprite is horizontal and facing 0 degrees by default, then what you need to do is adjust the x scale of the sprite based on the distance from the player to the mouse.

Here is an example:
Code:
light_direction = point_direction(ob_player.x,ob_player.y,mouse_x,mouse_y);
light_distance = point_distance(ob_player.x,ob_player.y,mouse_x,mouse_y);

light_x_scale = light_distance/sprite_get_width(sprite_index);

draw_sprite_ext(light_sprite, 0, ob_player.x, ob_player.y, light_x_scale, 1, light_direction, c_white, 1);
 
B

Buddy Theoret

Guest
im not sure how im supposed too set this up??.. can you please tell me how im supposed too use this line of code??

is there any other simpler way?? like say perhaps

(( sprite_width = mouse_x )) ??? or anything like that?? because i dont have any objects named Light & light_direction or light_distance is not a thing that can be used in gml script...

((( light_direction???? = point_direction(ob_player.x,ob_player.y,mouse_x,mouse_y);
light_distance???? = point_distance(ob_player.x,ob_player.y,mouse_x,mouse_y);

light_x_scale??? << how is this supposed too work?? = light_distance/sprite_get_width(sprite_index);

draw_sprite_ext(light_sprite, 0, ob_player.x, ob_player.y, light_x_scale, 1, light_direction, c_white, 1); )))
 
B

Buddy Theoret

Guest
Assuming your light sprite is horizontal and facing 0 degrees by default, then what you need to do is adjust the x scale of the sprite based on the distance from the player to the mouse.

Here is an example:
Code:
light_direction = point_direction(ob_player.x,ob_player.y,mouse_x,mouse_y);
light_distance = point_distance(ob_player.x,ob_player.y,mouse_x,mouse_y);

light_x_scale = light_distance/sprite_get_width(sprite_index);

draw_sprite_ext(light_sprite, 0, ob_player.x, ob_player.y, light_x_scale, 1, light_direction, c_white, 1);

i posted a reply too this underneath your post.. review it & reply please and thank you.
 
C

Catastrophe

Guest
What he wrote is the way to do it, there is no simpler way because it is at an angle.

What he's using are called variables, not objects, they let you store and get information. This code is meant to be put in the draw event of your flashlight object or in addition to draw_self in the player object. and you are supposed to change obj_player with the name of your player object and light_sprite with the sprite of your flashlight or sprite_index if it is assigned

I think it would be wise to start with a youtube tutorial on the basics of Game Maker programming, one that has a simple engine not one like shaun spalding's. Starting out as a newbie like this, I guarantee you will end up throwing out all of the code for your initial game anyways at some point, so you won't really be wasting time. We could explain the basics to get this working, but as it involves explaining variables, dot operators, etc, it sounds like you should learn some of these.
 
B

Buddy Theoret

Guest
What he wrote is the way to do it, there is no simpler way because it is at an angle.

What he's using are called variables, not objects, they let you store and get information. This code is meant to be put in the draw event of your flashlight object or in addition to draw_self in the player object. and you are supposed to change obj_player with the name of your player object and light_sprite with the sprite of your flashlight or sprite_index if it is assigned

I think it would be wise to start with a youtube tutorial on the basics of Game Maker programming, one that has a simple engine not one like shaun spalding's. Starting out as a newbie like this, I guarantee you will end up throwing out all of the code for your initial game anyways at some point, so you won't really be wasting time. We could explain the basics to get this working, but as it involves explaining variables, dot operators, etc, it sounds like you should learn some of these.


i know how to work variables.. ive been using game maker for years but stopped for a few years and i just started back too it a few weeks ago.. but non the less ive never tried too make a flashlight that does what im going to make it do... i was confused because he never said anything about using variables.. he just put that script up there and left it for me to figure it out...
all i was asking was if there was another way to do this.. and im sure there is.. i just havent figured out an easy way to perform this neat light trick...
but can you please do me a favour and maybe just tell me where im putting this and how im setting it up... do i need to create global variables for this to work or what.? thats all im asking..
 
C

Catastrophe

Guest
Nope, you just put this in the draw event in your flashlight, and make the changes I said earlier regarding the player/sprite_index names. If you have problems let us know.
 
C

Catastrophe

Guest
Alternatively, put this in step:


image_angle = point_direction(ob_player.x,ob_player.y,mouse_x,mouse_y);
light_distance = point_distance(ob_player.x,ob_player.y,mouse_x,mouse_y);

image_xscale = light_distance/sprite_get_width(sprite_index);
 
B

Buddy Theoret

Guest
Alternatively, put this in step:


image_angle = point_direction(ob_player.x,ob_player.y,mouse_x,mouse_y);
light_distance = point_distance(ob_player.x,ob_player.y,mouse_x,mouse_y);

image_xscale = light_distance/sprite_get_width(sprite_index);


ok so i forgot to mention that i am not using the newest game maker 2... im using game maker 8.0 pro version.. and before you suggest that i upgrade,, i will not do that because im to used to this version of game maker & i have no intentions on upgrading.. but if anyone knows a way to do this using GM8 Pro. please inform me :)
 
Last edited by a moderator:
Are you sure you're not overreaching? You say you know how to work variables yet you were really confused when presented with some, and the code given to you doesn't do anything fancy that versions of Game Maker even older than 8.0 could easily handle. If copying and pasting this simple block of code is that troublesome, I second you looking through some basic tutorials...
 

FrostyCat

Redemption Seeker
Everything that was said thus far works for all versions of GM starting from 6.0, and it's the absolute simplest way to do it. Quit whining already.

Once again, put this in the Step event like you were told:
Code:
image_angle = point_direction(ob_player.x, ob_player.y, mouse_x, mouse_y);
image_xscale = point_distance(ob_player.x, ob_player.y, mouse_x, mouse_y)/sprite_get_width(sprite_index);
The sprite should be a cone flaring out to the right, and the sprite's origin should be at the starting tip of the cone.

If you choose to be a dinosaur and one that can't even recognize variables to boot, have it your way. But don't complain when the rest of the user community turns their back on you.
 
B

Buddy Theoret

Guest
Ok for one @FrostyCat I am not ''whining'' I am asking for help like these forums are for.. I obviously already put this 💩💩💩💩 in the drawing and the step event and i changed the names like suggested.. I've tried many other ways that I have thought of myself without having to look at all.. & @BattleRifle BR55 i know how to code & use variable's.. did you not read that I already said this??. please, guys lets not start trolling my thread.. i need help with this. not criticism.
& @FrostyCat do you honestly think that I haven't done what these other guys have suggested if i was still coming to this thread and spending time posting more comments??
im obviously asking for another way because this way is NOT working.. maybe you just don't understand that..
il make it easier for you @FrostyCat.. don't post on my thread if you'r just going to be ignorant.. i don't need you to repeat someone else's suggestion with a rude message involved... what i need is another way of doing this trick.. so thanks for trolling but i dont need your help @FrostyCat..

@
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, okay... just CALM DOWN EVERYONE. :)

@Buddy Theoret : While @FrostyCat can be a bit harsh at times with their comments, their heart's in the right place and they do a tremendous amount of work trying to help people on the forums. I would consider carefully what they say and maybe think about why they say it, and I'd refrain from posting to insult other members (some words in your post have been edited by me, btw).

As for your issue, please be more specific. Saying "it is NOT working" doesn't help us help you. What is the code that you've been given doing? In what way isn't it working? Can you share a gif or a video of the issue you are having with the code? You want another way to this, but any other way to do it would involve much more complex methods than those described (for example, creating a textured primitive mesh), and if you are unable to get the given code to work it's unlikely that you'll get anything more complex to work... unless, of course, there really is an issue with the given code, which we can't know because you haven't explained in any detail what happens when you use it.

So, please, relax and understand that these people are trying to help, and while I personally don't subscribe to the "tough love" attitude of some members, I can appreciate that it's frustrating to be giving help and advice to only be told constantly that "it doesn't work".
 
B

Buddy Theoret

Guest
i never even said it didnt work not once yet and he said i was whining?? all i asked was if there was a simpler way to do it and then all of a sudden im getting posts saying that i need to watch tutorials from the beginning on variables and stuff i already know about?? and that im a dinosaur because id prefer 8 over the newer versions?? did i ask for that criticism at all because i dont remember posting about it.
 

rIKmAN

Member
im getting posts saying that i need to watch tutorials from the beginning on variables and stuff i already know about??
If you already know about variables then it's confusing that you would have said "because i dont have any objects named Light & light_direction or light_distance is not a thing that can be used in gml script..." when the code posting was simply using variables named "light_direction" and "light_distance" to store return values from GML functions and had nothing to do with objects at all.

Rather than getting defensive try and explain the exact issue you are having, what you have tried from the code others have posted and why it didn't work (error messages? blank screen? distorted sprite?)

There is nothing wrong with not understanding the basics, but trying to bluff your way through doesn't help anyone - least of all you.

Also not wanting to update is fine and is entirely up to you, but be aware that you will recieve less help and support using such an old version than you will using the most recent version simply because people update and stop using old unsupported software.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
i never even said it didnt work not once yet
Ummm.....

do i need to create global variables for this to work or what.?
This implies that the code being given doesn't work.

another way because this way is NOT working..
You state clearly here that this is not working.

all i asked was if there was a simpler way to do it and then all of a sudden im getting posts saying that i need to watch tutorials from the beginning on variables and stuff i already know about??
Your replies here imply you don't, so it's natural that people try to suggest you go looking at tutorials. And if you are looking for a "simpler" way, there isn't one. What you've been given so far is the simplest way there is to do this, and the fact you want something simpler also implies that you have little experience with GameMaker... which is why (again) you were directed to tutorials. People are actually trying to help you based on the little information you have given. And note that there's nothing wrong with not understanding something! We've all been there! I still struggle with some concepts and I've been programming for 15+ years... :)

and that im a dinosaur because id prefer 8 over the newer versions?? did i ask for that criticism at all because i dont remember posting about it.
While I agree that the phrasing was inappropriate, the sentiment is not, unfortunately... We are 4 versions ahead of GM 8 now (there was 8.1, GM:Html5, then GM:Studio and now GameMaker Studio 2), so you really can't expect people to be able to support such an old version, especially as there have been so many changes between then and now (but people have been trying to help you!).

So... like I said, just saying that what you've been given isn't what you want doesn't help us to help you. We need to know more precisely what is you DO want. Give us examples from other games. Explain why this technique isn't what you want (or isn't working).
 
C

Catastrophe

Guest
Hey, sorry if I caused a ruckus with suggesting tutorials @Buddy Theoret . The fact is that youtube tutorials are an extremely good resource when starting out, one that I use a lot. I still use them to understand things I need to know more of, such as how to do shaders.

I did not mean to imply incompetence or anything, just that you seem to be at a level where you could use more of the beginning tutorials. "Beginner" in the world of programming generally means you have less than 1000 hours of experience in a language ;)

The very idea of even rotating a cone and stretching for instance, not just variables, is something you would probably figure out how to do after watching some tutorials. Such tutorials will teach you a lot of useful functions, (e.g. the point_direction and point_distance functions we used here). Unfortunately, game programming is a complex subject where there is not much point in starting unless you have at least a basic knowledge of some of the more useful library functions.

And the fact is, when people don't do the very basics of tutorials or manual research, we end up getting 100+ long reply threads that are not useful in the long run to the original poster even because they're just asking for one broken thing to fix after the next.

Edit: in any case, before doing some more learning of the language, if you need help as nocturne says, we do actually need to know the problem. If the game crashes, the way to go forward is to paste your crash report (you should be given an option of abort/copy/clear, copy it and paste it here). If it's not a crash, let us know the issue. If it's because the code is two lines instead of the one you want, you'll just have to suck it up, though, haha

If the issue is you need the cone to maintain aspect ratio, simply add
image_yscale = image_xscale
at the end of our posted code.
 
Last edited by a moderator:
& @BattleRifle BR55 i know how to code & use variable's.. did you not read that I already said this??
I did; it was precisely why I recommended more basic tutorials if you were stating that you knew how variables work yet were confused on how to incorporate the two given to you, mistaking them for object names. I wasn't trying to criticize, it's just that what you were saying in one post didn't fall in line with what you were saying in another post, effectively not making any sense to me. I'd rather you start slow but make steady progress rather than work fast and then catch yourself in all sorts of issues with every new thing you try to add.
 
Top