Page 1 of 1

Activating and Deactivating "follow mouse"

PostPosted: Sun Apr 20, 2014 9:11 pm
by gustavo02
tibia2.jpg

Hello, maybe this is a really noob question but since I'm a noob at programming, I guess this is normal.

Here is what I want to do:
I have a small ball in the center of screen and I want to control it with the mouse. The "follow mouse" function works perfectly.
But I want the ball to follow the mouse only in an specified area, this would be the black are of the screen (image attached).
I don't want the ball to go move over the walls.

So, to make the ball follow the mouse only in a specified region, I tried this:
I made a "Draw Actor" event in the ball and set "script editor" as the action.
In the script, I wrote:

Code: Select all
if (-400 <= xmouse <= 200)
{
    FollowMouse("Event Actor", X_AXIS);
}

if (xmouse > 200)
{
    FollowMouse("Event Actor", NONE_AXIS);
}


So the ball would stop following the mouse on the X AXIS when the xmouse position were higher than 200 (which corresponds to the wall region).
But when I do this the ball simply doesn't move.
And if I use "else if" in place of the second "if", the ball starts following the mouse but won't stop even when xmouse is higher than 200.

So, any idea of what I'm doing wrong?

Re: Activating and Deactivating "follow mouse"

PostPosted: Mon Apr 21, 2014 2:45 am
by barney12345
I know what probably is the long and winded way that doesn't use what you've done where you create another actor which follows the mouse, create a variable called whatever and create a wire frame region that covers the black area. You put collision on any side of the grey area with the new actor and make the variable 1. You make collision on any of the wire frame region with the new actor to change the variable to 0 and put change the code in the ball actor to only follow the mouse when variable =0.

That should work but there is probably someone who has an easier way

Re: Activating and Deactivating "follow mouse"

PostPosted: Mon Apr 21, 2014 9:27 am
by lcl
One problem is that you can't have if conditions like this one:
Code: Select all
if (-400 <= xmouse <= 200)

You can't have two conditions tied up as one condition, as you've done above. You should instead write:
Code: Select all
if (xmouse >= - 400 && xmouse <= 200)

In this code && means simply 'and'. There is also an operator for 'or' which is ||

Another problem in your code is that xmouse is never negative. It has different coordinate system than actors have. xmouse presents the cursors xscreen, which is the dkistance from the left edge of the view actor, e.g. the screen. ymouse then represents the distance from the top edge of view.

You can either adjust the values to match with how xmouse acts or then you can use the actor's x (the ball's x) for the conditions.

Also, in a case like this, use else if in the second if.

(I wrote this.on my phone, so excuse any spelling mistakes)

Re: Activating and Deactivating "follow mouse"

PostPosted: Mon Apr 21, 2014 7:11 pm
by gustavo02
Thanks, barney. I haven't tried that yet, but it seems like an interesting way to do things, I may use that way in some other situations too.

Thanks, lcl, I did what you said and now it works exactly as I want. Nice explanation too.