Search Tutorials
Up to now, all we have done is click on a ball and begun to drag it, now we will see what happens when we drop it. As you can see from the On Release action, when the mouse is released, the movie clip calls some code called OnRelease. Here is what that code says.
Set Variable: "found" = false
Set Variable: "count" = 0
Loop While (count <= 6 )
Set Variable: "count" = count + 1
If (GetProperty ("/ball" & /:ballnum, _droptarget) eq "/target" & count and
GetProperty ("/ball" & /:ballnum, _droptarget) ne "/target" & eval("/:ballpos" & /:ballnum))
Set Property ("/ball" & /:ballnum, X Position) =
GetProperty ("/target" & count, _x)
Set Property ("/ball" & /:ballnum, Y Position) =
GetProperty ("/target" & count, _y)
Set Property ("/balla" & /:ballnum, X Position) =
GetProperty ("/target" & count, _x)
Set Property ("/balla" & /:ballnum, Y Position) =
GetProperty ("/target" & count, _y)
Set Variable: "/ball" & /:ballnum & ":current_targetX" =
GetProperty ("/target" & count, _x)
Set Variable: "/ball" & /:ballnum & ":current_targetY" =
GetProperty ("/target" & count, _y)
Set Variable: "/:last" = count
Set Variable: "/:ballpos" & /:ballnum = count
Set Variable: "found" = true
Call ("/code:CheckResidents")
Call ("/code:Swap")
Else If (found ne true)
Set Property ("/ball" & /:ballnum, X Position) = /:current_targetX
Set Property ("/ball" & /:ballnum, Y Position) = /:current_targetY
End If
End Loop
Set Variable: "num" = 0
Loop While (num <= 6)
If (/:ballnum <> num)
Set Property ("/ball" & num, Visibility) = 1
End If
Set Variable: "num" = num + 1
End Loop
This is basically two loops. The second loop sets our visibility back to 1 for all the other movie clips after the first loop is run, but the first loop is the main one here. That first if statement is pretty long so let me put it in English. If the place you just dropped the ball onto equals a target area, and that target area is not the target you just dragged from then... If this condition is met then the lines following set the ball you just dropped to the same location as the target area you just dropped on. It also sets balla to that same location, so each instance of that movie clip are moved. If the condition is not met, the ball will snap back to its original location (which gets set when you click on it). In addition if the first condition is met there are three other variables that are set, and two more pieces of code that are called. The variable last gets set to the target number you just dropped on (so now we have first and last which are the two target numbers we moved from and to). ballpos1 (or ballpos2, whatever) gets set to the same thing that first gets set to, the target number (so now ballpos1 will equal 3 or whatever target you dropped it onto meaning that ball1 is now in the 3 position). The last thing that gets set is a found variable. This is so the else if statement will work. In other words, if the first condition is met and found gets set to true, then the else if will not run. To be honest I don't know why we can't just have 'else' instead of 'else if' and do away with the found variable, but it doesn't work right if we change it like that. If anyone knows why I would like to know. Now let's go look at the two functions that get called in that last loop. First, here is CheckResidents
Set Variable: "num" = 0
Loop While (num <= 6)
If (Eval ("/:ballpos" & num) = /:last)
Set Variable: "/:resident1" = num
End If
Set Variable: "num" = num + 1
End Loop
Set Variable: "num" = 0
Loop While (num < /:resident1)
If (Eval ("/:ballpos" & num) = /:last)
Set Variable: "/:resident2" = num
End If
Set Variable: "num" = num + 1
End Loop
Stop
What these loops do is find both the number of the movie clip that was just dragged and the number of the movie clip that was already sitting on the target we just dropped on. The first loop will run through and assign resident1 to the first movie clip (the highest number out of six) and the second loop will start at 0 and go up to the first number found, locating the second number and assigning it to resident2. Maybe it makes more sense this way. Each loop takes the target that was just dropped on and compares it to the location of every movie clip. Along the way it find two movie clips whose location are equal to that target, and it assigns resident1 and resident2 to each of those movie clips number. So if a target contains ball1, and we drop ball3 on it, resident1 gets set to 3(ball3) and resident2 gets set to 1 (ball1).
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||
|