Hey Class! This blog will be a place to review course content, download copies of files completed in class and get information on assignments.
Class 1
This class we reviewed what we are going to learn over the next semester. On the Tuesday class we build a small PHP cat list/form and on the Thursday class we build a small baseball player list/form.
This class will be spent creating a JavaScript validated form. Our form will include these items and rules:
Name - 5 to 50 - a-z,-,spaces
Email Address - 6 to 50 - x@x.xx
Phone Number - 7 to 20 - #,-,(),.,spaces
Address - 5 to 50
City - 4 to 20 - a-z,-,#,’,spaces
Postal Code - 6 or 7 - x9×9x9x - with an optional space
Province - select
Comment - not blank
In one of my Flash courses taught at Humber College we often choose web sites to deconstruct. Today we have chosen red-issue.com.
First let’s figure out what we want to learn from this web site. We are going to create a grid of images where each image will occupy the full screen. This grid will be navigated by drawing arrows.
First lets import all the Flash and custom classes we will need:
The last import statement is a class that helps us recognize mouse gestures. The Mouse Gestures Class was written by Didier Brun.
Next we need a few variables to define the grid dimensions and to keep track of what row and column we are currently looking at.
var verticalCounter:Number = new Number();
var horizontalCounter:Number = new Number();
var gridWidth:Number=new Number(4);
var gridHeight:Number=new Number(3);
On the Enter Frame Event we will check the values in the horizontalCounter and verticalCounter variable and position the grid accordingly. That way when someone draws a down arrow we will simply add one to the verticalCounter variable.
stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(evt:Event):void {
var targetX:Number = new Number(stage.stageWidth * horizontalCounter);
grid_mc.x += (targetX - grid_mc.x) / 3;
var targetY:Number = new Number(stage.stageHeight * verticalCounter);
grid_mc.y += (targetY - grid_mc.y) / 3;
}
Wen using the Mouse Gestures Class we need to tell the Class what gestures to look for. These gestures are determined by a set of numbers. For example a up arrow starting from the left consists of a 7 motion and then a 1 motion. Here are the motions tracked and the numbers for each.
Here is the code to create a new Mouse Gesture and add the four different directions as well as the zoom in and zoom out gestures.
var mg:MouseGesture = new MouseGesture(stage);
mg.addGesture("UP","71");
mg.addGesture("UP","53");
mg.addGesture("DOWN","17");
mg.addGesture("DOWN","35");
mg.addGesture("LEFT","13");
mg.addGesture("LEFT","75");
mg.addGesture("RIGHT","31");
mg.addGesture("RIGHT","57");
mg.addGesture("OUT","0");
mg.addGesture("OUT","4");
mg.addGesture("IN","4321076542");
Now with this class we are going to listen for a few specific events.
A Mouse Gesture is defined by the user pressing their mouse down, moving the mouse and then releasing. When a Mouse Gesture matches one of the items defines in our list the matchHandler function is executed. This function makes the necessary changes to verticalCounter or horizontalCounter and then removes the drawing.
function matchHandler(evt:GestureEvent):void{
switch (evt.datas){
case "UP":
if(verticalCounter < 0){
verticalCounter++;
}
break;
case "DOWN":
if(verticalCounter > 1 - gridHeight){
verticalCounter--;
}
break;
case "LEFT":
if(horizontalCounter > 1 - gridWidth){
horizontalCounter--;
}
break;
case "RIGHT":
if(horizontalCounter < 0){
horizontalCounter++;
}
break;
case "IN":
// Zoom in code would go here
break;
case "OUT":
// Zoom out code would go here
break;
}
var tweenOut:Tween = new Tween(draw_mc,"alpha",Strong.easeOut,1,0,6);
}
This code was taken from the GesturesDemo.as file that comes with the Mouse Gestures Class. When the Start Capture Event is triggered this code draws whatever the user does.
function startHandler(e:GestureEvent):void{
draw_mc.graphics.clear();
draw_mc.alpha = 1;
draw_mc.graphics.lineStyle(4,0x444444);
draw_mc.graphics.moveTo(mouseX,mouseY);
addEventListener(Event.ENTER_FRAME,capturingHandler);
}
When the user lets go of their mouse the Stop Capture Event is triggered and executes the startHandler function. This function stops the drawing initialized by the startHandler function.
function stopHandler(e:GestureEvent):void{
removeEventListener(Event.ENTER_FRAME,capturingHandler);
}
This function actually performs the drawing. It is started by the startHandler function and is stopped by the stopHandler function.
function capturingHandler(e:Event):void{
draw_mc.graphics.lineTo(mouseX,mouseY);
}
When the user releases the mouse button if there is no match the noMatchHandler function removes the drawing.
function noMatchHandler(e:GestureEvent):void{
var tweenOut:Tween = new Tween(draw_mc,"alpha",Strong.easeOut,1,0,6);
}
The next set of code creates the photo grid and makes any necessary changes when the Stage is re-sized. You can get a much more in depth description of this code from the Photo Grid post.
They take a little getting used to but once your comfortable with the sites and the whole Object Orientated Programming concept these sites become invaluable.
This class will be spent reviewing some Flash font practices, the basics of Object Orientated programming and learning how movie clip levels work.
Class 2
For your first assignment you with either build a Flash Weather RSS Feed or a Flash Contact form. You only have to complete one or other, not both. This assignment is Due April 13, 2009!
In this class we will build some basic Flash web site file structures and focus on loading exterrnal SWFs. We will also start deconstructing Mono Face.
Flash Web Site Assignment
The Flash web site assignment is due on the last day of class on April 27th, 2009!
This post will walk through the construction of a simple XML and Flash gallery. If you want to use the images and XML file we are referring to in this example you can download our XML document here and download our images here. Our gallery will simply load the first image and then with each click it’ll load the next image form our XML document.
The Flash plugin is required to view this object.
First of all here is the XML we will be working with:
Next we need a Movie Clip to load images into and apply Tweens to:
var containerMovie:MovieClip = new MovieClip();
addChild(containerMovie);
Then we define a URL Loarder and an XML Object:
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
We are going to need two variables: one to keep track of what image we are currently viewing and another to keep track of whether or not we are in the progress of loading the next image.
var galleryCounter:Number=new Number(0);
var galleryStatus:Boolean=new Boolean(false);
Then we are gong to add an Event LIstener to the main Movie Clip to listen for Click Events.
When some one clicks the main Movie Clip we check to make sure another image isn’t half way through loading; and if it isn’t we add one to the counter Variable, check to make sure it hasn’t gone too high and then load that photo.
function clickHandler(evt:MouseEvent):void {
if (galleryStatus==false) {
galleryCounter++;
if (galleryCounter>=xmlData.photo.length()) {
galleryCounter=0;
}
loadPhotoStep1(galleryCounter);
}
}
Now that our containers and butons are all set up lets load our XML.
xmlLoader.load(new URLRequest("gallery.xml"));
Once that loading is complete we put the loaded information into our XML Object and start eh first step of loading our image.
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
function LoadXML(e:Event):void {
xmlData=new XML(e.target.data);
loadPhotoStep1(galleryCounter);
}
Setp 1 - we save the loaded image number into our galleryCounter variable and set galleryStatus to true. This prevents our clickHandler function above from loading another image before this one has finished loading. And we Tween the Alpha out of the current image.
function loadPhotoStep1(photoNumber:Number):void {
galleryCounter=photoNumber;
galleryStatus=true;
var tweenOut:Tween=new Tween(containerMovie,"alpha",Strong.easeOut,1,0,15);
tweenOut.addEventListener(TweenEvent.MOTION_FINISH,loadPhotoStep2);
}
Step 2 - once the first Tween has completed we load empty the container Movie Clip and load in the new image.
function loadPhotoStep2(evt:TweenEvent):void {
var imageLoader:Loader = new Loader();
var imageRequest:URLRequest=new URLRequest(xmlData.photo[galleryCounter].filename);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadPhotoStep3);
imageLoader.load(imageRequest);
emptyContainer();
containerMovie.addChild(imageLoader);
}
One the image has loaded in we Tween the Alpha back in.
function loadPhotoStep3(evt:Event):void {
var tweenIn:Tween=new Tween(containerMovie,"alpha",Strong.easeOut,0,1,15);
tweenIn.addEventListener(TweenEvent.MOTION_FINISH,loadPhotoStep4);
}
Once that Tween is complete we set galleryStatus back to false so the next time the user clicks the Movie Clip i’s good to go!
function loadPhotoStep4(evt:Event):void {
galleryStatus=false;
}
This last function removes all Children from the containerMovie Movie Clip. It is called by the step 2 function.
function emptyContainer() :void {
for (var i:int; i < containerMovie.numChildren; i++){
containerMovie.removeChildAt(i);
}
}
When teaching Flash one of the questions that often comes up is how to generate a menu based on XML. By the time the question gets to me usually the FLA is to the point where he XML is loaded, there is an imported Object from the library for each item in the XML and those Objects are clickable. Where it gets difficult is having each of those items remember what XML item they belong to.
In Actionscript 2.0 this was easy. We could assign whatever properties/variables/functions to whatever and everything would work out fine. Using Actionscript 3.0 we now hove to do this properly….well kind of.
First we need some XML to import. Lets use this very generic XML file.
<?xml version="1.0" encoding="utf-8"?>
<root_element>
<item>
<number>1</number>
<title>Title Number 1</title>
<image>image1.jpg</image>
</item>
<item>
<number>2</number>
<title>Title Number 2</title>
<image>image2.jpg</image>
</item>
<item>
<number>3</number>
<title>Title Number 3</title>
<image>image3.jpg</image>
</item>
<item>
<number>4</number>
<title>Title Number 4</title>
<image>image4.jpg</image>
</item>
</root_element>
Now lets go through two different methods of achieving this.
Method One: Avoiding External AS Files
The proper way to do this would be to create a Custom Class using an external Actionscript file and maybe partner this with a Library Object with the same name. However, this can be achieved without an External Actionscript file…it’s a little dirty but it’ll work.
Open up a new Actionscript 3.0 file and create a new Symbol with the following properties:
In this new Object give it the following timeline:
On the number text box put a Dynamic Text Field with the Instance name number_tb. On the Title layer put a Dynamic Text Field with the instance name title_tb. And on the background layer put some sort of background. IT should look something like this:
Now go back to the Stage and select Frame 1. The first thing we need to do is create a URL Request, a URL Loader and an XML Object:
var exampleXML:XML = new XML();
var exampleRequest:URLRequest=new URLRequest("flash_number_example.xml");
var exampleLoader:URLLoader = new URLLoader();
Next we need to load the URL Request using the URL Loader.
exampleLoader.load(exampleRequest);
When the loading is complete we will trigger the completeHandler Function.
And lastly we need to define that function.The first line takes the loaded data from our URL Loader and puts it into an XML object. Before we moved it into an XML Object the data would have been treated as text. This allows us to do all sorts of XML type things with it.
Next we loop through each item in the XML Object and create a copy of an ItemObject. For each ItemObject we set the test in the title_tb Text Field to the title from the XML and the number_tb Text Field to the item number. Next we position the Item Object, give it it’s Button actions and lastly add it to the Stage.
Special thanks to Mazoonist for the mouseChildren line of code. This solution was found in an actionscript.og forum post. This line makes sure the whole button is clickable. If we did not have this line the button would be clickable but you would not get the point mouse whenever your mouse is over a text box.
function completeHandler(evt:Event):void {
exampleXML=XML(evt.target.data);
for (var i:int=0; i<exampleXM.item.length(); i++ )
var nextItem:ItemObject = new ItemObject();
nextItem.title_tb.text = String(exampleXML.item[i].title);
nextItem.number_tb.text = String(i);
nextItem.x = 141 * i;
nextItem.addEventListener(MouseEvent.CLICK,clickHandler);
nextItem.mouseChildren = false;
nextItem.buttonMode = true;
addChild(nextItem);
}
}
And of course we need a function to execute on he click of our Buttons. At this point this code just traces it’s own number. Now that each item know it’s number it can go back to the XML object and retrieve anything else it may need.
function clickHandler(evt:Event):void {
trace(evt.target.number_tb.text);
}
In this method we will use a Library Object in Partnership with an external AS file. Start by opening a new Actionscript 3.0 document. Just like the above example insert a new symbol with the following properties:
In that new symbol give it the following timeline:
On the title layer put a Text Field with the Instance name title_tb and on the background layer put some sort of background.
Next we need an external AS file to help define our ItemObject Object. Creating a Custom Class Actionscript file is exactly like creating an object in real life. We give the Object a name and we define what properties the Object has. In our case we are creating an ItemObject. From looking at our last method we know that an ItemObject is a type of MovieClip; which is actually a little over kill. We can go one step back. So our ItemObject is a type of Sprite.
The different between a Sprite and a MovieClip is that a MovieClip is a Sprite with a timeline. So MovieClip have properties like currentframe and all sorts of timeline related stuff.
When creating a Custom Class we use the package syntax and first list all pre-existing Classes the this Custom Class requires. So in our example we need the Sprite and MouseEvent Classes.
Next we start our Class and provide the base Class; in this case a Sprite:
public class ItemObject extends Sprite {
We now need ot list what properties this new Custom Class has. In our example above the ItemObject has two properties that a Spite doesn’t have. We need a place to store the text and a pace to store the number.
var itemText:String;
var itemNumber:Number;
Next we need what is called a Constructor. A Constructor is the code that is executed when the Class is created. In the constructor definition we also specify what values are required to initially create an instance of our Custom Class.
In our example we need the text and the number to create an instance of an ItemObject. This function then saves the two provided values in the two properties defined above, adds a Rollover action and sets a few default properties.
public function ItemObject(itemText:String,itemNumber:Number){
this.itemText = itemText;
this.itemNumber = itemNumber;
this.title_tb.text = this.itemText;
this.addEventListener(MouseEvent.CLICK,clickHandler);
this.mouseChildren = false;
this.buttonMode = true;
}
Finally we need the clickHandler function called above. In this case we just trace the ItemObject’s number. And close the parenthesis from the open Class and Package lines above.
public function clickHandler(evt:MouseEvent):void {
trace(this.itemNumber);
}