Having an Actor/Spaceship follow mouse at varying speeds

Talk about making games.

Having an Actor/Spaceship follow mouse at varying speeds

Postby DavidBeoulve » Sun Apr 10, 2005 5:46 pm

On CreateActor, I create a timer that ticks every 100 milliseconds, or every 10th of a second.

When that timer ticks, I run a script on that actor (in this case, the player's ship)

Remember, you must create the timer on the actor in question. You must create the custom variables I use here in the script editor or they will be "undeclared."

// double distance(double x1, double y1, double x2, double y2)
// Returns distance between points (x1, y1) and (x2, y2).
// double direction(double x1, double y1, double x2, double y2)
// Returns direction between points (x1, y1) and (x2, y2) (in degrees, 0 to 360, from positive x axis, counterclockwise, in degrees).

// first we get where the mouse is, taking into account the size of the view, or this doesn't work right:
MOUSE_X = xmouse - (view.width/2);
MOUSE_Y = ymouse - (view.height/2);

// Then we find the distance...
DISTANCE_TO_MOUSE = distance(x,y,MOUSE_X,MOUSE_Y);

// and set the angle of the actor to move towards the mouse
angle = direction(x,y,MOUSE_X,MOUSE_Y);

// now we change how fast the spacecraft moves towards the mouse based on how far away it is from the mouse.
if (DISTANCE_TO_MOUSE > 200)
{
diretional_velocity = 30;
} else if (DISTANCE_TO_MOUSE > 150 && DISTANCE_TO_MOUSE <= 200)
{
diretional_velocity = 25;
} else if (DISTANCE_TO_MOUSE > 100 && DISTANCE_TO_MOUSE <= 150)
{
diretional_velocity = 20;
} else if (DISTANCE_TO_MOUSE > 50 && DISTANCE_TO_MOUSE <= 100)
{
diretional_velocity = 15;
} else if (DISTANCE_TO_MOUSE <= 20 && DISTANCE_TO_MOUSE > 50)
{
diretional_velocity = 5;
} else if (DISTANCE_TO_MOUSE <= 20)
{
diretional_velocity = 0;
}

// viola. Alter the numbers to take into account the size of your ship graphic -- I bet there's a "Get Width of Actor" function but I don't know it.

It's easy to do. I suck at coding in GE, but this works.
DavidBeoulve
 
Posts: 7
Joined: Sat Apr 09, 2005 3:16 pm
Location: FL, USA
Score: 0 Give a positive score

Re: Having an Actor/Spaceship follow mouse at varying speeds

Postby makslane » Mon Apr 11, 2005 7:51 pm

DavidBeoulve wrote:On CreateActor, I create a timer that ticks every 100 milliseconds, or every 10th of a second.

When that timer ticks, I run a script on that actor (in this case, the player's ship)


You can use the Draw Actor event instead
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DavidBeoulve » Mon Apr 11, 2005 11:12 pm

Ah, thank you :)

I have changed it in my game in progress.

"directional_velocity" miss-spelled? It runs in the game, I copied that variable from the pop-up variable list in GE.
DavidBeoulve
 
Posts: 7
Joined: Sat Apr 09, 2005 3:16 pm
Location: FL, USA
Score: 0 Give a positive score

Postby ingsan » Tue Apr 19, 2005 2:51 pm

Nice coding, David !
Better than simply having the spaceship "sticking" on the mouse pointer :P Thanks
User avatar
ingsan
 
Posts: 580
Joined: Mon Nov 17, 2003 4:02 pm
Location: Maurician LADY in FRANCE
Score: 6 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest