<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>adam.benjamin.thomas</title>
	<atom:link href="http://www.adambenjamin.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.adambenjamin.com/blog</link>
	<description>flash, dreamweaver, php, javascript and more</description>
	<pubDate>Thu, 17 Dec 2009 22:41:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>AVIS 216 - Fall 2009</title>
		<link>http://www.adambenjamin.com/blog/?p=738</link>
		<comments>http://www.adambenjamin.com/blog/?p=738#comments</comments>
		<pubDate>Tue, 15 Sep 2009 11:33:32 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Humber]]></category>

		<guid isPermaLink="false">http://www.adambenjamin.com/blog/?p=738</guid>
		<description><![CDATA[Hey Class,
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 [...]]]></description>
			<content:encoded><![CDATA[<p>Hey Class,</p>
<p>Hey Class! This blog will be a place to review course content, download copies of files completed in class and get information on  assignments.</p>
<p><strong>Class 1</strong></p>
<p>This class we reviewed what we are going to learn over the next semester. On the Tuesday class we build a small <a href="http://www.adambenjamin.com/blog/wp-content/class-1-tuesday.zip">PHP cat list/form</a> and on the Thursday class we build a small <a href="http://www.adambenjamin.com/blog/wp-content/class-1-thursday.zip">baseball player list/form</a>.</p>
<p>You were also assigned the <a href="http://www.adambenjamin.com/blog/wp-content/assignment-1-learningjournal.doc">Learning Journal assignment</a>.</p>
<p><strong>Class 2</strong></p>
<p>This class will eb spent coveirng the DOM, variables and conrol structures:</p>
<p>DOM - Document Object Model<br />
<a href="http://www.w3schools.com/js/js_obj_htmldom.asp" target="_blank">http://www.w3schools.com/js/js_obj_htmldom.asp</a></p>
<p>Where<br />
<a href="http://www.w3schools.com/js/js_variables.asp">http://www.w3schools.com/js/js_variables.asp<br />
</a><a href="http://www.w3schools.com/js/js_whereto.asp">http://www.w3schools.com/js/js_whereto.asp</a></p>
<p>Variables<br />
<a href="http://www.w3schools.com/js/js_variables.asp">http://www.w3schools.com/js/js_variables.asp</a><br />
<a href="http://www.w3schools.com/js/js_operators.asp">http://www.w3schools.com/js/js_operators.asp</a></p>
<p>Control Structures<br />
<a href="http://www.w3schools.com/js/js_if_else.asp">http://www.w3schools.com/js/js_if_else.asp</a><a href="http://www.w3schools.com/js/js_operators.asp"><br />
</a><a href="http://www.w3schools.com/js/js_comparisons.asp">http://www.w3schools.com/js/js_comparisons.asp</a></p>
<p>Download class files form <a href="http://www.adambenjamin.com/blog/wp-content/class-2-tuesdays.zip">Tuesday</a> or <a href="http://www.adambenjamin.com/blog/wp-content/class-2-thursdays.zip">Thursday</a>.</p>
<p><strong>Class 3</strong></p>
<p>Today we will be covering the switch, for loop, while loop, arrays and functions:</p>
<p>Switch<br />
<a href="http://www.w3schools.com/jS/js_switch.asp" target="_blank">http://www.w3schools.com/jS/js_switch.asp</a></p>
<p>For Loop<br />
<a href="http://www.w3schools.com/JS/js_loop_for.asp" target="_blank">http://www.w3schools.com/JS/js_loop_for.asp</a></p>
<p>While Loop<br />
<a href="http://www.w3schools.com/JS/js_loop_while.asp" target="_blank">http://www.w3schools.com/JS/js_loop_while.asp</a></p>
<p>Functions<br />
<a href="http://www.w3schools.com/JS/js_functions.asp" target="_blank">http://www.w3schools.com/JS/js_functions.asp</a></p>
<p>Arrays<br />
<a href="http://www.w3schools.com/JS/js_obj_array.asp" target="_blank">http://www.w3schools.com/JS/js_obj_array.asp</a></p>
<p>You have also been assigned the <a href="http://www.adambenjamin.com/blog/wp-content/javascript-assignment1.doc">JavaScript assignment</a>.</p>
<p>Download class files form <a href="http://www.adambenjamin.com/blog/wp-content/class-3-tuesdays.zip">Tuesday</a> or <a href="http://www.adambenjamin.com/blog/wp-content/class-3-thursdays.zip">Thursday</a>.</p>
<h2><span style="color: #ff0000;">Assignment 2 is Due October 22, 2009</span></h2>
<p><strong>Class 4</strong></p>
<p>This class will be spent creating a JavaScript validated form. Our form will include these items and rules:</p>
<p>Name - 5 to 50 - a-z,-,spaces<br />
Email Address - 6 to 50 - x@x.xx<br />
Phone Number - 7 to 20 - #,-,(),.,spaces<br />
Address - 5 to 50<br />
City - 4 to 20 - a-z,-,#,&#8217;,spaces<br />
Postal Code - 6 or 7 - x9&#215;9x9x - with an optional space<br />
Province - select<br />
Comment - not blank</p>
<p><strong>Class 5</strong></p>
<p>This class was spent setting up PHPMyAdmin for use in future classes: <a href="http://www.phpmyadmin.net/" target="_blank">http://www.phpmyadmin.net/</a>.</p>
<p><strong>Class 6</strong></p>
<p>This class was spent reviewing some of teh PHP basics:</p>
<p>Echo<br />
<a href="http://www.w3schools.com/php/func_string_echo.asp">http://www.w3schools.com/php/func_string_echo.asp</a></p>
<p><a href="http://www.w3schools.com/php/func_string_echo.asp"></a>Variables<br />
<a href="http://www.w3schools.com/php/php_variables.asp">http://www.w3schools.com/php/php_variables.asp</a></p>
<p><strong>Class 7</strong></p>
<p>This class was spent reviewing PHP control structures and loops:</p>
<p>The If Statement<br />
<a href="http://www.w3schools.com/php/php_if_else.asp">http://www.w3schools.com/php/php_if_else.asp</a></p>
<p><a href="http://www.w3schools.com/php/php_if_else.asp"></a>The Switch<br />
<a href="http://www.w3schools.com/php/php_switch.asp">http://www.w3schools.com/php/php_switch.asp</a></p>
<p>The For Loop<br />
<a href="http://www.w3schools.com/php/php_looping_for.asp">http://www.w3schools.com/php/php_looping_for.asp</a></p>
<p>Class 8</p>
<p>Today we build a basic PHP form. Your first PHP assignment will be to finish this form.</p>
<p><a href="http://www.adambenjamin.com/blog/wp-content/php-assignment-11.doc">Download the PHP Form Assignment</a></p>
<h2><span style="color: #ff0000;"><span style="color: #ff0000;">Assignment 3 is Due November 26, 2009</span></span></h2>
<p><strong>Final Assignment</strong></p>
<p><a href="http://www.adambenjamin.com/blog/wp-content/php-assignment-2.doc">Download the PHP Module Assignment</a></p>
<h2><span style="color: #ff0000;">Final Assignment is Due on the last Thursday  of classes</span></h2>
<p><a href="http://www.w3schools.com/js/js_if_else.asp"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=738</wfw:commentRss>
		</item>
		<item>
		<title>Deconstruct red-issue.com</title>
		<link>http://www.adambenjamin.com/blog/?p=610</link>
		<comments>http://www.adambenjamin.com/blog/?p=610#comments</comments>
		<pubDate>Sun, 22 Mar 2009 17:50:46 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[deconstruct]]></category>

		<category><![CDATA[deconstruction]]></category>

		<category><![CDATA[gallery]]></category>

		<category><![CDATA[mouse gesture]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=610</guid>
		<description><![CDATA[
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&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://red-issue.com" target="_blank"><img class="alignnone size-full wp-image-698" title="flash_red_screenshot" src="http://adambenjamin.com/blog/wp-content/flash_red_screenshot.jpg" alt="" width="589" height="402" /></a></p>
<p>In one of my Flash courses taught at Humber College we often choose web sites to deconstruct. Today we have chosen <a href="http://red-issue.com/" target="_self">red-issue.com</a>.</p>
<p>First let&#8217;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.</p>
<p>First lets import all the Flash and custom classes we will need:</p>
<pre lang="actionscript3">import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import com.foxaweb.ui.gesture.*;</pre>
<p>The last import statement is a class that helps us recognize mouse gestures. The <a href="http://www.bytearray.org/?p=91" target="_blank">Mouse Gestures Class</a> was written by <a href="http://www.foxaweb.com" target="_blank">Didier Brun</a>.</p>
<p>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.</p>
<pre lang="actionscript3">var verticalCounter:Number = new Number();
var horizontalCounter:Number = new Number();
var gridWidth:Number=new Number(4);
var gridHeight:Number=new Number(3);</pre>
<p>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.</p>
<pre lang="actionscript3">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;
}</pre>
<p>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.</p>
<p><img class="alignnone size-full wp-image-701" title="flash_red_numbers" src="http://adambenjamin.com/blog/wp-content/flash_red_numbers.jpg" alt="" width="130" height="126" /></p>
<p>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.</p>
<pre lang="actionscript3">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");</pre>
<p>Now with this class we are going to listen for a few specific events.</p>
<pre lang="actionscript3">mg.addEventListener(GestureEvent.GESTURE_MATCH,matchHandler);
mg.addEventListener(GestureEvent.NO_MATCH,noMatchHandler);
mg.addEventListener(GestureEvent.START_CAPTURE,startHandler);
mg.addEventListener(GestureEvent.STOP_CAPTURE,stopHandler);</pre>
<p>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.</p>
<pre lang="actionscript3" escaped="true">function matchHandler(evt:GestureEvent):void{
     switch (evt.datas){
          case "UP":
               if(verticalCounter &lt; 0){
                    verticalCounter++;
               }
          break;
          case "DOWN":
               if(verticalCounter &gt; 1 - gridHeight){
                    verticalCounter--;
               }
          break;
          case "LEFT":
               if(horizontalCounter &gt; 1 - gridWidth){
                    horizontalCounter--;
          }
          break;
          case "RIGHT":
               if(horizontalCounter &lt; 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);
}</pre>
<p>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.</p>
<pre lang="actionscript3">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);
}</pre>
<p>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.</p>
<pre lang="actionscript3">function stopHandler(e:GestureEvent):void{
     removeEventListener(Event.ENTER_FRAME,capturingHandler);
}</pre>
<p>This function actually performs the drawing. It is started by the startHandler function and is stopped by the stopHandler function.</p>
<pre lang="actionscript3">function capturingHandler(e:Event):void{
     draw_mc.graphics.lineTo(mouseX,mouseY);
}</pre>
<p>When the user releases the mouse button if there is no match the noMatchHandler function removes the drawing.</p>
<pre lang="actionscript3">function noMatchHandler(e:GestureEvent):void{
     var tweenOut:Tween = new Tween(draw_mc,"alpha",Strong.easeOut,1,0,6);
}</pre>
<p>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 <a href="http://adambenjamin.com/blog/?p=606">Photo Grid post</a>.</p>
<pre lang="actionscript3">stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;</pre>
<p>This list of images could eventually come from an XML document but for now we will just use an array.</p>
<pre lang="actionscript3" escaped="true">var imageDir:String = new String( "http://adambenjamin.com/blog/wp-content/" );
var gridArray:Array=new Array(
     ({image:"img_0001.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0002.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0003.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0004.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0005.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0006.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0007.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0008.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0009.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0010.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0011.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0012.jpg",imageWidth:800,imageHeight:532}));

var movieArray:Array = new Array();
var maskArray:Array = new Array();
var loaderArray:Array = new Array();

stage.addEventListener(Event.RESIZE, resizeHandler);
function resizeHandler(evt:Event):void {
     for (var i:int = 0; i &lt; gridArray.length; i ++) {
          resizeMask(i);
          resizeImage(i);
          reposition(i);
     }
}

for (var i:int = 0; i &lt; gridArray.length; i ++) {
     maskArray[i] = new MovieClip();
     maskArray[i].graphics.lineStyle(0,0x000088);
     maskArray[i].graphics.beginFill(0x000088);
     maskArray[i].graphics.drawRect(0,0,1,1);
     maskArray[i].graphics.endFill();
     resizeMask(i);
     grid_mc.addChild(maskArray[i]);
     movieArray[i] = new MovieClip();
     var request:URLRequest=new URLRequest(imageDir + gridArray[i]["image"]);
     loaderArray[i] = new Loader();

     loaderArray[i].load(request);
     resizeImage(i);
     movieArray[i].addChild(loaderArray[i]);
     grid_mc.addChild(movieArray[i]);
     movieArray[i].mask=maskArray[i];
     reposition(i);
}

function reposition(i:int):void{
     maskArray[i].x = i % gridWidth * Number(stage.stageWidth);
     maskArray[i].y = Math.floor( i / gridWidth ) * Number(stage.stageHeight);
     movieArray[i].x = i % gridWidth * Number(stage.stageWidth);
     movieArray[i].y = Math.floor( i / gridWidth ) * Number(stage.stageHeight);
}

function resizeMask(i:int):void{
     maskArray[i].width = Number(stage.stageWidth);
     maskArray[i].height = Number(stage.stageHeight);
}

function resizeImage(i:int):void {
     var widthRatio:Number = Number(stage.stageWidth)/gridArray[i]["imageWidth"];
     var heightRatio:Number = Number(stage.stageHeight)/gridArray[i]["imageHeight"];
     if( widthRatio &gt; heightRatio){
          movieArray[i].scaleX = widthRatio;
          movieArray[i].scaleY = widthRatio;
     }else{
          movieArray[i].scaleX = heightRatio;
          movieArray[i].scaleY = heightRatio;
     }
}</pre>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="700" height="350">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_red_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_red_file.swf" width="700" height="350">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_red_file.fla">Download Red Issue FLA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=610</wfw:commentRss>
		</item>
		<item>
		<title>AVIS 340 - Winter 2009 - Session 2</title>
		<link>http://www.adambenjamin.com/blog/?p=690</link>
		<comments>http://www.adambenjamin.com/blog/?p=690#comments</comments>
		<pubDate>Mon, 09 Mar 2009 16:02:00 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Humber]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=690</guid>
		<description><![CDATA[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
To start here are two of the most important links when programming Actionscript 3.0:
Adobe Livedocs Flash CS4 Documentation
Adobe LIvedocs AS3 Language Reference
They take a little getting used to but once your [...]]]></description>
			<content:encoded><![CDATA[<p>Hey Class! This blog will be a place to review course content, download copies of files completed in class and get information on  assignments.</p>
<p><strong>Class 1</strong></p>
<p>To start here are two of the most important links when programming Actionscript 3.0:</p>
<p><a href="http://livedocs.adobe.com/flash/9.0/main/" target="_blank">Adobe Livedocs Flash CS4 Documentation</a></p>
<p><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/" target="_blank">Adobe LIvedocs AS3 Language Reference</a></p>
<p>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.</p>
<p>This class will be spent reviewing some Flash font practices, the basics of Object Orientated programming and learning how movie clip levels work.</p>
<p><strong>Class 2</strong></p>
<p>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!</p>
<p><strong>Flash Weather RSS Feed Assignment</strong></p>
<p><a href="http://adambenjamin.com/blog/wp-content/assignment3-rssweather.doc">Download the Flash Weather RSS Feed Assignment Word Document</a></p>
<p><strong>Flash Contact Form Assignment</strong></p>
<p><a href="http://adambenjamin.com/blog/wp-content/assignment2-flashcontactform.doc">Download the Flash Contact Form Assignment Word Document</a></p>
<p>The example of the contact form build in class can be viewed on the <a href="http://adambenjamin.com/blog/?p=437">Flash Contact Form</a> post.</p>
<p>This assignment is due on Monday Due April 13!!</p>
<p><strong>Class 3</strong></p>
<p>This class will be spent focusing on <a href="http://adambenjamin.com/blog/?p=366">building preloaders</a>. We will also look at <a href="http://adambenjamin.com/blog/?p=392">deconstructing the Pepsi preloader</a>.</p>
<p><strong>Class 4</strong></p>
<p>In this class we will build some basic Flash web site file structures and focus on loading exterrnal SWFs. We will also start <a href="http://adambenjamin.com/blog/?p=721">deconstructing Mono Face</a>.</p>
<p><strong>Flash Web Site Assignment</strong></p>
<p>The Flash web site assignment is due on the last day of class on April 27th, 2009!</p>
<p><a href="http://adambenjamin.com/blog/wp-content/assignment4-flashwebsite.doc">Download the Web Site Assignment Word Document</a></p>
<p><strong>Class 5</strong></p>
<p>Today we will review how to make a <a href="http://adambenjamin.com/blog/?p=634">basic XML gallery</a> and continue with <a href="http://adambenjamin.com/blog/?p=721">deconstructing Mono Face</a>.</p>
<p><img src="file:///C:/DOCUME~1/thomasa/LOCALS~1/Temp/moz-screenshot.jpg" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=690</wfw:commentRss>
		</item>
		<item>
		<title>Flash XML Gallery</title>
		<link>http://www.adambenjamin.com/blog/?p=634</link>
		<comments>http://www.adambenjamin.com/blog/?p=634#comments</comments>
		<pubDate>Sat, 28 Feb 2009 00:35:01 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[XML]]></category>

		<category><![CDATA[gallery]]></category>

		<category><![CDATA[tween]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=634</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://adambenjamin.com/blog/wp-content/gallery.xml" target="_blank">download our XML document here</a> and <a href="http://adambenjamin.com/blog/wp-content/flash_grid_images.zip">download our images here</a>. Our gallery will simply load the first image and then with each click it&#8217;ll load the next image form our XML document.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="800" height="530">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_gallery_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_gallery_file.swf" width="800" height="530">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>First of all here is the XML we will be working with:</p>
<pre lang="xml" escaped="true">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;gallery&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0001.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Field at Night&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0002.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Van on the Road&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0003.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Van on the Road Close Up&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0004.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Mountains&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0005.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Edge of a Cliff&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0006.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Another Road&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0007.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Tight Path&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0008.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Yellow Pickup&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0009.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Power Lines&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0010.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Pig&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0011.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Drying Coffee Beans&lt;/title&gt;
     &lt;/photo&gt;
     &lt;photo&gt;
          &lt;filename&gt;http://adambenjamin.com/blog/wp-content/img_0012.jpg&lt;/filename&gt;
          &lt;width&gt;800&lt;/width&gt;
          &lt;height&gt;531&lt;/height&gt;
          &lt;title&gt;Homes&lt;/title&gt;
     &lt;/photo&gt;
&lt;/gallery&gt;</pre>
<p>In this example the only piece of information we will need is the filename. In future posts we may use the image titles and dimensions.</p>
<p>First we need to import a few Classes:</p>
<pre lang="actionscript3">import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;</pre>
<p>Next we need a Movie Clip to load images into and apply Tweens to:</p>
<pre lang="actionscript3">var containerMovie:MovieClip = new MovieClip();
addChild(containerMovie);</pre>
<p>Then we define a URL Loarder and an XML Object:</p>
<pre lang="actionscript3">var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();</pre>
<p>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.</p>
<pre lang="actionscript3">var galleryCounter:Number=new Number(0);
var galleryStatus:Boolean=new Boolean(false);</pre>
<p>Then we are gong to add an Event LIstener to the main Movie Clip to listen for Click Events.</p>
<pre lang="actionscript3">containerMovie.buttonMode = true;
containerMovie.addEventListener(MouseEvent.CLICK,clickHandler);</pre>
<p>When some one clicks the main Movie Clip we check to make sure another image isn&#8217;t half way through loading; and if it isn&#8217;t we add one to the counter Variable, check to make sure it hasn&#8217;t gone too high and then load that photo.</p>
<pre lang="actionscript3">function clickHandler(evt:MouseEvent):void {
     if (galleryStatus==false) {
          galleryCounter++;
          if (galleryCounter&gt;=xmlData.photo.length()) {
               galleryCounter=0;
          }
          loadPhotoStep1(galleryCounter);
     }
}</pre>
<p>Now that our containers and butons are all set up lets load our XML.</p>
<pre lang="actionscript3">xmlLoader.load(new URLRequest("gallery.xml"));</pre>
<p>Once that loading is complete we put the loaded information into our XML Object and start eh first step of loading our image.</p>
<pre lang="actionscript3">xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
function LoadXML(e:Event):void {
     xmlData=new XML(e.target.data);
     loadPhotoStep1(galleryCounter);
}</pre>
<p>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.</p>
<pre lang="actionscript3">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);
}</pre>
<p>Step 2 - once the first Tween has completed we load empty the container Movie Clip and load in the new image.</p>
<pre lang="actionscript3">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);
}</pre>
<p>One the image has loaded in we Tween the Alpha back in.</p>
<pre lang="actionscript3">function loadPhotoStep3(evt:Event):void {
     var tweenIn:Tween=new Tween(containerMovie,"alpha",Strong.easeOut,0,1,15);
     tweenIn.addEventListener(TweenEvent.MOTION_FINISH,loadPhotoStep4);
}</pre>
<p>Once that Tween is complete we set galleryStatus back to false so the next time the user clicks the Movie Clip i&#8217;s good to go!</p>
<pre lang="actionscript3">function loadPhotoStep4(evt:Event):void {
     galleryStatus=false;
}</pre>
<p>This last function removes all Children from the containerMovie Movie Clip. It is called by the step 2 function.</p>
<pre lang="actionscript3" escaped="true">function emptyContainer() :void {
     for (var i:int; i &lt; containerMovie.numChildren; i++){
          containerMovie.removeChildAt(i);
     }
}</pre>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_gallery_file.fla">Download the Gallery Flash File</a></p>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_gallery_new.zip">Download a Version of this Gallery with Thumbnails</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=634</wfw:commentRss>
		</item>
		<item>
		<title>My Own Number</title>
		<link>http://www.adambenjamin.com/blog/?p=650</link>
		<comments>http://www.adambenjamin.com/blog/?p=650#comments</comments>
		<pubDate>Fri, 27 Feb 2009 23:20:57 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[XML]]></category>

		<category><![CDATA[menu]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=650</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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&#8230;.well kind of.</p>
<p>First we need some XML to import. Lets use this very generic XML file.</p>
<pre lang="xml" escaped="true">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;root_element&gt;
     &lt;item&gt;
          &lt;number&gt;1&lt;/number&gt;
          &lt;title&gt;Title Number 1&lt;/title&gt;
          &lt;image&gt;image1.jpg&lt;/image&gt;
     &lt;/item&gt;
     &lt;item&gt;
          &lt;number&gt;2&lt;/number&gt;
          &lt;title&gt;Title Number 2&lt;/title&gt;
          &lt;image&gt;image2.jpg&lt;/image&gt;
     &lt;/item&gt;
     &lt;item&gt;
          &lt;number&gt;3&lt;/number&gt;
          &lt;title&gt;Title Number 3&lt;/title&gt;
          &lt;image&gt;image3.jpg&lt;/image&gt;
     &lt;/item&gt;
     &lt;item&gt;
          &lt;number&gt;4&lt;/number&gt;
          &lt;title&gt;Title Number 4&lt;/title&gt;
          &lt;image&gt;image4.jpg&lt;/image&gt;
     &lt;/item&gt;
&lt;/root_element&gt;</pre>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_number_example.xml" target="_blank">Download the Example XML Document</a></p>
<p>Now lets go through two different methods of achieving this.</p>
<p><strong>Method One: Avoiding External AS Files</strong></p>
<p>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&#8230;it&#8217;s a little dirty but it&#8217;ll work.</p>
<p>Open up a new Actionscript 3.0 file and create a new Symbol with the following properties:</p>
<p><img class="alignnone size-full wp-image-652" title="flash_number_object" src="http://adambenjamin.com/blog/wp-content/flash_number_object.jpg" alt="" width="466" height="592" /></p>
<p>In this new Object give it the following timeline:</p>
<p><img class="alignnone size-full wp-image-655" title="flash_number_timeline_hack" src="http://adambenjamin.com/blog/wp-content/flash_number_timeline_hack.jpg" alt="" width="532" height="151" /></p>
<p>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:</p>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_timeline_stage_hack.jpg"><img class="alignnone size-full wp-image-658" title="flash_timeline_stage_hack" src="http://adambenjamin.com/blog/wp-content/flash_timeline_stage_hack.jpg" alt="" width="147" height="71" /></a></p>
<p>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:</p>
<pre lang="actionscript3" escaped="true">var exampleXML:XML = new XML();
var exampleRequest:URLRequest=new URLRequest(&amp;quot;flash_number_example.xml&amp;quot;);
var exampleLoader:URLLoader = new URLLoader();</pre>
<p>Next we need to load the URL Request using the URL Loader.</p>
<pre lang="actionscript3" escaped="true">exampleLoader.load(exampleRequest);</pre>
<p>When the loading is complete we will trigger the completeHandler Function.</p>
<pre lang="actionscript3" escaped="true">exampleLoader.addEventListener(Event.COMPLETE,completeHandler);</pre>
<p>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.</p>
<p>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&#8217;s Button actions and lastly add it to the Stage.</p>
<p>Special thanks to Mazoonist for the mouseChildren line of code. This solution was found in an <a href="http://www.actionscript.org/forums/archive/index.php3/t-141513.html" target="_blank">actionscript.og forum post</a>. 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.</p>
<pre lang="actionscript3" escaped="true">function completeHandler(evt:Event):void {
     exampleXML=XML(evt.target.data);
     for (var i:int=0; i&lt;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);
     }
}</pre>
<p>And of course we need a function to execute on he click of our Buttons. At this point this code just traces it&#8217;s own number. Now that each item know it&#8217;s number it can go back to the XML object and retrieve anything else it may need.</p>
<pre lang="actionscript3">function clickHandler(evt:Event):void {
     trace(evt.target.number_tb.text);
}</pre>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_number_hack.fla">Download the Hack Version FLA</a></p>
<p><strong>Method Two: External AS Files</strong></p>
<p>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:</p>
<p><img class="alignnone size-full wp-image-652" title="flash_number_object" src="http://adambenjamin.com/blog/wp-content/flash_number_object.jpg" alt="" width="466" height="592" /></p>
<p>In that new symbol give it the following timeline:</p>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_number_timeline_proper.jpg"><img class="alignnone size-full wp-image-656" title="flash_number_timeline_proper" src="http://adambenjamin.com/blog/wp-content/flash_number_timeline_proper.jpg" alt="" width="532" height="151" /></a></p>
<p>On the title layer put a Text Field with the Instance name title_tb and on the background layer put some sort of background.</p>
<p><img class="alignnone size-full wp-image-659" title="flash_timeline_stage_proper" src="http://adambenjamin.com/blog/wp-content/flash_timeline_stage_proper.jpg" alt="" width="147" height="39" /></p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<pre lang="actionscript3">package {
     mport flash.display.Sprite;
     import flash.events.MouseEvent;</pre>
<p>Next we start our Class and provide the base Class; in this case a Sprite:</p>
<pre lang="actionscript3">     public class ItemObject extends Sprite {</pre>
<p>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&#8217;t have. We need a place to store the text and a pace to store the number.</p>
<pre lang="actionscript3">          var itemText:String;
          var itemNumber:Number;</pre>
<p>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.</p>
<p>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.</p>
<pre lang="actionscript3">          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;
          }</pre>
<p>Finally we need the clickHandler function called above. In this case we just trace the ItemObject&#8217;s number. And close the parenthesis from the open Class and Package lines above.</p>
<pre lang="actionscript3">          public function clickHandler(evt:MouseEvent):void {
               trace(this.itemNumber);
          }</pre>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_number_proper.fla">Download the Proper Version FLA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=650</wfw:commentRss>
		</item>
		<item>
		<title>Flash Photo Grid</title>
		<link>http://www.adambenjamin.com/blog/?p=606</link>
		<comments>http://www.adambenjamin.com/blog/?p=606#comments</comments>
		<pubDate>Mon, 23 Feb 2009 03:42:28 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[grid]]></category>

		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=606</guid>
		<description><![CDATA[In this post we are going to create a grid of images that is resizable using two text boxes. This task came from the Deconstruct red-issue.com post.
We are going to build a Flash file that will take the list of images and put the in a grid using the gridWidth and gridHeight as well as [...]]]></description>
			<content:encoded><![CDATA[<p>In this post we are going to create a grid of images that is resizable using two text boxes. This task came from the <a href="http://adambenjamin.com/blog/?p=610">Deconstruct red-issue.com</a> post.</p>
<p>We are going to build a Flash file that will take the list of images and put the in a grid using the gridWidth and gridHeight as well as the text from the width_tb and height_tb text Fields.</p>
<p>To make reusing this file easier we are first going to define a variable to determine the directory the images are stored in. If you want to use the same images as this example you can <a href="http://adambenjamin.com/blog/wp-content/flash_grid_images.zip">download them here</a>. Lets assume the images are in an images directory in the same folder as our SWF file.</p>
<pre lang="actionscript3">var imageDir:String = new String( "images /" );</pre>
<p>Once this file was being used in a web site, like <a href="http://red-issue.com">red-issue.com</a>, it would like get it&#8217;s list of images from an XML file. For now lets just store them in an Array.</p>
<pre lang="actionscript3">var gridArray:Array=new Array(
     ({image:"img_0001.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0002.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0003.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0004.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0005.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0006.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0007.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0008.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0009.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0010.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0011.jpg",imageWidth:800,imageHeight:532}),
     ({image:"img_0012.jpg",imageWidth:800,imageHeight:532}));</pre>
<p>Next we need a vaiable to determine the width and height of the photo grid.</p>
<pre lang="actionscript3">var gridWidth:Number=new Number(4);
var gridHeight:Number=new Number(3);</pre>
<p>Each photo im our grid is going to need a Movie Clip, a Mask and a Loader. Lets assume the number of photos we are putting in our grid is unknown. This means we will need to store these items in three sepereate Arrays.</p>
<pre lang="actionscript3">var movieArray:Array = new Array();
var maskArray:Array = new Array();
var loaderArray:Array = new Array();</pre>
<p>In this example we are determining the width of each photo using two Text Fields. When we go back to the <a href="http://adambenjamin.com/blog/?p=610" target="_blank">Deconstruct red-issue.com</a> post we will be using the Stage width and height. This next excerpt of code will only be used for this example.</p>
<pre lang="actionscript3">width_tb.tabIndex = 1;
height_tb.tabIndex = 2;
submit_btn.tabIndex = 3;
width_lb.text = "Width:";
width_tb.text = "100";
height_lb.text = "Height:";
height_tb.text = "100";
submit_btn.label = "Resize";</pre>
<p>When the Stage is resized, or in this example when the submit Button is pressed, we need to re-size our images and masks and then re-position everything in our grid. Lets break this into three seperate steps. All our clickHandler function has to do is trigger these three functions. These three functions will assume that the Movie Clips, Masks and Loaders already exist.</p>
<pre lang="actionscript3" escaped="true">submit_btn.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(evt:Event):void {
     for (var i:int = 0; i &lt; gridArray.length; i ++) {
          resizeMask(i);
          resizeImage(i);
          reposition(i);
     }
}</pre>
<p>The first functionwill reposition the Movie Clips and Masks based on the new image width and height.</p>
<pre lang="actionscript3" escaped="true">function reposition(i:int):void{
     maskArray[i].x = i % gridWidth * Number(width_tb.text);
     maskArray[i].y = Math.floor( i / gridWidth ) * Number(height_tb.text);
     movieArray[i].x = i % gridWidth * Number(width_tb.text);
     movieArray[i].y = Math.floor( i / gridWidth ) * Number(height_tb.text);
}</pre>
<p>The second function will resize the Masks.</p>
<pre lang="actionscript3" escaped="true">function resizeMask(i:int):void{
     maskArray[i].width = Number(width_tb.text);
     maskArray[i].height = Number(height_tb.text);
}</pre>
<p>And the last function will resize the Movie Clips. This is a little more complicated then resizing the masks as the Movie Clips need to remain proportianate.</p>
<pre lang="actionscript3" escaped="true">function resizeImage(i:int):void {
     var widthRatio:Number = Number(width_tb.text)/gridArray[i]["imageWidth"];
     var heightRatio:Number = Number(height_tb.text)/gridArray[i]["imageHeight"];
     if( widthRatio &gt; heightRatio){
          movieArray[i].scaleX = widthRatio;
          movieArray[i].scaleY = widthRatio;
     }else{
          movieArray[i].scaleX = heightRatio;
          movieArray[i].scaleY = heightRatio;
     }
}</pre>
<p>Now that all of our functions are set to go we just need to crreate our Masks, Loaders and Movie Clips. The first seven lines of our this for loop create and resize our Masks, the next seven after that create the image Movie Clips and load our images in and finally the two lines set the Masks and execite the reposition function.</p>
<pre lang="actionscript3" escaped="true">for (var i:int = 0; i &lt; gridArray.length; i ++) {
     maskArray[i] = new MovieClip();
     maskArray[i].graphics.lineStyle(0,0x000088);
     maskArray[i].graphics.beginFill(0x000088);
     maskArray[i].graphics.drawRect(0,0,1,1);
     maskArray[i].graphics.endFill();
     resizeMask(i);
     grid_mc.addChild(maskArray[i]);
     movieArray[i] = new MovieClip();
     var request:URLRequest=new URLRequest(imageDir + gridArray[i]["image"]);
     loaderArray[i] = new Loader();
     loaderArray[i].load(request);
     resizeImage(i);
     movieArray[i].addChild(loaderArray[i]);
     grid_mc.addChild(movieArray[i]);
     movieArray[i].mask=maskArray[i];
     reposition(i);
}</pre>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="700" height="400">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_grid_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_grid_file.swf" width="700" height="400">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_grid_file.fla">Download the Image Grid Flash File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=606</wfw:commentRss>
		</item>
		<item>
		<title>Flash XML Weather Feed</title>
		<link>http://www.adambenjamin.com/blog/?p=583</link>
		<comments>http://www.adambenjamin.com/blog/?p=583#comments</comments>
		<pubDate>Sun, 22 Feb 2009 20:12:41 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=583</guid>
		<description><![CDATA[Integrating Flash with XML is increasingly becoming more and more common and useful! Working with XML has also become a whole lot easier with Actionscript 3.0. In this example we will create a Flash file that will display the current weather. We will get our weather information from Boy Genius. The weather feeds are available [...]]]></description>
			<content:encoded><![CDATA[<p>Integrating Flash with XML is increasingly becoming more and more common and useful! Working with XML has also become a whole lot easier with Actionscript 3.0. In this example we will create a Flash file that will display the current weather. We will get our weather information from <a href="http://boygenius.com" target="_blank">Boy Genius</a>. The weather feeds are available <a href="http://weather.boygenius.com/feeds/" target="_blank">here</a>. For this example we are going to use the weather from <a href="http://weather.boygenius.com/feeds/ontario-toronto.xml" target="_blank">Toronto Ontario Canada</a>.</p>
<p>Here is the XML we will be working with:</p>
<pre lang="xml" escaped="true">&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
&lt;?xml-stylesheet type="text/xsl" href="/weather.xsl" alternate="no" ?&gt;
&lt;city&gt;
     &lt;name&gt;Toronto&lt;/name&gt;
     &lt;state&gt;Ontario&lt;/state&gt;
     &lt;temperatures&gt;
          &lt;fahrenheit&gt;27&lt;/fahrenheit&gt;
          &lt;celsius&gt;-2&lt;/celsius&gt;
     &lt;/temperatures&gt;
     &lt;conditions&gt;FLURRIES&lt;/conditions&gt;
     &lt;dewpoint&gt;18&lt;/dewpoint&gt;
     &lt;relative_humidity&gt;69&lt;/relative_humidity&gt;
     &lt;wind&gt;W28G33&lt;/wind&gt;
     &lt;wind_english&gt;West 28 MPH Gusts Reaching 33 MPH &lt;/wind_english&gt;
     &lt;barometric_pressure&gt;29.88R&lt;/barometric_pressure&gt;
     &lt;notes&gt;&lt;/notes&gt;
     &lt;wind_chill_fahrenheit&gt;11&lt;/wind_chill_fahrenheit&gt;
     &lt;heat_index_fahrenheit&gt;Not Applicable&lt;/heat_index_fahrenheit&gt;
     &lt;image&gt;http://www.srh.noaa.gov/weather/images/fcicons/na.gif&lt;/image&gt;
     &lt;coordinates&gt;
          &lt;latitude&gt;N/A&lt;/latitude&gt;
          &lt;longitude&gt;N/A&lt;/longitude&gt;
     &lt;/coordinates&gt;
     &lt;last_updated&gt;February 22, 2009 18:19:18 GMT&lt;/last_updated&gt;
     &lt;last_updated_seconds_gmt&gt;1235344758&lt;/last_updated_seconds_gmt&gt;
     &lt;info_courtesy_of&gt;The National Weather Service&lt;/info_courtesy_of&gt;
     &lt;source_url&gt;http://weather.noaa.gov/pub/data/raw/as/asus43.kfgf.rwr.fgf.txt&lt;/source_url&gt;
     &lt;feed_problems_contact&gt;xml@boygenius.com&lt;/feed_problems_contact&gt;
     &lt;xml_generation_script_author&gt;Todd Finney, Boy Genius Incorporated&lt;/xml_generation_script_author&gt;
&lt;/city&gt;</pre>
<p>Writing the Actionscript to import this XML and display it on the stage is a common assignment I give to my students. In this example we are just going to import the city, state and the image.</p>
<p>First lest define a Text Format Object to use with the weather city and state.</p>
<pre lang="actionscript3">var newFormat:TextFormat = new TextFormat();
newFormat.size = 20;
newFormat.color = 0x666666;
newFormat.font = "Arial";</pre>
<p>Next we need to define a URL Loader and an XML Object. We will load the XML using the URL Loader and then on the Load Complete Event we will put the content into an XML Object.</p>
<p>When the Loader Complete Event is triggered all the information about that event will go into the variable evt defined in the function definition. Inside that Event variable is the target of the event and in this case the data loaded. We need to put that data, which is just text at this point, and put it in an XML Object. This was Flash can do all sorts of XML type things.</p>
<pre lang="actionscript3">var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://weather.boygenius.com/feeds/ontario-toronto.xml"));
function LoadXML(evt:Event):void {
     xmlData=new XML(evt.target.data);
}</pre>
<p>Inside the loadXML function we need to add code that will create a Text Field, use our Text Format Object defined earlier and display the city and state from the XML document. Add the following code to the loadXML function.</p>
<p>To get the city and state our of our XML Object we simply refer to the XML Object and then add a dot and the item we are looking for. So in this example we want xmlData.name and xmlData.state. This gets a little more complicated when there are multiple copies of items in an XML document. For example pulling information about one image out of an XML document that includes information about many images.</p>
<pre lang="actionscript3">     var nameTextField:TextField = new TextField();
     nameTextField.text = String( xmlData.name ) + ", " + String( xmlData.state );
     nameTextField.selectable = false;
     nameTextField.width = 200;
     nameTextField.x = 10;
     nameTextField.y = 10;
     nameTextField.setTextFormat( newFormat );
     addChild(nameTextField);</pre>
<p>And lastly we need to add more code to the loadXML function to load the weather image and add it to the stage.</p>
<pre lang="actionscript3">     var imageLoader:Loader = new Loader();
     var imageRequest:URLRequest = new URLRequest(xmlData.image);
     imageLoader.load(imageRequest);
     imageLoader.x = 10;
     imageLoader.y = 100;
     addChild(imageLoader);</pre>
<p>The current weather in Toronto Ontario Canada is:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="400" height="80">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_weahter_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_weahter_file.swf" width="400" height="80">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_weahter_file.fla">Download the Flash Weather XML File</a></p>
<p>For anyone who has had some experience with Flash and XML you will quickly learn about a cross-domain policy. For some reason Flash has added a security feature to Flash which prevents an SWF from loading content from another domain at run-time. There is lots of information about this on the <a href="http://www.adobe.com/devnet/flashplayer/articles/cross_domain_policy.html" target="_blank">Adobe web site</a>. In theory this isn&#8217;t a bad idea; trying to prevent people from using other people&#8217;s content and their bandwidth is kind of noble. Except this security feature is so easy to get around they may as well have not built it in.</p>
<p>Our Weather example above will work fine when previewing the file in Flash. As soon as you put the file online Flash will refuse to load the XML from the Boy Genius site because they do not have a cross-domain policy. All we have to do is create a PHP file to grab the weather XML from Boy Genius and regurgitate it. Here is a three line PHP file that will regurgitate what ever XML file you tell it to.</p>
<pre lang="php" escaped="true">&lt;?php
header ("content-type: text/xml");
$file = file( $_GET['url'] );
echo implode( $file, chr(13) );
?&gt;</pre>
<p>Lets say for example we want to load the Toronto weather from the Boy Genius web site. All we need to do is call the above file, lets say it is called xmlCaller.php, and pass it a URL. The XML call in Flash would be:</p>
<pre lang="actionscript3">xmlCaller.php?url=http://weather.boygenius.com/feeds/ontario-toronto.xml</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=583</wfw:commentRss>
		</item>
		<item>
		<title>Deconstruct neave.com part 2</title>
		<link>http://www.adambenjamin.com/blog/?p=474</link>
		<comments>http://www.adambenjamin.com/blog/?p=474#comments</comments>
		<pubDate>Sat, 21 Feb 2009 14:55:44 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[deconstruction]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=474</guid>
		<description><![CDATA[This post is the completion of deconstructing the web site neave.com. Part one of this deconstructing can be found here.

Now that we have individually built all the components we just need to put them all together into a web site. On your main timeline create the following layer and frame structure:

On the footer timeline create [...]]]></description>
			<content:encoded><![CDATA[<p>This post is the completion of deconstructing the web site <a href="http://neave.com" target="_blank">neave.com</a>. Part one of this deconstructing can be found <a href="http://adambenjamin.com/blog/?p=343">here</a>.</p>
<p><a href="http://neave.com" target="_blank"><img class="alignnone size-full wp-image-344" title="flash_neave_screenshot" src="http://adambenjamin.com/blog/wp-content/flash_neave_screenshot.jpg" alt="" width="704" height="255" /></a></p>
<p>Now that we have individually built all the components we just need to put them all together into a web site. On your main timeline create the following layer and frame structure:</p>
<p><img class="alignnone size-full wp-image-561" title="flash_neave_timeline1" src="http://adambenjamin.com/blog/wp-content/flash_neave_timeline1.jpg" alt="" width="500" height="141" /></p>
<p>On the footer timeline create your footer Movie Clip and give it the name footer_mc. On the main layer put your content Movie Clip and name it content_mc. Inside yout content Movie Clip create the following layer and frame structure:</p>
<p><img class="alignnone size-full wp-image-562" title="flash_neave_timeline2" src="http://adambenjamin.com/blog/wp-content/flash_neave_timeline2.jpg" alt="" width="500" height="223" /></p>
<p><a href="http://adambenjamin.com/blog/wp-content/flash_neave_nocode.fla">Download the Flash file without the Actionscript</a></p>
<p>On frame one of our actions layer on the stage enter the following Actionscript:</p>
<pre lang="actionscript3">import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;

stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, resizeHandler);

resizeHandler(null);

function resizeHandler(event:Event):void {
     var sw:Number=stage.stageWidth;
     var sh:Number=stage.stageHeight;
     footer_mc.y = sh - 50;
     footer_mc.x = ( sw - 600 ) / 2;
     content_mc.y = ( sh - 200 ) / 2;
     content_mc.x = ( sw - 600 ) / 2;
}</pre>
<p>This code imports a few required classesand re-positions our site content based on the size of the Flash viewable area. This code is executed once at the begining and again when ever the stage is resized. This code, and most of teh code on this page, is discussed in more detail in the <a href="http://adambenjamin.com/blog/?p=343">Deconstructing neave.com Part 1</a> post. Most of this code was origianly taken from a <a href="http://www.adobe.com/devnet/flash/articles/liquid_gui_03.html#" target="_blank">Creating Liquid GUIs article</a> from the Adobe site.</p>
<p>Next click into the content_mc Movie Clip and on frame one of the actions layer goes the rest of our code. Lets start with importing a few required classes:</p>
<pre lang="actionscript3">import gs.TweenMax;
import gs.easing.*;
import flash.display.Bitmap;
import flash.display.BitmapData;</pre>
<p>The first two import commands are importing non Flash classes. You will need to have the TweenMax class in the same folder as your fla. I&#8217;ll mention this one more time&#8230;.most of this code is broken down in more detail in the initial <a href="http://adambenjamin.com/blog/?p=343">Deconstructing neave.com</a> post.</p>
<p>Next we are going to create a Movie Clip that will play a 320 pixel by 100 pixel FLV file when a menu option triggers a roll over event. The videos we will be using are <a href="http://adambenjamin.com/blog/wp-content/flash_neave_lines.f4v">lines</a>, <a href="http://adambenjamin.com/blog/wp-content/flash_neave_colours.f4v">colours</a>, <a href="http://adambenjamin.com/blog/wp-content/flash_neave_marsh.f4v">marsh</a> and <a href="http://adambenjamin.com/blog/wp-content/flash_neave_waves.f4v">waves</a>.</p>
<pre lang="actionscript3">var firstVideo:MovieClip = new MovieClip();
video_mc.addChild(firstVideo);</pre>
<p>Lets also set up everything we need to play a video so that all the rollover code will have to do is select the video to play.</p>
<pre lang="actionscript3">var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
function asyncErrorHandler(evt:AsyncErrorEvent):void {
     // Ignore errors
}
var vid:Video = new Video(320,100);
vid.attachNetStream(ns);
firstVideo.addChild(vid);</pre>
<p>The video we play on roll over will need to fill whatever space is available. Lets fill 800 pixels of space using videos that are 320 pixels wide. These two value will be used to determine how may times to repeat the video. There is more information on this concept on the <a href="http://adambenjamin.com/blog/?p=521">Videos Indefinately</a> post.</p>
<pre lang="actionscript3">var widthToFill:Number = new Number(800);
var videoWidth:Number = new Number(320);</pre>
<p>We will also need an unknown number of Movie Clips, Bitmap Objects and Bitmap Data Objects; so each time we create another set of these items we are going to store them in three Arrays.</p>
<pre lang="actionscript3">var videoArray:Array = new Array();
var bitmapArray:Array = new Array();
var bitmapDataArray:Array = new Array();</pre>
<p>Now we&#8217;re all set&#8230;.lets create enough Movie Clips and Bimap Objects to fill the space specificed above.</p>
<pre lang="actionscript3">for( var i:int = 1; i &lt; widthToFill / videoWidth; i ++ ) {
     videoArray[i] = new MovieClip();
     videoArray[i].x = i * 320;
     bitmapDataArray[i] = new BitmapData(320,100,false,0x000000);
     bitmapArray[i] = new Bitmap(bitmapDataArray[i]);
     if( i % 2 == 1 ){
          bitmapArray[i].scaleX = -1;
          bitmapArray[i].x = 320;
     }
     videoArray[i].addChild(bitmapArray[i]);
     video_mc.addChild(videoArray[i]);
}</pre>
<p>The first line determins how many sets we are going to need and loops the excerpt of code accordingly, The next two commands create a new Movie Clip and positions it. The two after that create a new Bitmap Object and a Bitmap Data Object. The code in the if statement will flip and reposition every second Movie Clip so are videos look seamless. And finally we add our Bitmap Object to our Movie Clip and we add our Movie Clip to our container Movie Clip.</p>
<p>Now we just need to set up a function to update our Bitmap Data Objects on the Enter Frame Event.</p>
<pre lang="actionscript3">function enterFrameHandler(evt:Event):void {
     for( var i:int = 1; i &lt; widthToFill / videoWidth; i ++ ) {
          bitmapDataArray[i].draw(firstVideo);
     }
}</pre>
<p>Finally! Our video roll overs are all set to go. Lets move on to adding the roll over code to each of our butons. First we need to give out container Movie Clip a staring blur and zero alpha.</p>
<pre lang="actionscript3">TweenMax.to( video_mc, 0.1, {blurFilter:{blurX:20, blurY:20}, autoAlpha:0} );</pre>
<p>This next excerpt is our actual roll over code. When our visitor rolls over our first option a number of things are happening. First we tween the vertial_mc, horizontal_mc and stripes_mc to the desired colours. Next we change the text to inform our visitor what sectino they have just rolloed over, in this example we choose &#8220;Neave Games&#8221;. Next we tween our mask)mc and stripes_mc to make sure the whole text is visible. In this case 395 is sufficient. There would be some more complex methods of figuring out this number, but for now we just guessed a few times until it looked right. And finally we play the desired video, activate our Enter Frame Functiuon from above and unblur and make visible the video.</p>
<pre lang="actionscript3">option1_mc.buttonMode=true;
option1_mc.addEventListener( MouseEvent.ROLL_OVER, option1OverHandler );
function option1OverHandler( evt:MouseEvent ):void {
     TweenMax.to(vertical_mc, 3, {tint:0x631553});
     TweenMax.to(horizontal_mc, 3, {tint:0xff00cc});
     TweenMax.to(stripes_mc, 3, {tint:0xff00cc});
     title_tb.text = "Neave Games";
     TweenMax.to(mask_mc, 0.5, {x:395});
     TweenMax.to(stripes_mc, 0.5, {x:395});
     ns.play("flash_neave_lines.f4v");
     video_mc.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
     TweenMax.to( video_mc, 1, {blurFilter:{blurX:0, blurY:0}, autoAlpha:1} );
}</pre>
<p>This next code is pretty much identical to the above roll over code with a diffent video to play, different colours and a different x position.</p>
<pre lang="actionscript3">option2_mc.buttonMode=true;
option2_mc.addEventListener( MouseEvent.ROLL_OVER, option2OverHandler );
function option2OverHandler( evt:MouseEvent ):void {
     TweenMax.to(vertical_mc, 3, {tint:0x185e1d});
     TweenMax.to(horizontal_mc, 3, {tint:0x1ff82e});
     TweenMax.to(stripes_mc, 3, {tint:0x1ff82e});
     title_tb.text = "Neave Television";
     TweenMax.to(mask_mc, 0.5, {x:475});
     TweenMax.to(stripes_mc, 0.5, {x:475});
     ns.play("flash_neave_marsh.f4v");
     video_mc.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
     TweenMax.to( video_mc, 1, {blurFilter:{blurX:0, blurY:0}, autoAlpha:1} );
}</pre>
<p>These last two sets of code can be duplicated for each option you want to have in your menu. On the other hand, on roll out we can reuse the same function for eavery button.</p>
<pre lang="actionscript3">option1_mc.addEventListener( MouseEvent.ROLL_OUT, optionOutHandler );
option2_mc.addEventListener( MouseEvent.ROLL_OUT, optionOutHandler );
option3_mc.addEventListener( MouseEvent.ROLL_OUT, optionOutHandler );
option4_mc.addEventListener( MouseEvent.ROLL_OUT, optionOutHandler );
function optionOutHandler( evt:MouseEvent ):void {
     TweenMax.to(vertical_mc, 3, {tint:0x111111});
     TweenMax.to(horizontal_mc, 3, {tint:0x222222});
     TweenMax.to(stripes_mc, 3, {tint:0x222222});
     title_tb.text = "Neave.com";
     TweenMax.to(mask_mc, 0.5, {x:325});
     TweenMax.to(stripes_mc, 0.5, {x:325});
     ns.pause();
     video_mc.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
     TweenMax.to( video_mc, 1, {blurFilter:{blurX:20, blurY:20}, autoAlpha:0} );
}</pre>
<p>And there we have it&#8230;something that resembles <a href="http://neave.com" target="_blank">neave.com</a>. Maybe not quite as nice but not bad for two three hour classes.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="800" height="350">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_neave_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_neave_file.swf" width="800" height="350">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_neave_file.fla">Download the Complete Flash File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=474</wfw:commentRss>
		</item>
		<item>
		<title>Video Indefinately</title>
		<link>http://www.adambenjamin.com/blog/?p=521</link>
		<comments>http://www.adambenjamin.com/blog/?p=521#comments</comments>
		<pubDate>Sun, 15 Feb 2009 19:49:18 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[bitmap]]></category>

		<category><![CDATA[bitmapdata]]></category>

		<category><![CDATA[flv]]></category>

		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=521</guid>
		<description><![CDATA[While Deconstructing Neave we came accross a situation where we needed a video to fill a horizontal space with an unknow width. We are going to achieve this by taking a 320 x 100 pixel video and repeat it using Bitmap Objects until we hit the end of the stage.

If you need n FLV fileyou [...]]]></description>
			<content:encoded><![CDATA[<p>While <a href="http://adambenjamin.com/blog/?p=474">Deconstructing Neave</a> we came accross a situation where we needed a video to fill a horizontal space with an unknow width. We are going to achieve this by taking a 320 x 100 pixel video and repeat it using Bitmap Objects until we hit the end of the stage.</p>
<p><img class="alignnone size-full wp-image-522" title="flash_indef_repeat" src="http://adambenjamin.com/blog/wp-content/flash_indef_repeat.jpg" alt="" width="700" height="120" /></p>
<p>If you need n FLV fileyou can download <a href="http://adambenjamin.com/blog/wp-content/flash_indef_waves.f4v">our waves video</a> which was taken from <a href="http://www.freestockfootage.com/" target="_blank">Free Stock Footage</a>. There is already a much more detailed post available on <a href="http://adambenjamin.com/blog/?p=433">Flash Bitmap Ojects</a>.</p>
<p>First we need to import the Flash Bitmap and Bitmap Data Object:</p>
<pre lang="actionscript3">import flash.display.Bitmap;
import flash.display.BitmapData;</pre>
<p>Next we need a Movie Clip that will be used to play the 320 by 100 pixel FLV.</p>
<pre lang="actionscript3">var firstVideo:MovieClip = new MovieClip();
addChild(firstVideo);</pre>
<p>We will define any numbers, like our video width and the width to fill, at the top of our code. This practice makes our code easy to resuse. For someone working with this code they do not have to understand the bulk of the code; they just change the settings at the top.</p>
<pre lang="actionscript3">var widthToFill:Number = new Number(690);
var videoWidth:Number = new Number(320);</pre>
<p>This code will require an unknow number of Movie Clips, Bitmap Objects and Mitmap DataObjects. We will use an array to store each of these.</p>
<pre lang="actionscript3">var videoArray:Array = new Array();
var bitmapArray:Array = new Array();
var bitmapDataArray:Array = new Array();</pre>
<p>The next loop will run once for each required video. It will create a new Movie Clip and add a new Bimap Object to that Movie Clip. For each second Movie Clip it will flip it using the scaleY property so that the videos line up seamlessly.</p>
<pre lang="actionscript3">for( var i:int = 1; i &lt; widthToFill / videoWidth; i ++ ) {
     videoArray[i] = new MovieClip();
     videoArray[i].x = i * 320;
     bitmapDataArray[i] = new BitmapData(320,100,false,0xffffff);
     bitmapArray[i] = new Bitmap(bitmapDataArray[i]);
     if( i % 2 == 1 ){
          bitmapArray[i].scaleX = -1;
          bitmapArray[i].x = 320;
     }
     videoArray[i].addChild(bitmapArray[i]);
     addChild(videoArray[i]);
}</pre>
<p>On the Enter Frame Event this loop will copy the data from our first Movie Clip to each required Bitmap Data.</p>
<pre lang="actionscript3">this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(evt:Event):void {
     for( var i:int = 1; i &lt; widthToFill / videoWidth; i ++ ) {
          bitmapDataArray[i].draw(firstVideo);
     }
}</pre>
<p>Now that all the Movie Clips, Bitmap Objects and Bitmap Data Objects are set up all we need to do is play the video:</p>
<pre lang="actionscript3">var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
function asyncErrorHandler(evt:AsyncErrorEvent):void {
     // Ignore errors
}
var vid:Video = new Video(320,100);
vid.attachNetStream(ns);
firstVideo.addChild(vid);
ns.play("flash_indef_waves.f4v");</pre>
<p>And there we have it, a seamless repetition of a video. Click the video to replay it.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_5" width="700" height="100">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_indef_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_indef_file.swf" width="700" height="100">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_indef_file.fla">Download the Indefinate Flash FLA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=521</wfw:commentRss>
		</item>
		<item>
		<title>Creating a Video Reflection</title>
		<link>http://www.adambenjamin.com/blog/?p=513</link>
		<comments>http://www.adambenjamin.com/blog/?p=513#comments</comments>
		<pubDate>Sun, 15 Feb 2009 18:19:47 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<category><![CDATA[bitmap]]></category>

		<category><![CDATA[bitmapdata]]></category>

		<category><![CDATA[flv]]></category>

		<category><![CDATA[reflection]]></category>

		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://adambenjamin.com/blog/?p=513</guid>
		<description><![CDATA[A lot of this post will use some of the concepts from the post about using the Flash Bitmap Object. We will combine this along with some gradient drawing code taken Learnola to play an FLV video with a reflection using 100% Actionscript. If you need a video you can download our trippy lines video [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of this post will use some of the concepts from the post about using the <a href="http://adambenjamin.com/blog/?p=433">Flash Bitmap Object</a>. We will combine this along with some gradient drawing code taken <a href="http://learnola.com/2008/11/11/flash-cs3-tutorial-gradient-fills-in-actionscript-3/" target="_blank">Learnola</a> to play an FLV video with a reflection using 100% Actionscript. If you need a video you can <a href="http://adambenjamin.com/blog/wp-content/flash_bitmap_lines.f4v">download our trippy lines video</a> which was taken from <a href="http://www.freestockfootage.com/" target="_blank">Free Stock Footage</a>.</p>
<p>First we need to import a few objects:</p>
<pre lang="actionscript3">import flash.display.Bitmap;
import flash.display.BitmapData;</pre>
<p>Next we need to create two Movie Clips. The first on will be used to play an FLV and the second one will be used to hold the Bitmap Object and act as a reflection.</p>
<pre lang="actionscript3">var firstVideo:MovieClip = new MovieClip();
firstVideo.x = 10;
firstVideo.y = 10;
addChild(firstVideo);
var secondVideo:MovieClip = new MovieClip();
secondVideo.x = 10;
secondVideo.y = 492;
addChild(secondVideo);</pre>
<p>Next we are going to create a Bitmap Object and link it to a Bitmap Data Object. We are also going to set the scaleY property to -1 to flip the video vertically</p>
<pre lang="actionscript3">var myBitmapData:BitmapData = new BitmapData(320,240,false,0xffffff);
var myBitmap:Bitmap = new Bitmap(myBitmapData);
myBitmap.scaleY = -1;
secondVideo.addChild(myBitmap);</pre>
<p>Next we attach a function to the Enter Frame Event. This function, running 24 times a second, will copy the data from our first MovieClip to our Bitmap Data Object.</p>
<pre lang="actionscript3">this.addEventListener(Event.ENTER_FRAME,drawUpdate);
function drawUpdate(evt:Event):void {
     myBitmapData.draw(firstVideo);
}</pre>
<p>Then we play our video and add it to the first Movie Clip.</p>
<pre lang="actionscript3">var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
function asyncErrorHandler(evt:AsyncErrorEvent):void {
     // Ignore errors
}
var vid:Video = new Video();
vid.attachNetStream(ns);
firstVideo.addChild(vid);
ns.play("flash_bitmap_lines.f4v");</pre>
<p>This next excerpt of code was taken from <a href="http://learnola.com/2008/11/11/flash-cs3-tutorial-gradient-fills-in-actionscript-3/" target="_blank">Learnola</a>. It uses some of the Flash draing Classes to create a gradient.</p>
<pre lang="actionscript3">var linGrad:String = GradientType.LINEAR;
var linMatrix:Matrix = new Matrix();
linMatrix.createGradientBox(320,30,1.57);
var linColors:Array = [0x000000, 0x000000];
var linAlphas:Array = [0.20, 0];
var linRatios:Array = [0, 255];
var lin:Sprite = new Sprite();
lin.graphics.beginGradientFill(linGrad, linColors, linAlphas, linRatios,linMatrix);
lin.graphics.drawRect(0, 0, 320, 30);
lin.x = 10;
lin.y = 251;
addChild(lin);</pre>
<p>And finally we cache both the gradient and video as a bitmap. This allows us to use the gradient as a mask.</p>
<pre lang="actionscript3">secondVideo.cacheAsBitmap = true;
lin.cacheAsBitmap = true;
secondVideo.mask = lin;</pre>
<p>And there we have it&#8230;a 100% Actionscript reflection, click the video to play is again:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_6" width="340" height="300">
      <param name="movie" value="http://adambenjamin.com/blog/wp-content/flash_reflection_file.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://adambenjamin.com/blog/wp-content/flash_reflection_file.swf" width="340" height="300">
      <!--<![endif]-->
        <p><p>The Flash plugin is required to view this object.</p></p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p><a href="http://adambenjamin.com/blog/wp-content/flash_reflection_file.fla">Download the Reflection FLA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adambenjamin.com/blog/?feed=rss2&amp;p=513</wfw:commentRss>
		</item>
	</channel>
</rss>
