Narhiz
Member
Hi. Recently I tried to get my character in game to pick up a weapon and drop it by pressing the F key. I was able to do this thanks to the help on this forum. But the selection of weapons took place in such a way that I simply replaced the standard sprite of my character with a sprite with a weapon and removed the weapon from the room. When the character dropped the weapon, his sprite changed back to the standard one and a new weapon appeared on the floor near the character. Now I want to implement this mechanic differently. It is necessary that when picking up a weapon, its sprite is replaced with another (when the character is holding it in his hand) and it is transferred from the floor to the character's hand. When throwing, it is necessary that a new weapon is not created, as in the old way, but that the weapon is moved from the hands to the floor and the sprite is replaced with the original one. In this case, after selection, it is necessary to make the weapon move and rotate with the character. I've tried using the lengthdir_x / y functions to implement the fit, but either they don't work or I'm using them incorrectly. I have not thought about synchronizing the movement and rotation of the weapon with the character yet, since it makes no sense without the right selection. Tell me, in which direction should I go to solve this problem? You do not need to upload the finished code, describe in general terms the principle of operation and what functions I should study.
Player Step Event:
Player Create Event:
P.S:
The text was written using google translate, so it may contain inaccuracies.
Player Step Event:
GML:
// Movement
if (keyboard_check(ord("D"))) x += 4;
if (keyboard_check(ord("A"))) x -= 4;
if (keyboard_check(ord("W"))) y -= 4;
if (keyboard_check(ord("S"))) y += 4;
image_angle = point_direction(x, y, mouse_x, mouse_y);
// PickUp and drop the gun
if keyboard_check_pressed(ord("F"))
{
if (player_state == PLAYER_STATE.emptyhanded)
{
var pos_gun = instance_place(x, y, obj_gun);
if (pos_gun != noone)
{
lendir_x = lengthdir_x(36, image_angle)
lendir_y = lengthdir_y(10, image_angle)
player_state = PLAYER_STATE.holdingweapon
obj_green_man.sprite_index = spr_green_man_gun
with (pos_gun)
{
image_angle = point_direction(x ,y, mouse_x, mouse_y)
pos_gun.sprite_index = spr_holding_gun
x = obj_green_man.x + obj_green_man.lendir_x
y = obj_green_man.y + obj_green_man.lendir_y
}
}
}
else
if (player_state == PLAYER_STATE.holdingweapon)
{
player_state = PLAYER_STATE.emptyhanded
obj_green_man.sprite_index = spr_green_man
direction = point_direction(x, y, mouse_x, mouse_y);
var xx = x + lengthdir_x(20, direction);
var yy = y + lengthdir_y(20, direction);
instance_create_layer(xx, yy, "Guns", obj_gun)
}
}
GML:
enum PLAYER_STATE
{
emptyhanded,
holdingweapon
}
player_state = PLAYER_STATE.emptyhanded
The text was written using google translate, so it may contain inaccuracies.