Flash 4 scripting part 2 - using get property and set property
simple snap to grid sample

 

the sample:

click here to download the fla file

the way to do it:

- creating the dragable movieclip
first of all i created the red shape as a button symbol ("drag button"), then i created a movieclip ("drag movieclip") into which i placed the button. i dragged the movieclip onto the stage and gave it an instance name ("drag"). back in symbol editing mode for the movieclip i applied the following action to the button:

On (Press)
  Start Drag ("/drag")
End On
On (Release)
  Stop Drag
End On

this makes the movieclip dragable (if you are not sure why we identified the movieclip with /drag then have a look at the menu sample/ using tell target)

- the action script in the main timeline

i don't go into details of how to draw the line and distribute your content on layers etc. but it is all important that you have two frames in the timeline and two keyframes in the layer with the script.

if you have only one frame or a stop action at frame 2 your script will not be executed. when you have two frames Flash loops continuosly between them and executes the script every time it comes to frame 2.

i also created two textfields called xvalue and yvalue which are not necessary for the snap to grid script but allow us to monitor the current x and y position of the movieclip.

the script on frame 2 looks like that:

Set Variable: "xvalue" = GetProperty ( "/drag",_x )
Set Variable: "yvalue" = GetProperty ( "/drag",_y )
If (xvalue > 420)
  Set Property ("/drag", X Position) = "505"
  Set Property ("/drag", Y Position) = "84"
  Stop Drag
End If

if you went through the scripting basics before you should already be able to get the idea what this means. in detail:

- the two Set Variable lines update the text in our variable fields (= redefine the value of the variables) every time Flash plays frame 2. once we chose Set Variable we have to define which variable should be set and what new value we want to give it. since this new value should be the current position of the movieclip we use the GetProperty function to retrieve this value. if you doubleclick the function in the functions selection it already comes up with the right syntax. you substitute the target and the type of property you want to retrieve (which you can again select from the lists in the expression editor)

- with the if statement we check if the movieclip already crossed the line which has an x position of 420. since i already tested for the current x position and stored it in a variable i simply use the variable for the if statement. i could as well use If (GetProperty ( "/drag",_x ) > 420).

if this condition is TRUE then i redefine the x and y position of the clip using Set Property and issue a stop drag command to fix it.

even if you click on the clip again which would make it dragable again the clip will bounce back to its snap position because If (xvalue > 420) is still TRUE and therefore the Set Property commands are issued with every loop through frame 2.

i left it at that which means you could still try to catch the symbol at the moment where Flash is on frame 1 and drag it beyond the line quickly. to prevent this you simply have to duplicate the framescript for frame 1 as well.

bg 20.1.00

 

 

 
1