Monday, October 25, 2010

Create an ActionScript 3 Scrolling Video Menu

Flash Player Actionscript 3 Flash Video

The ScrollPane component is used to to display images, and SWF files, but to assign functions to nested clips inside The ScrollPane is a real pain. In this post we will design a scroll pane and use the contents as a menu for a FLVPlayback component. The original script comes from flashconf.com, which was just a scroller but we will add buttons inside to control a FLVPlayback component.

The Scroller and Mask

  1. Get the TweenLite engine and save it in the same folder to access the classes
  2. Draw a rectangle 200×250. This will act as a mask for the content.
  3. Convert the rectangle to a movie clip. Set the registration point to the top left corner.
  4. Give the movie clip an instance name of myMask.
  5. Draw a line of size 1×250 on the right side of the stage do not convert to movie clip.
  6. Draw a circle 20×20. Convert it to a movie clip. Set registration point to center.
  7. Give the circle an instance name of scrollMC.
  8. Put the circle on the top of the line like below.
scrollbar

Create the content movie clip

  1. Create a movie clip out of a rectangle the same width as the mask.
  2. The height can be any amount as only the mask size portion will be shown..
  3. Set the registration point to the top left corner.
  4. Give the content an instance name of myContent
  5. Add images to use as thumbnails for your video's and convert them to buttons
  6. All functions are on the parent timeline.
  7. Use movieClip.movieClip format to access the buttons inside the child MC.
  8. Ensure all instance names are completed for the buttons and movie clips
  9. Paste or type the below code.
  10. The tweens blur can be adjusted inside the tweenFinished function.

The Video Player

  1. Place an FLVPlayback component on stage with instance the name of myMovie.
  2. Choose a skin or use video controls.
  3. Give all controls instance names.
  4. Set the controls to the FLVPlayback component.
  5. Set the initial video, volume level and stop autoplay.

The Final Code

//Import TweenMax and the plugin for the blur filter
import com.greensock.TweenMax;
import com.greensock.plugins.BlurFilterPlugin;
import flash.display.*;

myMovie.playPauseButton = playPause;
myMovie.stopButton = stopBtn;
myMovie.seekBar = myBar;
myMovie.source = "http://www.yoursite.com/yourVideo1.flv";
myMovie.autoPlay = false;
myMovie.volumeBar = volBar;

myContent.one_btn.addEventListener(MouseEvent.CLICK, playVid);
myContent.two_btn.addEventListener(MouseEvent.CLICK, playVid2);
myContent.three_btn.addEventListener(MouseEvent.CLICK, playVid3);

function playVid(e:MouseEvent):void{
myMovie.source = "http://www.yoursite.com/yourVideo1.flv";
}

function playVid2(e:MouseEvent):void{
myMovie.source = "http://www.yoursite.com/yourVideo2.flv";
}

function playVid3(e:MouseEvent):void{
myMovie.source = "http://www.yoursite.com/yourVideo3.flv";
}

//Save the content's and mask's height.
//Assign your own content height here!!
var CONTENT_HEIGHT:Number = 940;
var MASK_HEIGHT:Number = 250;

//We want to know what was the previous y coordinate of the content (for the animation)
var oldY:Number = myContent.y;

//Position the content on the top left corner of the mask
myContent.x = myMask.x;
myContent.y = myMask.y;

//Set the mask to our content
myContent.mask = myMask;

//Create a rectangle that will act as the bounds to the scrollMC.
//This way the scrollMC can only be dragged along the line.
var bounds:Rectangle = new Rectangle(scrollMC.x,scrollMC.y,0,250);

//We want to know when the user is scrolling
var scrolling:Boolean = false;

//Listen when the user is holding the mouse down on the scrollMC
scrollMC.addEventListener(MouseEvent.MOUSE_DOWN, startScroll);

//Listen when the user releases the mouse button
stage.addEventListener(MouseEvent.MOUSE_UP, stopScroll);

//This function is called when the user is dragging the scrollMC
function startScroll(e:Event):void {

//Set scrolling to true
scrolling = true;

//Start dragging the scrollMC
scrollMC.startDrag(false,bounds);
}

//This function is called when the user stops dragging the scrollMC
function stopScroll(e:Event):void {

//Set scrolling to false
scrolling = false;

//Stop the drag
scrollMC.stopDrag();
}

//Add ENTER_FRAME to animate the scroll
addEventListener(Event.ENTER_FRAME, enterHandler);

//This function is called in each frame
function enterHandler(e:Event):void {

//Check if we are scrolling
if (scrolling == true) {

//Calculate the distance how far the scrollMC is from the top
var distance:Number = Math.round(scrollMC.y - bounds.y);

//Calculate the percentage of the distance from the line height.
//So when the scrollMC is on top, percentage is 0 and when its
//at the bottom the percentage is 1.
var percentage:Number = distance / MASK_HEIGHT;

//Save the old y coordinate
oldY = myContent.y;

//Calculate a new y target coordinate for the content.
//We subtract the mask's height from the contentHeight.
//Otherwise the content would move too far up when we scroll down.
//Remove the subraction to see for yourself!
var targetY:Number = -((CONTENT_HEIGHT - MASK_HEIGHT) * percentage) + myMask.y;

//We only want to animate the scroll if the old y is different from the new y.
//In our movie we animate the scroll if the difference is bigger than 5 pixels.
if (Math.abs(oldY - targetY) > 5) {

//Tween the content to the new location.
//Call the function tweenFinished() when the tween is complete.
TweenMax.to(myContent, 0.3, {y: targetY, blurFilter:{blurX:2, blurY:2}, onComplete: tweenFinished});
}
}
}

//This function is called when the tween is finished
function tweenFinished():void {

//Tween the content back to "normal" (= remove blur)
TweenMax.to(myContent, 0.3, {blurFilter:{blurX:0, blurY:0}});
}


ActionScript 3 Basic Mouse Events

Flex Builder 4 Flash Video Mouse Events

Click on the SWF or move the mouse to activate the text.

The code below shows how the text in a dynamic text field responds to the user moving and clicking with the mouse.

  • //Make a dynamic text field named mouse_events  
  • //The stage will listen for each event and display the related text.  
  •   
  • stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down);   
  • stage.addEventListener(MouseEvent.MOUSE_UP, mouse_up);   
  • stage.addEventListener(MouseEvent.MOUSE_MOVE, moving);  
  • stage.addEventListener(MouseEvent.MOUSE_OUT, leave);  
  •   
  • function moving(event:MouseEvent)   
  • {   
  •     mouse_events.text = "Your mouse is moving";  
  • }   
  • function mouse_down(event:MouseEvent)   
  • {   
  •     mouse_events.text = "The mouse button is down";  
  • }   
  • function mouse_up(event:MouseEvent)   
  • {   
  •     mouse_events.text = "Mouse button is up again";  
  • }
  • function leave(event:MouseEvent)   
  • {   
  •     mouse_events.text = ""; //No text  
  • }

CLICK : MouseEvent.CLICK
Used to detect mouse clicks.

DOUBLE_CLICK : MouseEvent.DOUBLE_CLICK
Used to detect double clicks.

MOUSE_DOWN : MouseEvent.MOUSE_DOWN
Checks when mouse is pressed down.

MOUSE_LEAVE : MouseEvent.MOUSE_LEAVE
Monitors when the mouse leaves the stage.

MOUSE_MOVE : MouseEvent.MOUSE_MOVE
Monitors when the mouse moves.

MOUSE_OUT : MouseEvent.MOUSE_OUT
Monitors when the mouse moves out of the attached to object of the event.

MOUSE_OVER : MouseEvent.MOUSE_OVER
Monitors when the mouse moves over the attached to object of the event.

MOUSE_UP : MouseEvent.MOUSE_UP
Monitors when the mouse moves up the attached to object of the event from a click.

MOUSE_WHEEL : MouseEvent.MOUSE_WHEEL
Monitors when the mouse wheel moves, detect the positive or negative delta property for distance and direction moved.

ROLL_OUT : MouseEvent.ROLL_OUT
Dispatched when the user moves a pointing device away from an InteractiveObject instance.

Thursday, October 21, 2010

Make A Movie Clip Follow Mouse Clicks Using ActionScript 3

Flex Builder 4 Flash Video Mouse Events

Click on the stage and the ball will follow the mouse click.


The Code

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;

var xMovement:Tween;
var yMovement:Tween;

function Start():void
{
stage.addEventListener(MouseEvent.CLICK, moveToClick);
}

function moveToClick(event:MouseEvent):void
{

xMovement = new Tween(circle_mc,"x",Back.easeIn,circle_mc.x,mouseX,.7,true);
yMovement = new Tween(circle_mc,"y",Back.easeIn,circle_mc.y,mouseY,.7,true);
}

Start();

The fl.transitions.package

The fl.transitions.package contains classes that let you use ActionScript to create animation effects. You use the Tween and TransitionManager classes as the primary classes for customizing animation in ActionScript 3.0.

Blinds The Blinds class reveals the movie clip object by using appearing or disappearing rectangles.
Fade The Fade class fades the movie clip object in or out.
Fly The Fly class slides the movie clip object in from a specified direction.
Iris The Iris class reveals the movie clip object by using an animated mask of a square shape or a circle shape that zooms in or out.
Photo Makes the movie clip object appear or disappear like a photographic flash.
PixelDissolve The PixelDissolve class reveals reveals the movie clip object by using randomly appearing or disappearing rectangles in a checkerboard pattern.
Rotate The Rotate class rotates the movie clip object.
Squeeze The Squeeze class scales the movie clip object horizontally or vertically.
Transition The Transition class is the base class for all transition classes.
TransitionManager The TransitionManager class defines animation effects.
Tween The Tween class lets you use ActionScript to move, resize, and fade movie clips by specifying a property of the target movie clip to animate over a number of frames or seconds.
TweenEvent The TweenEvent class represents events that are broadcast by the fl.transitions.Tween class.
Wipe The Wipe class reveals or hides the movie clip object by using an animated mask of a shape that moves horizontally.
Zoom The Zoom class zooms the movie clip object in or out by scaling it in proportion.

 

ActionScript 3.0 Reference

Wednesday, October 20, 2010

Copy to Clipboard Using ActionScript 3

Flex Builder 4 Flash Video



setClipboard()

Replaces the contents of the Clipboard with a specified text string. This method works from any security context when called as a result of a user event (such as a keyboard or input device event handler). This method is provided for SWF content running in Flash Player 9. It allows only adding String content to the Clipboard.. Three-dimensional display objects follow the pointer and Sprite.startDrag() moves the object within the three-dimensional plane defined by the display object. Or, if the display object is a two-dimensional object and the child of a three-dimensional object, the two-dimensional object moves within the three dimensional plane defined by the three-dimensional parent object.

flash.desktop Clipboard

The Clipboard class provides a container for transferring data and objects through the clipboard. The operating system clipboard can be accessed through the static generalClipboard property.

A Clipboard object can contain the same information in more than one format. By supplying information in multiple formats, you increase the chances that another application will be able to use that information. Add data to a Clipboard object with the setData() or setDataHandler() method.

ActionScript 3.0 Reference

Drag and Drop with ActionScript 3

Flex Builder 4 Flash Video Mouse Events



Drag and drop code

reset.addEventListener(MouseEvent.CLICK, comeHome);

function comeHome(e:Event) {
mc.x = 115;
mc.y = 70;
}

function initDragger(mc:MovieClip):void
{
mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownDragger);
mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpDragger);
}

function mouseDownDragger(e:MouseEvent):void
{
e.currentTarget.startDrag();
}
function mouseUpDragger(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}

// Set up drag
initDragger(mc);

startDrag()

Lets the user drag the specified sprite. The sprite remains draggable until explicitly stopped through a call to the Sprite.stopDrag() method, or until another sprite is made draggable. Only one sprite is draggable at a time. Three-dimensional display objects follow the pointer and Sprite.startDrag() moves the object within the three-dimensional plane defined by the display object. Or, if the display object is a two-dimensional object and the child of a three-dimensional object, the two-dimensional object moves within the three dimensional plane defined by the three-dimensional parent object.

Parameters

lockCenter:Boolean (default = false) — Specifies whether the draggable sprite is locked to the center of the pointer position (true), or locked to the point where the user first clicked the sprite (false).

bounds:Rectangle(default = null) — Value relative to the coordinates of the Sprite's parent that specify a constraint rectangle for the Sprite.

stopDrag()

Ends the startDrag() method. A sprite that was made draggable with the startDrag() method remains draggable until a stopDrag() method is added, or until another sprite becomes draggable. Only one sprite is draggable at a time.

Related Posts Plugin for WordPress, Blogger...