Part 1: Movement
Step 1: Create a simple object, like a dot. Convert it to a movie clip, naming it whatever you want. Give it an instance name of dot. Step 2: Right Click and Edit the dot. On it's first frame, give it the action Stop. Step 3: On it's second frame, copy in this code: (You don't have to copy the comments) Comment: Set the position of our dot. Set Variable: x = GetProperty(,_x) Set Variable: y = GetProperty(,_y) Comment: Set the position of a dragged object (we will create the object later) Set Variable: x2 = GetProperty(/drag,_x) Set Variable: y2 = GetProperty(/drag,_y) Comment: Get the distance between the dot and the dragged object Set Variable: xdist = x2-x Set Variable: ydist = y2-y Comment: This is the trickiest part. The dot is going to move at 5 pixels every frame until it reaches the object called /drag. We are checking to see how close it is, so the if the object is less than 5 pixels away, we don't want the dot to move over it. If ((xdist < 5 AND xdist > -5)) Set Variable: xmove = xdist Else Set Variable: xmove = 5 If (x2
-5)) Set Variable: ymove = ydist Else Set Variable: ymove = 5 If (x2 -5)) Set Variable: xmove = xdist Else Set Variable: xmove = 5 If (x2 -5)) Set Variable: ymove = ydist Else Set Variable: ymove = 5 If (x2 x2 OR GetProperty("",_y)<>y2) GoToAndPlay(3) Else GoToAndStop(1) End If Step 6: Now you're finally finished with the dot/character! Now, create another dot and convert it to a symbol. Give it a name, and give it's instance the name drag. Step 7: Edit the new dot, and give it one action on its first frame: Comment: This object will follow the mouse. StartDrag(,lockcenter) Step 8: Now go back to the movie and right-click the new dot. Click Properties. Step 9: Go to Color Effect, select Alpha, and drag the slider to 0. Step 10: Close the properties window and create a rectangle that convers the entire movie. Fill it with any color you want. Step 11: Convert the rectangle to a symbol. Call it whatever you want, and be sure to convert it into a button symbol this time. Step 12: Open the button properties, click the Actions tab, and insert this code: On(Press) Comment: This will make the dot move. Begin Tell Target(/dot) Play() End Tell Target End On Step 13: If you haven't already, place all three objects in the movie. Put the first dot somewhere in the middle, the second anywhere you want, and the rectangle so it covers the movie screen. Set the rectangle's alpha to 0. Test your movie. Step 14: Click anywhere and the dot should move to your cursor. This is a more advanced way of moving than using arrow keys.
PART 2: Collision
Step 15: Draw an object. It can be a tree, a wall, a square. Make it about four times as big as the first dot you drew. Step 16: Convert your work of art into a movie clip. This time it doesn't need an instance name. Just right-click and edit. Step 17: In the first frame, insert this code: Comment: Get my position Set Variable: x = GetProperty(,_x) Set Variable: y = GetProperty(,_y) Comment: Get the dot's position Set Variable: x2 = GetProperty(/dot,_x) Set Variable: y2 = GetProperty(/dot,_y) Comment: Do some math to get the positions of my sides. Set Variable: top = y-(GetProperty("",_height)/2) Set Variable: bottom = y+(GetProperty("",_height)/2) Set Variable: left = x-(GetProperty("",_width)/2) Set Variable: right = x+(GetProperty("",_width)/2) Comment: Check to see if the dot has collided with me. Push it away if it has. If (x2 >= left AND x2 <= right AND y2 <= top AND y2 >= top 10) Set Property (/dot,_y) = top-10 End If If (x2 >= left AND x2 <= right AND y2 >= bottom AND y2 <= bottom + 10) Set Property (/dot,_y) = bottom+10 End If If (y2 >= top AND y2 <= bottom AND x2 <= left AND x2 >= left 10) Set Property (/dot,_x) = left-10 End If If (y2 >= top AND y2 <= bottom AND x2 >= lright AND x2 <= right + 10) Set Property (/dot,_x) = left-10 End If Step 18: Now for the object's second frame, put in this code: GoToAndPlay(1) Step 19: Go back to the movie and make several copies of this object. Scatter them around. Step 20: Test the movie. Walk around a little, then try to walk through one of these squares. See what happens?
|» Level Intermediate|
Rating: 4 Votes: 9
|Teaches how to move characters by clicking and how to make objects that the character can collide with.|
|Download the files used in this tutorial.|
|Download (4 kb)|
|More help? Search our boards for quick answers!|