Activating and Deactivating "follow mouse"

Non-platform specific questions.

Activating and Deactivating "follow mouse"

Postby gustavo02 » Sun Apr 20, 2014 9:11 pm

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?
Attachments
gus test.rar
(478.59 KiB) Downloaded 154 times
gustavo02
 
Posts: 6
Joined: Tue Feb 12, 2013 8:22 pm
Score: 0 Give a positive score

Re: Activating and Deactivating "follow mouse"

Postby barney12345 » Mon Apr 21, 2014 2:45 am

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
Darth Vader called... he wants his cookies back
User avatar
barney12345
 
Posts: 122
Joined: Wed Nov 07, 2012 6:52 am
Location: A land down under
Score: 4 Give a positive score

Re: Activating and Deactivating "follow mouse"

Postby lcl » Mon Apr 21, 2014 9:27 am

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)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Activating and Deactivating "follow mouse"

Postby gustavo02 » Mon Apr 21, 2014 7:11 pm

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.
gustavo02
 
Posts: 6
Joined: Tue Feb 12, 2013 8:22 pm
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron