Community
SCI Programming => SCI Community How To's & Tutorials => Topic started by: Cloudee1 on December 24, 2006, 08:41:35 PM
-
This is a question that I recently asked at Mega-Tokyo in which Troflip was good enough to write up some code illustrating that exact functionallity. This is a little snippet of code which I had hoped Troflip could enter here, but as he has not signed up yet and I really didn't want to lose this snippet as I plan to find a use for it, I have moved it here.
Troflip's code to get a view to follow the cursor broken up with small explanations added by me.
We first need to create an instance of an Actor, it needs to be an actor so that it can move around the screen. An init method has been included in the instance telling the actor to do what that script says.
(instance thing of Act
(properties
x 100
y 100
view 800
)
(method (init)
(super:init())
(self:setScript(thingScript))
)
)
Now that the instance is complete, we also need to create the script which the instances init method references.
(instance thingScript of Script
(properties)
(method (doit)
(var myEvent)
(super:doit())
(= myEvent Event:new(evNULL))
(send client:setMotion(MoveTo (send myEvent:x) (send myEvent:y)))
(send myEvent:dispose())
)
)
...and then of course do (thing:init()) in your room's init method.
* View this how-to script in it's entirety here (http://sciprogramming.com/community/index.php/topic,697.msg3744.html#msg3744)
-
So looking at the code for the room1 script in it's entirety as it is posted in the repository, I wonder why it is that I didn't include the doit method in the instance of the actor. I put it in it's own script instance and I'm not sure now that I needed to, makes more sense to have all the bit of code as compact as possible. Also there should be a small memory gain by loading a single instance instead of two different ones.
Looks like it is time to do a little testing to see if there was a reason that I did it the way that I did.
-
If you just want to track the current cursor location (i.e. if you don't need a view to follow the cursor), create a couple of new global variables in your Main.sc:
gTrackMouse = TRUE
gMouseX
gMouseY
Then add the following method in the Script class (Obj.sc):
(method (trackMouse)
(var nullEvent, mouseX, mouseY)
= nullEvent (Event:new(evNULL))
= gMouseX (send nullEvent:x)
= gMouseY (send nullEvent:y)
(send nullEvent:dispose())
)
and modify the doit() method for the Script class as well:
(method (doit)
(var theTime)
(if (== gTrackMouse TRUE)
(self:trackMouse())
)
...
You can now check the current position of the mouse anywhere in your game by referencing the gMouseX and gMouseY global variables. Additionally, you can toggle tracking the mouse movement with the gTrackMouse variable in the Main.sc by setting it TRUE or FALSE, so it can be disabled when you don't need it.
-
Is there a way to check if the cursor is on a certain control color?
I could probably do it by having an invisible object follow the cursor and check if that's on the color, but I feel like that would be a cumbersome and memory-eating way to do it...
On the subject of cursors...is it possible to simulate mouse movement? Can the cursor be moved without the player's input (via script etc)?
EDIT: I want to clarify I'm talking about SCI1.1, but being able to do it in SCI0 would be neat and open up some new possibilities for my future projects.
-
Is there a way to check if the cursor is on a certain control color?
You can get the control (or visual or priority colors) of any point or rect you want:
http://scicompanion.com/Documentation/Kernels/OnControl.html
On the subject of cursors...is it possible to simulate mouse movement? Can the cursor be moved without the player's input (via script etc)?
SetCursor takes an x and y (although I haven't tried it...)
http://scicompanion.com/Documentation/Kernels/SetCursor.html
-
I can't believe I didn't respond to this earlier (it appeared in 2006 and references earlier posts...). SCI0 contains an Event instance called uEvt (in user.sc) from which x and y coordinates can be read. It should work in SCI1.1 as well, though not when modal dialogs or the icon bar are active.
-
As far as SCI0 is concerned... using Studio Syntax...
Clicking on a view:
(if((> (send pEvent:x) (slime:nsLeft))and
(< (send pEvent:x) (slime:nsRight))and
(> (send pEvent:y) (slime:nsTop))and
(< (send pEvent:y) (slime:nsBottom)))
Print("You clicked on a view.")
)// end if item
Or, clicking on a Control Color
(if(== OnControl(ocSPECIAL (send pEvent:x) (send pEvent:y)) ctlSILVER) // Silver Control Color
Print("You clicked on the silver control color.")
)// end if control
In the above snippet, you can change it to check for a visual or priority color by changing the ocSPECIAL....
(if(== OnControl(ocPRIORITY (send pEvent:x) (send pEvent:y)) ctlSILVER) // Silver Priority Color
(if(== OnControl(ocVISUAL (send pEvent:x) (send pEvent:y)) ctlSILVER) // Silver Visual Color
and finally... clicking inside a rectangle...
(if((> (send pEvent:x) 273) and
(< (send pEvent:x) 320) and
(> (send pEvent:y) 0) and
(< (send pEvent:y) 45))
Print("You clicked inside a rectangle.")