DesktopX: Scripting Motion (Beginner)
From WinCustomize Wiki
| Scripting Motion | ||||||||
| ||||||||
[edit] Scripting Motion
An object’s movement is all about resetting the object’s position rapidly. An object’s position (if not a child object) is determined by setting the top/left coordinates of the object to the x/y positions on the screen.
You can set an object’s top and left positions separately:
Object.left = 1
Object.top = 1
Or you can set them all in one line:
Object.Move 1,1
That’s literally moving the object. What about constant motion? Through the use of a timer we can constantly increase or decrease the object’s top or left position, creating constant motion.
A few things to keep in mind when scripting motion:
- Unless you want the object to keep going and going, you must define where the object starts and stops
- If the object is a child object, the x/y coordinates are relative to the parent object’s height and width.
Here’s a script that will glide the object across the screen, left to right, and then start over again.
'Called when the script is executed Sub Object_OnScriptEnter Object.Move 1,1 Object.SetTimer 1,10 End Sub Sub Object_OnTimer1 stopPos = system.ScreenWidth If Object.left > stopPos Then Object.left = 0 Else Object.Left = Object.Left + 4 End If End Sub
Simple stuff. Here, I made the stop position a variable (stopPos) for clarification, but it's really not necessary. You can reverse the direction, in which case you'd subtract from object.left and set the stop position to 0 (the left boundary of the screen). You could also make the movement vertical by changing object.left to object.top and make it stop at the system.screenheight instead.
Now let’s have some fun. We’re going to make the object bounce off the screen boundaries. Using the same method we used for the fading effect we can keep track of vertical & horizontal direction to increase or decrease the object’s top & left positions.
Dim dirX, dirY 'Called when the script is executed Sub Object_OnScriptEnter dirX = "left" dirY = "up" object.SetTimer 1, 10 End Sub Sub Object_Ontimer1 '--Horizontal Movement-- Select Case dirX Case "left" If object.left =< 0 Then dirX = "right" Else object.left = object.left - 5 End If Case "right" If object.right => system.ScreenWidth Then dirX = "left" Else object.Left = object.Left + 5 End If End Select '--Vertical Movement-- Select Case dirY Case "up" If object.Top =< 0 Then dirY = "down" Else object.Top = object.Top - 5 End If Case "down" If object.Bottom => system.screenheight Then dirY = "up" Else Object.Top = object.Top + 5 End If End Select End Sub
That's it!
Next Tutorial: DesktopX: Scripting Color (Beginner)
