Throwing with the Mouse

Posted by Radial543 on July 16, 2007, 4:01 a.m.

Throwing With Mouse

[Throwing stuff is cool]

Throwing objects with a mouse is pretty nifty and can create some pretty unique game ideas. So you wanna know how to do it?

We will be using two objects in this tutorial, a controller (called objCont) and a ball (called objBall). Go create them now and start some coding! Lets start off with the ball object, in the creation event place:

held = true;

This variable basically determines whether the ball is being held, when the ball is created it is being held so the variable is set to true.

Next in the step event place:

if (held=true) {

x = mouse_x;

y = mouse_y;

}

Now we are just checking that if the ball is being held, we set the objects x and y co-ordinates to the current mouse x and co-ordinates.

That is all the code we need for objBall, lets move onto the controller; First we declaire the variables that we will use in the creation event:

mx = 0;

my = 0;

last = 0;

Variables mx and my will be storing the mouse x and y values respectively. You'll see the point of this in the next step. The varable last will store the instance id of the last ball being held, this is so actions can be directly applied to the specific instance of objBall that is being held.

Next is the step event:

if (mouse_check_button_pressed(mb_left)) {

last = instance_create(mouse_x,mouse_y,objBall);

}

if (mouse_check_button_released(mb_left)) {

last.held = false;

last.speed =

sqrt(sqr(mouse_x-mx)+sqr(mouse_y-my));

last.direction =

point_direction(mx,my,mouse_x,mouse_y);

}

mx = mouse_x;

my = mouse_y;

Now, the first if selection statement checks whether the left mouse button has been pressed, and if it is create and instance of objBall with its instance id assigned to the varaible last. This is the method of creating the ball for the sake of this tutorial. In a game you may want a ball to be created if the left mouse button is pressed in a specific reigon or if a object is clicked such as a basket of balls or a mound of snow.

The next selection checks if the left mouse button is released, and thus the ball has been let go. If this is true, then we first set the held variable of the ball to false by referring to it by the instance id stored in last. Next the speed of the ball is calculated, first we find the difference in mouse x and y co-ordinates between the current and previous step. To do this we subtract mx from the current mouse x value and my from the current mouse y value, mx and my will contain the mouse x and y values of the previous step. Then we simply find the maginitude of that vector by using Pythagoras.

Next the direction is assigned from obtaining the direction of the vector from the mouse position of the previous step to the mouse position of the current step.

The last part simply assigns the mouse x and y values to mx and my, notice that this is after the selection statement.

And you're done! Place objCont in a room and have a go pressing the left mouse button, moving the mouse in a direction and letting go of the mouse button. Neat huh?

You may want to decrease the speed of the balls by dividing the Pythagoras result by a number.

Comments

Radial543 16 years, 10 months ago

you were supposed to read all the entries

hippodog 16 years, 10 months ago

Yah, he pmed me too…Fine, I'll graze through your entries…but I'm not reading all of them…unless they are all intresting.

Nighthawk 16 years, 10 months ago

I got PMed as well.

RICANJO 16 years, 10 months ago

I remember this tutorial from MuZzle's website. For more, visit here.

panzercretin 16 years, 9 months ago

I as well got Pm'ed.