var _direction,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);
//draw a sprite here
draw_sprite_ext(spr_gun,0,x+_xcomponent,y+_ycomponent,1,1,_direction,c_white,1);
//or place an object
with (obj_gun) {
x = other.x + _xcomponent;
y = other.y + _ycomponent;
image_angle = _direction;
}
#Newbie here!
122 messages · Page 1 of 1 (latest)
well, it would need to go in the draw event if you go with the draw sprite variant
if you dont use that, and use the with(obj_gun) instead, it can go in the step
var _direction,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);
//draw a sprite here
draw_sprite_ext(spr_gun,0,x+_xcomponent,y+_ycomponent,1,1,_direction,c_white,1);
///EITHER THIS OR THAT
var _direction,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);
with (obj_gun) {
x = other.x + _xcomponent;
y = other.y + _ycomponent;
image_angle = _direction;
}
whats the difference between draw and craete?
and i had another question, how do the 'draw_rectangle () ' works, im trying to make a dialogue box but i dont understand what ineed to put inside of the ()
the create event only executes once, when the instance is created
when your game runs at 30 steps a second or whatever, all logic in the step and draw events and so on will execute 30 times a second
that means we also need to redraw everything every time. if you use draw functions outside of the draw event, they wont show up (there are exceptions that arent relevant rn) .
if you could draw in the create event, it would only show up for 1 frame
i just gave you an option here, since you already said your gun is a seperate object.
thats good and all, a lot of people do it like this, its just not optimal but it might be easier for you to work with objects
ok
but if you think about it, the gun probably doesnt need to do a whole lot, you can put your shoot logic into the player, so the gun really only ever has to draw itself right
thats why we could also use the draw_sprite instead, and directly draw the gun in the player object.
one thing you need to know is, when you put code in the draw event, itll override the "default" drawing of the instance.
to get around that, you want to put draw_self() in there, so the player object still draws itself
oh, thats why he disappears
when you get a little more experienced you might even do away with the default drawing alltogether, and just use the draw functions for everything
more control, you can draw individual limbs and so on
depending on where you put the draw_self(), before or after the gun code, it will draw ontop or behind the gun
draw_self() is exactly the same as
draw_sprite_ext(sprite_index, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
i probably did something wrong, but the chaacter is still without the gun
did you make sure you have a sprite named spr_gun
no, but idk if its necessary. i have a suspicion you put the code inside the obj_gun you have, instead of the player
Yes
its supposed to go into the player
and, there is an issue
and then you dont even need an obj_gun
wait a sec
take a look again. if you use the top example, you dont need an obj_gun.
if you want to go the other route, use the bottom code.
Either way, the code goes into the player draw event
alright. i guess its easier if you work with objects.
so we are gonna do stuff a little bit different
ok
are you creating the gun in the player create event?
if so, we want to save the instance into a variable to reference later.
because if there is ever more than one obj_gun on screen, stuff is gonna get weird.
///obj_player
//create event:
my_gun = instance_create_depth(x, y, depth-1, obj_gun); //we reference this specific gun
//step event:
var _direction,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);
with (my_gun) { //now we are sure to only move the players "personal" gun
x = other.x + _xcomponent; //no matter how many obj_gun's are in the scene
y = other.y + _ycomponent;
image_angle = _direction;
}
//draw event:
draw_self();
there is no more than two, i dragged the gun into the room, and now i put a draw event in the gun, thats why there are two
imma fix it
ah well, remove the gun from the room
and update your player code with the new code
the create code will make sure to spawn a gun for the player once he is created
i delete the draw_self from the gun?
none of my code was ever meant to go inside your obj_gun
ok
if you have an emty draw event in the gun, it wont show
either remove the draw event entirely, or use draw_self() in there
by adding a draw event to an object, your telling gamemaker "hey, im gonna take matters into my own hands"
removed
got it!
(:
sometimes gamemaker is hard to understand
and im only just aweek
it has some specific quirks
but they make sense once accustomed to them
most of the time
i really want to thank you, you are using your time to help me
all good, its good to get away from your own project once in a while
you look like a proffesional
professional
how do you spell it
english is not my first language, sometimes i get stuck in some words
one more thing, if you dont want the player to have the gun from the start, you could create an object
obj_gun_pickup.
if we restructure a bit
//player create event:
my_gun = noone; // we initialize the weapon variable;
//collision event obj_player with obj_gun_pickup
if (!instance_exists(my_gun)) { //if we are not already holding a gun, we create one, and delete the pickup
with (other) {
instance_destroy();
other.my_gun = instance_create_depth(x, y, other.depth-1, obj_gun);
}
}
so you create obj_gun_pickup, give it the gun sprite, and thats it
then you place it in the room somewhere, and if you walk into it, the player should " pick it up"
how do i fix, the gun is not on his hand, what do i need to change?
i think i know how to put this in the game
give it a try, replace the create event and add an collision event
this can be quite a difficult problem, but it depends. is the hand moving relative to the player? or is it fixed position.
if its a fixed position, it just comes down to finding the right offset values.
if your arms can move, then you are looking into Inverse Kinematics (spooky math)
fixed position, fortunately
no, fixed is good
oh
because else it gets way more complicated
how are you changing direction of your player
image_xscale?
yep
gotcha
im sorry for the wait, im having a brainfart for the _facing variable, this will do for now tho
var _direction,_facing,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_facing = 0;
if (image_xscale < 0) {
_facing = 180;
}
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);
yuh thats what its there for. dust off the cobwebs when your at it
not much traffic there
where i need to put this?
got it, this is better.
//player step event:
var _direction,_facing,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_facing = 90 - (sign(image_xscale)*90);
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);
with (my_gun) { //now we are sure to only move the players "personal" gun
x = other.x + _xcomponent; //no matter how many obj_gun's are in the scene
y = other.y + _ycomponent;
image_angle = _direction;
}
replaces the old step event code
makes the gun draw to the left or right side, based on your image_xscale
by adjusting the values for _xcomponent and _ycomponent, you can move the gun about relatively to the player. needs a bit of fiddling around on your end till you found the right spot where the gun should be
it hasnt changed much
the devil is in the details
ok but
i think i need to chnage some xs and ys
when you move left right, it should flip around right
oh it does, but, your sprite origins arent set to center
also, as i said, you need to adjust the values for x_component and _ycomponent
the 32, you can change that
also, here is a final rewrite, so the guns xscale changes with the players
//player step event:
var _direction,_facing,_xcomponent,_ycomponent;
_direction = point_direction(x,y,mouse_x,mouse_y);
_facing = 90 - (sign(image_xscale)*90);
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);
with (my_gun) { //now we are sure to only move the players"personal"gun
x = other.x + _xcomponent; //no matter how many obj_gun's are in the scene
y = other.y + _ycomponent;
image_angle = _direction;
image_xscale = abs(image_xscale)*sign(other.image_xscale); //this should mirror the gun sprite
}
sorry, was playing hour zero, btw you did that alone?