Posts Tagged ‘ Sheridan ’

Sample Flash AS3 Code to Help Designers and Developers Handle Data and Communication

Key to the Sheridan Interactive Multimedia program curriculum is teaching learners to build environments in which end users can communicate with each other. To do this, we need interfaces for input and display and we have to get data to and from the server. Think facebook, flickr, YouTube, etc.

Adobe Flash provides many communication classes and methods such as:

  • User Interface Components:
    • Button
    • CheckBox and RadioButton (Check Box & Radio Button)
    • ComboBox and List (Combo Box)
    • TextArea, TextInput and Label (Text Area & Input)
    • NumbericStepper and ScrollPane (Numeric Stepper & Scroll Pane)
    • Slider and TileList (Tile List)
    • DataGrid (Data Grid)
  • navigateToURL to load a Web Page from Flash
  • SharedObject to save a Flash “cookie”
  • FlashVars & SWF Query String to pass variables into Flash
  • ExternalInterface to call a function in Flash from JavaScript
  • Loading a SWF with Loader and URLRequest
  • FileReference to upload a local file to be used by flash
  • URLLoader and URLVariables to retrieve text or server output
  • XML to send and receive XML
  • Web Services to connect to scripts on other servers
  • Remoting to avoid serializing and deserializing data

Here are links to content examples from our curriculum for all of the above except Web Services and Remoting. The Sheridan lessons include more than the content – there are supporting lesson plans, definition documents, steps for building, supplementary links, reflection forms, and in class explanation, lab work, assignments, exams and final projects. If you are looking into schooling in multimedia, please visit the Sheridan Interactive Multimedia site for more information on our one year post grad program.

User Interface Components

Click here for code example

Flash AS3 Components

Communication 1 – navigateToURL, SharedObject, FlashVars, ExternalInterface, Loader, URLRequest and FileReference

Click here for code example

Flash AS3 Communication

Communication 2 – URLLoader and URLVariables to get text or server script data (PHP, MySQL)

Click here for code example

Although we recommend the Falcon Data Class for this type of connection

Flash AS3 Communication

XML in Flash

Click here for code example

Although we recommend the Falcon Data Class for this type of connection

XML in Flash CS3

Going From Flash ActionScript 3 to ActionScript 2

REASON

We are teaching AS3 to this year’s Sheridan Interactive Multimedia and some students have not seen AS2. We want the students to be somewhat aware of AS2 so if they see AS2 in the workplace they will have an idea of what is going on. Our mobile prototypes will be coded in Flash Lite 3 which is also AS2 based.

So this is an unusual situation. Most literature on the Web talks about how to convert or go from AS2 to AS3. In general, this can be turned around to retrograde from AS3 to AS2 but it is a little annoying to read if you have not done any AS2 and only know AS3. It is difficult because the writing assumes that you know AS2.

This post is to help those who know AS3 to write code in AS2. It is certainly not exhaustive as there were about 800 changes to the language. See:
http://livedocs.adobe.com/flex/201/langref/migration.html

It should be noted that we absolutely love AS3 and would not for a second want to return to AS2. AS3 has been so much easier to teach because of its consistency. It is more organized, flexible, powerful and fast. There is an initial organization set up. But once you are over that, it is almost redundant in its usage. For a chart overview of AS3 organization, please see:
http://www.flickr.com/photos/danzen/1382459629/

ORGANIZATION

You can choose to make an AS2 Flash file and when you do the code window shows AS2 and AS1 code in the left hand reference area. This is organized differently than all the nicely organized packages in AS3. Most of the classes you will want to look at are under ActionScript 2 Classes > Core or Movie. Movie is a hodgepodge of classes specific to Flash like MovieClip, TextField, etc. So that will look somewhat familiar.

Most coders coded on the timeline in a single timeline script on the first frame of the Movie. Sit on the first frame and press F9 to see the code window. You do not need to declare packages or start a class – you just code with variables and functions.

Advanced coders imported classes from external AS files. These could be in a package although there is no package block in the external AS itself – you put the package name at the front of the class name: class package.ClassName { You can then make a class approximately the same way as AS3 in the external AS file. If you wanted to make a MovieClip call a class when an instance of the MovieClip is created, you would set the linkage on the MovieClip to export for ActionScript and then set the Class to the external AS file. Slightly different than AS3.

People who were used to AS1 coding sometimes put code out on buttons and on MovieClips. We will not address that code – but watch out for it if you can’t seem to find any code in a legacy project.

BASIC DIFFERENCES

  • There is no Sprite class in AS2 we used the MovieClip class instead.
  • There are a number of properties of MovieClips and TextFields that started with _. Here are the most used ones in AS2 I am sure you will recognize the translation. Just look through properties of a MovieClip in AS2 to spot these:
    • _x, _y, _alpha, _visible, _width, _height, _xscale, _yscale
  • In AS3 we use a value from 0 to 1 for a various properties. In AS2 these are usually from 0 to 100. For instance:
    • myClip.alpha = .7 in AS3 is myClip._alpha = 70 in AS2
  • Instead of stage to reference the root timeline we used _root to reference the root timeline. If you are embedding a swf into another swf that other swf becomes the _root unless in the swf you are embedding you write _lockroot = true.
  • There is no addChild() – we used the _visible property.
  • Flash had _level for depth management so look up myClip.swapDepths(otherClip) and myClip.swapDepths(myClip.getNextHighestDepth()) – yum.

MOVIECLIP & TEXTFIELD DIFFERENCES

  • You did not instantiate the MovieClip class like new MovieClip(). Instead you had these methods available:
    • holderClip.createEmptyMovieClip(“name”, depth); // you figure out the depth.
    • holderClip.duplicateMovieClip(“name”, depth); // makes a copy (want in AS3)
    • holderClip.attachMovie(“linkageName”, “name”, depth); // from the library
    • And similarly, you did not instantiate a TextField like new TextField – instead:
    • holderClip.createTextField(“name”, depth, x, y, width, height);

EVENT DIFFERENCES

  • Common events were handled with a function literal

myButton.onRelease = function() { // note there is no click event
trace (“hi”);
trace (this); // will give you reference to myButton
}

  • Then there is the addListener() method:

myListener = new Object();
myListener.onKeyDown = function() {

if (Key.getCode() == 39) {
trace (“right arrow”);
}
}
Key.addListener(myListener);

  • And the addEventListener() method for components and custom events:

var myListener = new Object();
myListener.someEvent = function() {
trace (“event captured”);
}
myObject.addEventListener(“someEvent”, myListener);

  • And you would dispatch an event by adding the following code to your class

private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;

  • And in your constructor:

mx.events.EventDispatcher.initialize(this);

  • And finally where you want to dispatch the event:

this.dispatchEvent({type:”someEvent”, target:this, param:”value”});

MISSING AND CHANGED CLASSES

There are many classes that you do not have – it is easy enough to figure out what they are – they will not be there ;-) . But in some cases there are substitutes. Such as:

  • Timer() -> use setInterval() function like so:

clearInterval(myID);
myID = setInterval(myFunction, 1000, parameter);
// or in a class
myID = setInterval(this, “myMethod”, 1000, parameter);
// scope gets all mixed up – it’s a pain

  • You have none of the .net classes for passing data instead use LoadVars:

myVars = new LoadVars();
myVars.id = 12345;
newVars = new LoadVars();
newVars.onLoad = loaded;
// or if in a class
newVars.onLoad = mx.utils.Delegate.create(this, loaded);
myVars.sendAndLoad(“serverscript”, newVars, “POST”);

private function loaded() {
answer = newVars.answer;
}

  • XML class is completely different – it is not E4X. To access nodes it is something like this:

// if you read in XML you will want to set the:
myXML.ignoreWhite = true;
myXML.childNode[0].childNodes gives you access to the nodes under the root node
// then you can use properties like so:
myXML.childNode[0].childNodes[0].nodeName // gives node name
myXML.childNode[0].childNodes[0].attributes.age // gives value of age attribute
myXML.childNode[0].childNodes[0].nodeValue // gives value of node

COMPONENT DIFFERENCES

Well, there are probably a number of differences here but obvious ones are availability. In AS2 there were more components so check out the component panel to find:

  • AS2 – Tree – XML data made into a directory tree
  • AS2 – Accordion – bars to click on to reveal content between
  • AS2 – Menu and Menu Bar – for drop down menus
  • AS2 – A bunch of data components for connecting to datasets, XML and webservices
  • No Slider or TileList in AS2.

CONCLUSION

This has been an interesting trip through memory lane. Once again, we love AS3 and do not want to go back. Thank you Adobe for continuing to make improvements.

Dan Zen

Sheridan Interactive Multimedia Group Visits TVO

Students in the Sheridan Interactive Multimedia one year post-grad program in Oakville are given client projects where a group of four or five build an interactive feature or site for live clients. This term, one our clients is TVO (formerly TV Ontario) and our students, and professor Dan Zen, went for a visit to their office in Toronto. Bob Tarle, Innovation Program Manager, and our client project contact, led the tour.

tvovisit.jpg

We could tell that TVO and Bob are enthusiastic about convergence. On our tour, Bob must have greeted 50 people by name in many offices across many floors. The offices have been redesigned for openness and Bob’s has glass walls that are used by people as a white board – a glass board ;-) . The results are encouraging. TVO is doing well in new areas such as podcasting and alternative platforms such as game platforms. We were also shown e-learning applications and samples of content on the Wii.

The students are prototyping a media player using Adobe Flash and AIR and the H.264 codec. Their visit really helped them see how their project will fit within TVO. Pictured above are the students: Danni, Corey, Jeff, Anissa and Kristen with Bob Tarle from TVO. Inventor, Professor, Dan Zen accompanies the students through the props room, various studios and the TVO Kids offices.

We would like to thank Bob and TVO for working with our students and giving such a great tour of their important institution. These types of arrangements are really very beneficial for all parties. We got a nice note back from Bob that shows the type of involvement and experience:

Hi All,

FYI – Your visit on Friday was a highlight of my week, thanks. We will want to have you back before the holiday or early in the new year to present the working widget to some internal stakeholders and answer any questions they may have. I also would like to solidify a visit to Oakville for late Friday afternoon of this week or early the following week. I know that this is a busy time for all of you but remember not to let up. Sleep and tv are highly overrated:)

Bob

Sheridan Interactive Multimedia – http://imm.sheridanc.on.ca

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: