Button
Top  Previous  Next


Description
This is a step-by-step tutorial for creating a button that executes script when pressed.

Aim
This tutorial introduces the procedure for making a simple button and demonstrates the on(press) Button Event.

.swi file
"button.swi"

1.Start with a new project (File | New) and save the file as "mybutton.swi"  
 
2.Create a button.  
 
Select the Layout Panel and use the Rectangle tool to create a small rectangle or square.  

scripttute3_1

Use the Text Tool to create the static text "=".  

scripttute3_2

Move the static text over the rectangle, then select both the text and the shape. Use the Modify menu and select Grouping then Group as Button.  
scripttute3_3

Name the button b1 and select the checkboxes as shown below  
scripttute3_4

3.   Add some script to indicate when the button is pressed. Ensure the 'b1' button is selected in the 'Outline' Panel. Select the 'Script' Panel and use the 'Add Script' button to add an on(press) Button Event for the button. This is entered via the Menu items Events | Button | on(press).

scripttute3_5

The resulting script should be displayed:  
scripttute3_6
Add the trace statement trace("Button Released"); using the Menu commands Add Script | Debugging | trace(...). The resulting script should look like:  
scripttute3_7

4.Test the button by selecting the Debug tab among the Panels displayed on the right hand side of the 'Layout' Panel. Press the 'Play' button to run your Movie. Each time the left mouse button is pressed while over the button, the statement "Button Pressed" should appear in the 'Debug' Panel  
 

scripttute3_8

Analysis
Two Objects, a shape and a static Text Object, were grouped and made into a button using the Group as button command.

Once the button was created, an on(press) Button Event was added. The statements contained within this Event routine execute each time the left mouse button is pressed while the mouse is positioned over the button.

A trace statement was added to the on(press) Button Event to demonstrate the button Action in the 'Debug' Panel.