i need help (noob)

Y

ynckw

Guest
Hello

I created a 2d space shooter in which the player can be controlled with a joystick (mobile game). the player looks in the direction in which the joystick is used (mouse point). now i need a button to shoot . I have created an object that creates the sprite (GUI) at a certain point so that it always stays in the same place even when the camera is moving. how do I get it that the player does not move in the direction of the mouse but only in the direction of the joystick so that I can press the button with my other finger to shoot without the player then immediately turning the shot button.

i made the shoot on windows with space bar:

if (keyboard_check_pressed(vk_space)) {
create_bullet(image_angle, bulletSpd, faction, guns)

}

but why does not work this?

if mouse_check_button_pressed(mb_left) {
if collision_point(mouse_x, mouse_y, obj_shoot, true, false) {
create_bullet(image_angle, bulletSpd, faction, guns)
}
}

thank you
 
Y

ynckw

Guest
obj_shoot = shoot button but with no sprite (gui sprite created)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
GUI coordinates and mouse coordinates are not the same thing, as you can have multiple different resolutions in play, since the GUI layer is a fixed absolute value while the mouse wil depend on where in the room and the camera view the click is happening. Try changing this line:

GML:
if collision_point(mouse_x, mouse_y, obj_shoot, true, false) {
To this:

GML:
var _mx = device_mouse_x_to_gui(0);
var _my = device_mouse_y_to_gui(0);
if collision_point(_mx, _y, obj_shoot, true, false)
{
// Do something
}
So, assuming the object "obj_Shoot" is positioned correctly on the GUI layer, this will now detect the mouse clicks.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, so the obj_shoot doesn't have a sprite attached to it in the object propetties? In that case, instead of collision_point, use the function point_in_circle. So, create the object obj_Shoot at the position you require in the GUI layer then use the code:

GML:
var _mx = device_mouse_x_to_gui(0);
var _my = device_mouse_y_to_gui(0);
if point_in_circle(_mx, _my, obj_shoot.x, obj_Shoot.y, 32) // change 32 to the value required for the button
{
// Do something
}
 
Y

ynckw

Guest
i created an obj_shoot with: (obj_shoot with no sprite/ collison maks = spr_shoot, visible)

create event:

joystickStableX = 150
joystickStableY = 930


Step event: (should i code it to the obj_player where the space bar code is?)

var _mx = device_mouse_x_to_gui(0);
var _my = device_mouse_y_to_gui(0);
if point_in_circle(_mx, _my, obj_shoot.x, obj_shoot.y, 150)
{
create_bullet(image_angle, bulletSpd, faction, guns)
}

GUI Draw event:

draw_set_alpha(0.7)
draw_sprite(spr_shoot,0, joystickStableX,joystickStableY)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Looking at that code it just needs a minor tweak to work:

GML:
var _mx = device_mouse_x_to_gui(0);
var _my = device_mouse_y_to_gui(0);
if point_in_circle(_mx, _my, joystickStableX , joystickStableY, 150)
{
create_bullet(image_angle, bulletSpd, faction, guns)
}
 

HayManMarc

Member
I wouldn't put the ship sprite on the gui layer to begin with. I find it best to use the gui layer for what it is intended for: gui stuff.

You can easily manipulate the ship position to stay centered in the screen by using viewport values. This in turn makes creating bullets from this position much easier as the coordinates are already room coordinates along with the rest of the stuff in the room, not gui coordinates that you need to translate to room coordinates.

It just seems like you're taking a few unnecessary steps and convoluting your code. Seems like you're trying to make it simpler by just slapping it on the gui layer to keep it centered, but its actually causing more difficulties for you in the long run.
 
Y

ynckw

Guest
OK. I wrote the codes in the player script. if I now move the mouse over the button (without pressing), it shoots continuously.

if i could get in contact with you on discord i could send you the file

i think i leave it with the mobile control and stay on pc level.
 
Top