You can used this technique to fulfill the ActionScript Requirements in Project 3
If you are providing the user with the ability to do something with an element on the movie screen, be sure you give them some written directions about what the element can do and how to interact with it
Drag a movieclip around the stage
Create a new Flash document and save it as dragMovieclip.fla
Insert a new movieclip symbol and name it dragMC
import a graphic into dragMC
Click Scene 1 to edit the movie
Rename Layer 1 dragMC
Click in keyframe 1 of dragMC layer
drag the movieclip dragMC onto the stage in keyframe 1 of the dragMC layer
Save the movie
Create the Actionscript to control the movieclip
Insert a new layer in the timeline called Actions
Open the Actions panel (F11) and group it with the property inspector
Type this ActionScript into the Actions panel
The ActionScript updates the x, y coordinates of the movieclip as it is moved around the stage.
// set listeners for user input
drag_MC.addEventListener(MouseEvent.MOUSE_DOWN, startdrag_MCDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, stopdrag_MCDrag);
drag_MC.addEventListener(Event.ENTER_FRAME, dragdrag_MC);
// offset between movieclip location and click
var clickOffset:Point = null;
// user clicked the movieclip
function startdrag_MCDrag(event:MouseEvent) {
clickOffset = new Point(event.localX, event.localY);
}
// user released the movieclip
function stopdrag_MCDrag(event:MouseEvent) {
clickOffset = null;
}
// run every frame
function dragdrag_MC(event:Event) {
if (clickOffset != null) { // must be dragging
drag_MC.x = mouseX - clickOffset.x;
drag_MC.y = mouseY - clickOffset.y;
}
}
Notice that you can drag the movieclip off the stage and lose it because its movement is not constrained
To constrain the drag and drop to an area within the movie stage, try this ActionScript, which sets up a rectangle that puts a boundary around its movement.
This code is based on ActionScript in How to Cheat in Adobe Flash CS3, by Chris Georgenes, Focal Press, 2008. He allows his code to be reused for educational puirposes.
function startDragHandler(evt:MouseEvent):void {
bird_MC.startDrag(false, new Rectangle(50, 50, 250, 250));
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
}
function stopDragHandler(evt:MouseEvent):void {
bird_MC.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
}
bird_MC.buttonMode = true;
bird_MC.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
Check if cursor collides with movieclip:
Create a new Flash document and save it as collision.fla
Use the movieclip from the last movie to create the cursor object, bird
Create another movieclip symbol or use an old one that will be the collision object (the bird cursor object moves, the collision object is staionary).
Click on the stage
change the name of layer 1 to text to hold the dynamic text
Click in keyframe 1 of the text layer.
select the text tool
type this text and select the text type, dynamic text, in the property inspector
My test is: Did the bird get the fruit? or Collision?
this text is a placeholder, because the actionscript will write the text in the SWF file
dynamic text has a variable name and can change during the movie
Select the dynamic text box and give it an instance name in the property inspector: messageText1
Create a new layer called bird
Click in keyframe 1 of the bird layer
Drag the bird movieclip on the stage
Give it an instance name of bird in the property inspector
Create a new layer for the movieclip, stationary,l that will be the element with which the bird can collide (any point of the bird hits any point on the stationary object)
Drag the stationary movieclip onto the stage in the first frame of the stationary layer
Give it an instance name -- mine is fruit
Create a new layer, actions
Click in keyframe 1 of the actions layer and add this actionscript
This actionscript checks whether the x and y axis of the cursor (the bird) are over the stationary object (the fruit bowl).
If they are., then hitTestPoint will be true
If the two are merely touching but the cursor is not over the fruit then the hitTestPoint will be false.
The hitTestObject code tests whether the edge of the bird touches the edge of the bowl, even if the x, y axis do not
// check the cursor location against the fruit bowl
if (fruit.hitTestPoint(mouseX, mouseY, true)) {
messageText1.text = "Is the cursor x, y axis over the fruit? YES";
} else {
messageText1.text = "Is the cursor x, y axis over the fruit? NO";
}
// test bird versus fruit bowl -- did the edges touch?
if (bird.hitTestObject(fruit)) {
messageText2.text = "did the edge of the bird touch the fruit: YES";
} else {
messageText2.text = "did the edge of the bird touch the fruit: NO";
}
}