Getting an Actor to Stop Following the Mouse

Talk about making games.

Getting an Actor to Stop Following the Mouse

Postby plinydogg » Mon Dec 05, 2005 8:33 pm

Okay. Second question. I have looked through the forum posts for an answer, but can't really find one. I initially wanted to make the main actor follow the mouse, but I didn't want it to "teleport" or to stop once it got to the place where the mouse was first clicked (i.e., I didn't want the actor to just "walk" to the one place, but rather to continue to follow the movement of the mouse so long as it remained down).

Piecing together some of the great advice on the forums, I cobbled together a way to get the desired movement. Here's what I did:

(1) Created a filled region actor (called "Sensor") and added the following in the script editor for Sensor->Mouse Button Down (Left):

MoveTo("Ship", xmouse - (view.width/2), ymouse - (view.height/2), 10, "Mouse Position");

This accomplished getting the main actor to go to the place where the mouse first touched down.

(2) Added the same thing in the script editor for MainActor->Mouse Button Down (Left).

(3) Added the following in the script editor for MainActor->Draw Actor:

if (frame > 200)
{
MoveTo("Ship", xmouse - (view.width/2), ymouse - (view.height/2), 10, "Mouse Position");
}

This accomplishes the aim of getting the MainActor to go in the direction of the current position of the mouse rather than where the mouse first touched down (The reason for waiting until at least the 200th frame to execute is that the MainActor is created at startup but I still need to get through a splash screen and main menu. So as long as the code isn't executed at the moment MainActor is first drawn, I'm okay).

Anyways...my question (finally!): The movement accomplished by the steps discussed above is exactly what I need, but I can't get it to ever stop even after I've released the mouse button! Isn't there some way to do it like this (pseudocode):

while(MouseButton is down)
{
Move ship towards current position of mouse;
}
xvelocity = 0;
yvelocity = 0; //Leave the MainActor in the position it was in when the mouse is released

Thanks for your help in advance...I'll try not to clog the forum with silly questions too frequently =)

Plinydogg
plinydogg
 
Posts: 104
Joined: Thu Aug 25, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Tue Dec 06, 2005 12:56 am

In the Mouse Button Up event you can place a Move To action to move the Ship to 0, 0 relative to Ship:

MoveTo("Ship", 0, 0, 1000, "Ship");

So, the Ship will stop on current location.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby plinydogg » Tue Dec 06, 2005 3:56 pm

Thanks for the quick response Makslane, but that doesn't work and I think I know why: in order to get the Ship to follow the mouse's trajectory (instead of just going to the first point at which the mouse button is pressed down), I make the Ship recalculate where it should move every time it is redrawn, which makes it sort of like an infinite loop (it's always being redrawn and so its always recalculating where to move next and then redrawing itself), which makes the Mouseup event not work (I think).

Do you have any other ideas?

Is there some way that I could make the state of the mouse button (up or down) part of an "if" or a "while" statement? (for example, "if mouse button== still down, then redraw Ship). Or perhaps make a short timer that checks the state of the mouse button every few hundred milliseconds? In other words, is there some built in property for the state of a mouse button?

Sorry to keep bothering you...I really appreciate the help.

Plinydogg
plinydogg
 
Posts: 104
Joined: Thu Aug 25, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Tue Dec 06, 2005 8:41 pm

- Create a new integer variable mouseButtonDown
- On Mouse Button Down event, make mouseButtonDown = 1;
- On Mouse Button Up event, make mouseButtonDown = 0;
- On your Draw Actor event:

if(mouseButtonState)
{
//Move the ship here
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby plinydogg » Tue Dec 06, 2005 9:42 pm

It works perfectly! Thanks a million! Now, I promise not to ask any more questions for at least a week :wink:
plinydogg
 
Posts: 104
Joined: Thu Aug 25, 2005 8:34 pm
Score: 0 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest