GML - Two corner guns

From Global Programming Syntax

Jump to: navigation, search

In some games you may want to design it so that there are two corner guns. Well in this topic is an example of that. How it works is that the user clicks somewhere in the game window then a bullet will come flying from the bottom left or bottom right corner passing through where the mouse was when the mouse was clicked.

Example Code

Download Link
First create an object named 'obj_presser' (with no sprite) and place a 'Global Left Pressed' event in this object. Inside that event place the following code:

if (( room_width - mouse_x )<( room_width / 2 )) {
instance_create(0,room_height,obj_bulletz);
} else {
instance_create(room_width,room_height,obj_bulletz);
}
Then place that object anywhere in the rooms which is going to have these guns. After that, you will need to make another object called 'obj_bulletz' and add a create event into this object. In the create event place the following code:
{
var xx, ratio;
if (x<( room_width / 2 )) {
ratio=mouse_x/( room_height - mouse_y );
xx=( mouse_y * ratio ) + mouse_x;
} else {
ratio=( room_width - mouse_x)/( room_height - mouse_y );
xx=mouse_x - ( mouse_y * ratio );
}
move_towards_point(xx,0,12);
}
Then make a 'Outside Room' Event in the obj_bulletz object and place the following code in that event:
instance_destroy();

Also note that you do not need to place obj_bulletz inside the room as they will automatically be created.

Personal tools
languages
page stats
Toolbox