TUTORIALS

This blog contain al java,j2ee,javabeans ejb,rmi complete reference with lots of examples and source codes based tutorials,free downloads,basic to detailed view,and lots more...

Friday, November 6, 2009

Flash Animations


Flash Animations


Animation 1:
The following steps are designed to work in Flash MX 2004, but they should work in Flash MX and maybe even Flash 5:
i. Create a new document in Flash. Set the movie's width to 300 and movie's height to 200. Set your Flash movie's frame rate to 25.
ii. Now, draw a filled-in circle using the Circle tool. Your drawing area should look like the following image:

[ draw a circle ]
iii. Once your circle has been drawn, select your circle and press F8. The Convert to Symbol dialog box should appear. Select the option for Movie Clip and press OK.
iv. Select your newly converted movie clip if it is not selected. Give your movie clip the instance name, dot:

[ give your movie clip the instance name 'dot' ]
v. Now, select your movie clip and press F9. The actions dialog box will appear. Copy and paste the following section of code:
onClipEvent (load) {
//data you may want to change
width = 300;
height = 200;
speed = Math.round(Math.random()*2)+1;
//initial positions
x = this._x=Math.random()*width;
y = this._y=Math.random()*height;
x_new = Math.random()*width;
y_new = Math.random()*height;
}
onClipEvent (enterFrame) {
//x movement
if (x_new>this._x) {
sign_x = 1;
} else {
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) { this._x += sign_x*speed; } else { x_new = Math.random()*width; } //y movement if (y_new>this._y) {
sign_y = 1;
} else {
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) { this._y += sign_y*speed; } else { y_new = Math.random()*height; } } vi. Now, select the first frame of your timeline. Press F9 again to display the Actions dialog box for the frame. Copy and paste the following section of code into that frame: i = 0; while (i<25) mouseinterval="setInterval(changeAlpha,100);" _alpha =" Math.round(_root._xmouse/550*100);" snowflakes =" 75;" snowflakes =" 75;" width =" 300;" height =" 200;" _xscale =" this._yscale="50+Math.random()*100;" _alpha =" 20+Math.random()*50;" _x =" -width+Math.random()*(3*width);" _y =" -10+Math.random()*height;" i =" 1+Math.random()*2;" k =" -Math.PI+Math.random()*Math.PI;" rad =" 0;" xmovement =" _root._xmouse;">(width+50)) {
this._x = -45;
this._y = Math.random()*height*2;
}
if (this._x<-50) { this._x = width+45; this._y = Math.random()*height*2; } if (this._y>=height) {
this._y = -50;
this._x = -width+Math.random()*(3*width);
}
The above three sections of code are fairly self-explanatory. The three sections control when to remove a snowflake from display and when to display it again. There are two horizontal boundaries and one vertical boundaries set in the code. Because the width and height are constrained by the actual values of the width and height variables, the boundaries scale evenly with the size of your movie.

Animation 4:

We will start off by drawing 2 objects on the stage and making them movie clips (MC’s).
The first MC is your character. For now, we will draw a circle. Draw your circle, select it and press F8. Once you circle is a movie clip double click it. Then select all of the shape. Press Ctrl + K and align the shape horizontally centered and bottom edge We now need to give it an instance name, for this tutorial we will call it char.
Your next MC will be your ground. So, draw a rectangle about 20px thick underneath your character, so when we add jumping he has something to fall on to. Once you have created it give the instance name of ground.
Basic Movement
Now for our first bit of code, in a platform game, you will need left and right movement. The following code also has friction in it. Friction gives the game a more realistic feel:


Go down to the actions for your character movie clip and add this code:
onClipEvent (load) {
speed = 0;
maxmove = 15;
}
onClipEvent (enterFrame) {
if (_root.dead) {
this.gotoAndStop("dead");
} else {
speed *= .85;
if (speed>0) {
dir = "right";
} else if (speed<0) dir = "left" dir ="="" dir ="="">-maxmove) {
speed--;
}
this.gotoAndStop("run");
this._xscale = -100;
} else if (Key.isDown(Key.RIGHT)) {
if (speed<1>-1 && !attacking) {
speed = 0;
this.gotoAndStop("idle");
}
}
}
Gravity and Jumping
Next up we need jumping. This code will give you jumping; it would be inserted after the other key.isDown events:
if (speed<1>-1 && !attacking) {
speed = 0;
this.gotoAndStop("idle");
}
if (Key.isDown(Key.UP) && !jumping) {
jumping = true;
}
if (jumping) {
this.gotoAndStop("jump");
this._y -= jump;
jump -= .5;
if (jump<0) falling =" true;" jump =" -15;" jumping =" true;" jump =" 0;" speed =" 0;" maxmove =" 15;" jump =" -15;" jump =" 12;" jumping =" false;" falling =" false;" jumping =" true;" speed =" 0;" maxmove =" 15;" jump =" 0;" dir ="="">0) {
dir = "right";
} else if (speed<0) dir = "left" dir ="="" dir ="="">-maxmove) {
speed--;
}
this.gotoAndStop("run");
this._xscale = -100;
} else if (Key.isDown(Key.RIGHT)) {
if (speed<1>-1) {
speed = 0;
this.gotoAndStop("idle");
}
if (Key.isDown(Key.UP) && !jumping) {
jumping = true;
}
if (jumping) {
this.gotoAndStop("jump");
this._y -= jump;
jump -= .5;
if (jump<0) falling =" true;" jump =" -15;" jump =" 12;" jumping =" false;" falling =" false;" dir ="="" dir ="="" text="0;" speed="0;." dir ="="" dir ="="" enemyspeed =" 2;" enemystepsright =" 0;" enemystepsleft =" 0;" enemydir = "left" enemydir ="="" _xscale =" -100;" enemydir ="="" _xscale =" 100;" enemystepsright ="="" enemystepsright =" 0;" enemydir = "left" enemystepsleft ="="" enemystepsleft =" 0;" enemydir = "right" dir="right" _xscale="100;" run="" attack="" attacking="true;" then="" next="" part="" go="" on="" your="" characters="" actions="" but="" stops="" from="" going="" frame="" which="" it="" will="" due="" code="" sets="" character="" idle="" when="" the="" is="" below="" 1="" and="" greater="" that="" negative="" so="" we="" need="" to="" add="" this="" into="" our="" else="" if="" speed="0;"><1>-1 && !attacking) {
speed = 0;
this.gotoAndStop("idle");
}
The last code is for the enemy, it needs to check if the enemy is hitting our attack point, which we placed on, our characters attack frame. Insert this on the enemy object:
onClipEvent (enterFrame) {
if (this.hitTest(_root.char.attackpoint)) {
enemyspeed = 0;
enemystepsright = 0;
enemystepsleft = 0;
dead = true;
this.gotoAndStop("dead");
}
if (this.hitTest(_root.char) && !dead) {
_root.char.jumping = false;
_root.dead = true;
}
When doing this it is vital you put the italic part (the part that kills you character) after the bold part, as the script reads down, and it sets that you enemy is dead before it hits your character.
Now we have set attacking true, we need to set it false, when control is released. So add this to the bottom of your char.
if (_root.ground.hitTest(this._x, this._y, true) && falling) {
jump = 12;
jumping = false;
falling = false;
}
}
}
onClipEvent (keyUp) {
// on Key Up
if (Key.getCode() == Key.CONTROL) {
// if the release is control
attacking = false;
// attacking is false
}
}
More Animations
In both your character and enemy MCs you need to put it a new frame, with the label dead. This is the frame where it shows your dead characters.
Obstacles
Probably the easiest things in the game to do are the obstacles such as spikes, etc. With this code we will not kill the enemy but set the _root timeline position back to 0 and send the character to a specified position and then resets all our variables. All you need to do is insert your obstacle image, make it a MC, and give it this code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
_root._x = 0;
_root.char._x = _root.char.startX;
_root.char._y = _root.char.startY;
_root.char._y = _root.char.startY+Stage.height/2;
_root.char.speed = 0;
}
}
We also need our characters’ Y position to be collected at the start of the game, so flash knows where to moves the character to:
onClipEvent (load) {
jumping = true;
speed = 0;
maxmove = 15;
Ypos = this._y;
jump = 0;
}
Health
Now for health we need to do multiple things. We firstly need to make another dynamic text box with the instance name health. We then need to set all our variables.
So, on the frame we will need to add:
score.text=0;
health.text=100;
Then on our obstacles we need to check if the health is smaller than or equal to 0, which is italics (you only need this on one obstacle) then we need to check that they are not dead before we set health down, so insert this:
onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
if (_root.health.text<=0) { _root.dead = true; } if (!_root.dead) { _root.health.text -= 5; _root._x = 0; … … _root.char.speed = 0; } } } And we need to make the health move with the screen, so with the character edit in: if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true)) { _root.health._x += speed; _root.score._x += speed; this._x += speed; _root._x -= speed; } if (dir == "left" && !_root.rightblock.hitTest(this._x-20, this._y, true)) { _root.health._x += speed; _root.score._x += speed; this._x += speed; _root._x -= speed; } To set the health up with a pickup you would put _root.health.text += 1 instead of the score so: onClipEvent (enterFrame) { if (this.hitTest(_root.char)) { _root.health.text += 1; unloadMovie(this); } } Pick up and Shoot Gun Now we get to the hardest part of the whole game, being able to pick up, and then shoot a gun. From the start, when I was creating my gunning abilities: First of all, I drew my picture of a gun, made that a MC, and gave it a code a code similar to the pickup items codes but setting a variable (gotgun) true, and then this is later checked and if it is true it will fire. Here it is: onClipEvent (enterFrame) { if (this.hitTest(_root.char)) { _root.gotgun = true; unloadMovie(this); } } I then had to draw in my shooting frames into my character MC, and then gave it a frame label of shoot. After that I added in 2 points of codes to my character. i. onClipEvent (load) { jumping = true; speed = 0; maxmove = 15; jump = 0; _root.maxshoottime = 100; } ii. onClipEvent (enterFrame) { if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) { this._y += 6; jump = 0; jumping = true; } if (!_root.shooting) { _root.timer = 0; _root.mvsp = _xscale/20; } … iii. } else if (Key.isDown(Key.CONTROL)) { this.gotoAndStop("attack"); attacking = true; speed = 0; } else if (Key.isDown(Key.SPACE)) { if (_root.gotgun == true && !_root.shooting) { _root.attachMovie("bullet", "bulleter", 1, {_x:_root.char._x, _y:_root.char._y-25}); _root.shooting = true; with (_root.bulleter) { onEnterFrame = function () { if (_root.timer>_root.maxshoottime) {
_root.shooting = false;
unloadMovie(this);
}
_root.timer++;
_x += _root.mvsp;
};
}
speed = 0;
this.gotoAndStop("shoot");
}
} else if (speed<1>-1 && !attacking) {

You now have to draw your bullet. Once you have drawn it press F8 to make it a MC. Then open your library, find your bullet, Right-click it, go to linkage and click it. Then select the checkbox next to “Export for Action Script”. Now go to the Identifier field and type bullet.
One final thing is, you may notice that after a while of playing your game all your objects go out of place. To fix that you would insert in a code to re-enforce them. To re-enforce objects when moving your _root timeline you need to set their x position to the position in which they started at minus the current position of the _root.

So for that you need to place in a code into your characters load actions to get the starting x co-ordinates of your objects. We will also get the y position of your character for when he dies.
onClipEvent (load) {
jumping = false;
speed = 0;
maxmove = 15;
healthX = _root.health._x;
scoreX = _root.score._x;
Xpos = this._x;
Ypos = this._y;
_root.maxshoottime = 100;
}
And in the enterFrame events you re-enforce their positions:
onClipEvent (enterFrame) {
_x = Xpos-_root._x;
_root.score._x = scoreX-_root._x;
_root.health._x = healthX-_root._x;
if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
---

Labels:

XML Sockets in Flash

XML Sockets in Flash
By Richard Lyman
April 02, 2002


Familiarization
In this article, the XML language is not explained at all. For a tutorial on XML, see XML Basics at http://www.devshed.com/Server_Side/XML/XMLBasics. I do plan on discussing certain methods of formatting your XML so that parsing in the Flash Player 5.04 is faster. Flash has always been meant to facilitate SVG, primarily for web delivery, and recently also for other mediums of presentation. With any presentation there will be data that needs to be conveyed. Be it through a web site shopping cart, or an ATM infomercial - data is the heart of the matter.

XML was introduced as a means of freeing the languages we use to define the data we traffic in. Since XML is more a way of defining data rather than a strict method of defining data, there have been many *MLs introduced in an effort to best define the data particular to a given industry. While this massive effort to define data is laudable, we need to remember that sometimes it doesn't really matter how we define it, as long as all of the members of the conversation understand the language that is being spoken. To this end Flash's XMLSocket object works wonders.

True XML is often accompanied by a DTD, to help foreign environments interpret and manipulate the accompanying XML. Flash currently does not support a DTD. While it can interpret the DTD tag, and return the information contained therein, Flash does not validate XML from the DTD. While this could hamper Flash’s ability to survive in an increasingly ‘XMLish’ world of data, we'll see how it really doesn't matter.

Another important point to consider when speaking of Flash and its XMLSocket object is that Flash has a history of being the tool easiest to produce great works of non-usability. Many first impressions of Flash were of it's pointless self-aggrandizing intros, or maybe of inescapable pre-loaders. As I said - Flash's development environment facilitates the creation of unusable content. As we approach the XMLSocket object, we must then keep in mind that Flash (currently) has limitations to its true usability. But there are many developers who have helped rid us of many of those limitations, if we choose to use their tools, and the upcoming versions of Flash appear promising indeed.

In the end, Flash is preparing to dominate a market that did not exist before. Used properly, and effectively, the XMLSocket object will play a key role in that future. The web will not exist in its current form for long. Already we see changes in the fundamental uses of the sprawling Internet, and yet there are purposes that drive that change which will remain forever. Data will need to be exchanged. Through one language or another, using a web services platform or a web server, data will need to be conveyed.

The XMLSocket object brings about the marriage of both data and presentation in an environment not possible before. Those on the client end, and those with their hands on the stirring spoons can see changes as the bi-directional exchange of data is presented in the robust and warm environment Flash has to offer.

That having been said... let's examine the beast that's known as the XMLSocket object.


Methods and Properties
I'll enumerate the different methods and properties that this object contains, and then explain common coding practices.

close() - This method allows you to cleanly terminate an open socket from the client's end of communication.

connect() - This method allows you to connect to a server running a socket server that is able to interpret the EOL character that is currently standard for Flash sockets (the ASCII null character, or '\0'). It will also eventually invoke the onConnect event-handler. This method accepts two parameters: a string as the URL of the server to connect to, and an integer as the port that the socket server is waiting on.

send() - This method allows you to send a string of characters through the socket connection. While the string is usually in XML format it does not have to be. This string can either be constructed using the XML object within Flash, or manually if size permits. Flash will also automatically append the terminating null character for you.

onConnect() - This event-handler is invoked either on success or failure for the connection attempt. It will return a Boolean based on the connections status (not to be confused with the 'status' property of a parsed XML object).

onXML() - This event-handler is invoked upon receipt of a null character. When this event is triggered the data that was received since the last null character has already been parsed, and placed into a hierarchy that you can use to retrieve the data – again using the XML object in Flash.

onData() - This event is similar to the onXML event, but it is triggered before any parsing. This allows for manual parsing or custom string handling (which can be quite useful as we’ll see near the end of this article).

onClose() - This event is not as intuitive as it might seem. It is triggered when the server terminates the connection - not when the client terminates. This is generally used to handle an unexpected disconnection from the server.

Common Utilizations
Having outlined the methods and event handlers common to the XMLSocket object, let’s discuss how they all work together. Here is some sample code for a connection from Flash to a server with no functionality for sending, receiving, or parsing.
________________________________________
myXML = new XMLSocket;
myXML.onConnect = handleConnect;
myXML.connect("http://www.yourServer.com", 12345);

function handleConnect(connectionStatus){
connectionStatus ? trace("Connected.") : trace("Connection failed.");
}
________________________________________
As you can see, there really isn't that much to initiating a socket connection to your server.

So that you can test your Flash code, here is a very condensed socket server written in Perl. This server has no special functionality built in. It simply echoes anything it receives from any client to all clients connected. Notice that the only real change needed from a standard basic socket server is the setting of the null character as the EOL indicator (in Perl: $/="\0";).
________________________________________
use IO::Socket;
use IO::Select;
$SIG{PIPE}='IGNORE';
$m=new IO::Socket::INET(Listen=>1,LocalPort=>2229);
$O=new IO::Select($m);
$/="\0";
while(@S=$O->can_read){
foreach(@S){
if($_==$m){
$C=$m->accept;$O->add($C);
}else{
my $R=sysread($_, $i, 2048);
if($R==0){
$T=syswrite($_, '', 2048);
if($T==undef){
$O->remove($_);
}
}else{
foreach $C($O->handles){
$T=syswrite($C, $i,2048);
}
}
}
}
}
________________________________________
As was mentioned in the previous section, any language can handle Flash's sockets, as long as it can open ports, and recognize the null character as an EOL character. Socket apps have been written in Perl, PHP, Java, C++, and I'm sure in many other languages.

Since there really isn't any need to explain the above ActionScript code, we will provide an example that uses most of the functionality the object has to offer.
________________________________________
myXML = new XMLSocket;
myXML.onConnect = handleConnect;
myXML.onXML = handleXML;
myXML.onClose = handleDisconnect;
myXML.connect("http://www.yourServer.com", 12345);

function handleConnect(connectionStatus){
connectionStatus ? trace("Connected.") : trace("Connection failed.");
}

function handleXML(xmlObject){
trace("Object recieved:: "+xmlObject);
}

function sendXML(textToSend){
myXML.send(new XML('"+textToSend+""));
}

function handleDisconnect(){
trace("Connection lost.");
}

function closeConnection(){
trace("Closing connection to server.");
myXML.close();
}
________________________________________
That pretty much covers the basic implementation of the methods, and event handlers. But that's only the starting point. Where you take it from here will depend on the solution you're working to implement.

Possibilities
As I mentioned - where you go from here is up to you. XML has strength where other markup languages do not, in that it is a framework, not a finished product. You can define your data any way you'd like. As long as you code so that the clients and/or the server understand the language they speak, you can do anything, anyway you'd like.

For example, how about your own graphical presentation of the newest article from your favorite web site?? Simple. Write your server app to accept a string like, "", grab the HTML page, filter out the content needed, and send back a reply to that client like, "The info you requested". Then it would be a simple matter in Flash to parse the XML reply from the server into the graphical representation you designed. Tied to personalization, a user can visit your web site, and see 'live' updates of the content they want - presented the way you/they wanted it.

Speaking of requesting information and replying - how about minimizing the load your server experiences through HTTP requests for database information?? Many sites currently submit a new request for each segment of data that the client needs. Using Flash to maintain a persistent socket connection to your server, while the socket app running on your server also maintains a persistent connection to the database might allow you to funnel data to the client in 'real-time' with out the overhead of either page reloads, or new requests.

There are other advantages as well. Since this entire system is simply a socket connection on each end, with XML being the preferred language, you could send strings of numbers that each end could interpret as actions/reactions to situations. Many websites have designed full server engines, and client environments that allow for 'real-time' multi-user content. Until a few months ago the main focus of a multi-user app has been for chat, but many others are looking to more diverse methods of enabling client interaction. While Quake has yet to make an appearance in Flash, multi-user 3D, as well as 2D and ISO environments exist.

A benefit to the separation of ActionScript from other languages is that ActionScript can focus on the presentation of the data, and not necessarily the manipulation of it. If you've ever had some killer app that grabbed the data by the tail and swung it around, you might also want a simple means of delivering that data to others. Flash's graphical abilities combined with other languages data power though the XMLSocket object could be the answer.

Summary
All in all the XMLSocket object allows for an increased vocabulary between clients and/or server. This allows the developer to bring about more dynamic ‘real-time-capable’ data. The XMLSocket object also facilitates the transferal of information from storage to use. The XMLSocket object is only one of many methods available for this transferal, yet it’s advantages give the developer an increased range of motion in delivering that data to the client.

There are precautions that need to be taken, as Flash does not currently support encoding of a robust nature. There are Flash developers who have scripted objects that deal with MD5 encryption, yet they tend to be bulky, and unwieldy. Care must also be taken when addressing issues of usability in Animated representation of data. The Flash Player version 5 needs code snippets to deal with a loss of frame rate when parsing larger XML documents, yet the Flash Player 6 Beta release does not.

Attention should also be focused on the structuring of the XML document or string that is to be sent to Flash. Flash tends to favor less parent/child node structure, and more an attribute type of structure. For instance, the string:
________________________________________



Text1




Text2



________________________________________
Would be better defined like this:
________________________________________




________________________________________
if at all possible. There are circumstances that do not allow for this style of structure, but it should be used if it can.

It should also be remembered that Flash does not have to accept only XML formatted data. By using the ‘onData()’ event handler we can process other forms of information. Why send a string like this:
________________________________________


234


34


4


________________________________________
When the string: “234|34|4” would more than suffice? The XMLSocket does not use ‘headers’ like standard HTML does, nor does it require bulky tag explanations if you don’t want it to. The joy in the XMLSocket implementation is that you can define your data anyway you wish and display it from your server to anywhere you can squeeze Flash!

Labels:

Getting Dynamic Data into Macromedia Flash



Getting Dynamic Data into

Macromedia Flash

Because the Web is such a dynamic environment, manually adding content takes too long. This is why databases have been long-time companions to websites. So why not learn how to take that same dynamic data and pull it directly into Macromedia Flash MX?

Introduction

It did not take long for Web designers to realize that content is king on the Internet. Your website may look cool, but if the content isn't there then you are likely not to have too many visitors coming back. In this article, you will learn how Flash MX can receive content from a database and then present that within a Flash Movie. This is the first step needed to create Flash-based applications. You open up a whole new world of possibilities once you know how to get database content into Flash.

Download source files here.

Flash and Databases

Flash has been able to receive data from databases since the release of Flash 3; the connection is made through a technology called Server Scripting. Traditional client server applications have typically gone directly through an ODBC connection or JDBC connection. The most common server scripting languages are Microsoft's Active Server Pages, Java Server Pages, and Macromedia ColdFusion. The server-generated page collects the database content and passes it on to Flash, as shown in Figure 1.

Figure 1Figure 1 matthewdavid.ws has all of the content for the site dynamically loaded into it from different databases.

The way in which data is passed is something similar to this:

Step 1. A Web page is downloaded with a Flash movie embedded within it.

Step 2. The Flash movie makes a request to a server-generated Web page.

Step 3. The server-generated Web page connects to a database and receives the content made in the request.

Step 4. The database content is used to generate a dynamic Web page, which is sent to the Flash movie.

Step 5. Flash receives the Web page and ActionScript within the movie tells Flash where to place the content so that it can be viewed.

Creating the Database

The first step is to create the database. For this example, I am going to use a Microsoft Access database (see Figure 2). The reason is that Access is easy to use and most designers and developers have access to it. For more robust websites that receive a lot of traffic, you may want to think about using Oracle, SQL Server, or MySQL.

Figure 2Figure 2 Microsoft Access is used to build the database.

The database has one table called RandomRec with two fields: ID and Statement. That's it. Later, when you are more comfortable working with Flash, you can experiment with more complex databases.

Server Scripting Languages

The next step is to create the server-generated page that will connect to the database and extract the content that will be presented to Flash. For this article I am using Microsoft's ASP technology.

You will need to have access to a Microsoft Internet Information Server (IIS). This is the application server that translates the ASP code we write.

You need to create a website on the server. If you are unfamiliar with how to do this, contact your local ISP.

Place the Access database that you have just created within the root of the new website on Microsoft Internet Information Server.

Open Notepad or your favorite text editor and type the following code:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim MM_connstr_STRING
MM_connstr_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
       Server.MapPath("usability.mdb")
Dim rsUsability
Dim rsUsability_numRows
Set rsUsability = Server.CreateObject("ADODB.Recordset")
rsUsability.ActiveConnection = MM_connstr_STRING
rsUsability.Source = "SELECT * FROM RandomRec ORDER BY Statement ASC"
rsUsability.CursorType = 0
rsUsability.CursorLocation = 2
rsUsability.LockType = 1
rsUsability.Open()
rsUsability_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index
Repeat1__numRows = -1
Repeat1__index = 0
rsUsability_numRows = rsUsability_numRows + Repeat1__numRows
%>
<% 
While ((Repeat1__numRows <> 0) AND (NOT rsUsability.EOF)) 
%>
<%=(rsUsability.Fields.Item("Statement").Value)%>


<% 
 Repeat1__index=Repeat1__index+1
 Repeat1__numRows=Repeat1__numRows-1
 rsUsability.MoveNext()
Wend
%>
<%
rsUsability.Close()
Set rsUsability = Nothing
%>

Save the page as usability.asp. The script opens up a connection with the Usability database, retrieves the correct data out of the table RadmonRec, and repeats all of the rows of data.

Preview this page through the Web browser (see Figure 3). You should be able to view all of the data from the database.

Figure 3Figure 3 Here you can see the ASP page displaying content. Later, this information will be pulled into Flash.

The next step is to load this data directly into Flash.

Loading Dynamic Data

Believe or not, the hardest part has been completed. The next step is to pull the content into Flash.

For this exercise I am using Flash MX for the Flash 6 player. It handles data differently than Flash 5 and earlier. You will find that it is more efficient and more flexible.

1. Create a new movie in Flash. Size the movie 400 x 270 pixels.

2. Select the Text Tool from the Tools Panel and drag a text field on the stage 384 x 270 pixels. The Text field is intentionally smaller than the entire movie. This will be made clearer later.

3. Select the new text field. Change the type of text field to Dynamic.

4. Name the movie "statement". Note that with Flash MX you can name a text field in the same way that you can name a movie. That is, you no longer need to give the Text Field a Variable name. This makes working with ActionScript much easier and less confusing.

Change the Line Type to Multiline (see Figure 4) and click the Render Text as HTML button (see Figure 5).

Figure 4Figure 4 The Text Field is the dynamic element that will contain all of the information we need to display the loaded server-generated movie.

Figure 5Figure 5 The Render Text as HTML button formats text with HTML markup.

5. The next step is to load the data into the movie. This has to be achieved with ActionsScript.

Select Frame 1 of the Timeline and open the Actions Panel. You will need to write the following ActionScript into the Actions Panel.

6. The first step is to declare that a data file is going to be loaded. This is done with a new variable called "data" and is declared through the method "new LoadVar."

var data = new LoadVars();

7. You have to tell Flash where to find the data. In this example, the final SWF movie and ASP page will all reside in the same folder.

data.load("usability.asp");

8. Now that you have the content loaded into Flash you need to tell Flash what do with it. Create a new function, called "Value", that defines how to present the data.

9.  function Value() {
   for (content in this) {

10. Using the Text ActionScript controls in Flash MX you can tell Flash that the content needs to be placed in the movie called "statement." The "htmltext" property explains that the content coming into Flash contains some HTML formatting.

11.     statement.htmltext += content;
12.   }
}

13. The final statement inserts the content when the movie has fully loaded.

data.onLoad = Value;

14. The entire script looks like this (see Figure 6):

15.var c = new LoadVars();
16.c.load("usability.asp");
17.function showValues() {
18.   for (i in this) {
19.     statement.htmltext += i;
20.   }
21.}
c.onLoad = showValues;

Figure 6Figure 6 The scripts are placed into the Actions Panel.

At this point you can export the Flash Movie as usability.swf. View the usability.swf file in your Web browser. You will need to do this because the ASP file needs to be compiled by the server before it can be presented into the Flash Player.

Notice that the content from the ASP file loads successfully into the SWF movie, as shown in Figure 7. But you cannot view all of the content. The final step in this movie is to add a scroll bar that will allow you to view all of the content. This is why we left a little room on the stage.

Figure 7Figure 7 Here you can see the data presented in the final exported Flash Movie.

Scroll Bar Component

One of the default sets of components that ships with Flash MX is the Flash UI Components. Open the Components panel because you will want to look for the ScrollBar component in this set.

Drag the ScrollBar Component away from the Components panel and onto the Stage. Drag the ScrollBar over the Statement Text Field. You will notice that the Scroll Bar snaps into position on the right side of the text field. Save and Export the Flash Movie.

Go to your Web browser and view the new movie on your Web page, as shown in Figure 8. You will see that the scroll bars now automatically sizes to reflect the length of the content loaded into the Text Field. Pretty cool, huh?

Figure 8Figure 8 The ScrollBar component is added to the Dynamic Text Field.








Labels: