Start your Flash software. Select the "New" option from the "File" menu on the main Flash menu bar. Select "ActionScript 3" from the dialog box that appears to create a new AS3 flash file.
Select "Actions" from the "Window" menu on the main flash menu bar. Position your mouse cursor on line 1 in the editor.
Declare a circle and rectangle graphic movie object using AS3 code. Attach the drawRect and drawCircle graphics methods to movie clip objects that you name "ball" and "squareColor" to draw a rectangle and circle on the stage (screen). Use the "addChild" method to place the circle and rectangle on the stage. Type the following codes of lines in the Actions editor to draw and place these objects.
var color: uint = 0xff00ff;
var ball:MovieClip= new MovieClip();
ball.graphics.beginFill(color);
ball.graphics.drawCircle(100,100,100)
ball.graphics.endFill()
stage.addChild(ball)
var squareColor:Shape= new Shape();
squareColor.graphics.lineStyle(1)//make sure the lineStyle command precedes the drawCircle command
squareColor.graphics.beginFill(color);
squareColor.graphics.drawRect(200,200,300, 300)
squareColor.graphics.endFill()
stage.addChild(squareColor)
Add a "MOUSE_DOWN" event listener to the ball (circle) object with the addEventListener method. Name the function "itemSelected_fn" that the event listener calls. Code the itemSelected function such that when the mouse button is pressed down when over the ball object, you can drag the mouse by holding down the mouse button and physically moving the mouse. Also code an event listener in this function that will call a function named itemDeSelected_fn when the mouse button is released (also known as MOUSE_UP). Type the AS3 code listed below in the Action's editor on the next line after the code already typed in to accomplish this.
ball.addEventListener(MouseEvent.MOUSE_DOWN, itemSelected_fn)
function itemSelected_fn(myEvent:MouseEvent): void
{
myEvent.target.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, itemDeSelected_fn);
}
Code the dropTarget method in a function named itemDeselected such that the code will remove the ball from the stage if the ball is placed in the squareColor object and the mouse button is released. Also code the function such that it will display the text message "Item Returned, No Sale" if the mouse button is released when the ball is not within the boundaries of the squareColor object. Type in the code below following the other lines already entered to accomplish this.
itemDeSelected_fn(myEvent:MouseEvent):
void{
if (ball.dropTarget == squareColor) {stage.removeChild(ball)} else {trace("Item Returned, No Sale")
}
Review the entire code entered, as listed below, for syntax errors and functionality. Select the "blue check mark" icon on the Actions' editor menu bar to have ActionScript check for syntax errors.
var color: uint = 0xff00ff;
var ball:MovieClip= new MovieClip();
ball.graphics.beginFill(color);
ball.graphics.drawCircle(100,100,100)
ball.graphics.endFill()
stage.addChild(ball)
var squareColor:Shape= new Shape();
squareColor.graphics.lineStyle(1)//make sure the lineStyle command precedes the drawCircle command
squareColor.graphics.beginFill(color);
squareColor.graphics.drawRect(200,200,300, 300)
squareColor.graphics.endFill()
stage.addChild(squareColor)
ball.addEventListener(MouseEvent.MOUSE_DOWN, itemSelected_fn)
function itemSelected_fn(myEvent:MouseEvent): void
{
myEvent.target.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, itemDeSelected_fn);
}
Select the "TestMovie" option from the "Control" menu on the main Flash menu bar to test the movie. Position your mouse cursor over the ball, depress the right mouse button and drag the ball to a new position that is not over the square. Release the mouse button and observe that the text message "Item Returned, No Sale" appears. Position your mouse cursor over the ball again, depress the right mouse button and drag the ball on top of the square. Release the mouse button and observe that the ball is removed from the screen.