Daemon

 
Home / Services / Adobe Training / Tips & Tricks / Flash Remoting Section

Easy Flash Remoting Part Three

Author: Andrew Muller

The previous two parts examined making the connection with the Flash Remoting server and handling the results that it had returned. In this part we will look at displaying the results in the Flash movie using ActionScript.

The first thing to do is delete or comment out the trace function from within the original for loop that we used to dump our results in the previous part. Next we'll write the output code, as this series of exercises is discussing the use of ActionScript we'll look at a new MovieClip method introduced with Flash MX.

createTextField allows for the programmatic creation of an empty TextField within a movie. It uses six arguments, in order: instanceName, depth, x, y, width & height.

Within the loop code we'll use createTextField to dynamically create individual lines of text for each of the records returned by Flash Remoting.

In the sample code below we are dynamically naming each new TextField object by concatenating "text" and the current loop value. The loop value is also used as the depth value for the field and, in place of carriage returns, to calculate the x position of the field.

getDepartments_Result function
getDepartments_Result function

The next thing is to assign the current query value retrieved by Flash Remoting to the TextField.

A new addition to ActionScript in Flash MX is the TextField object, it has an extensive number of methods and properties that allow the developer much more control over text. The text property is used to assign a value to a TextField.

The first thing to do is resolve the name of the TextField so that the text property can be used in our code. In the code above we create a variable "x" equal to the name of the current TextField, this is done using the eval function.

In part two of this series we saw in the information returned by the NetConnect Debugger that the query result set was returned by the Flash Remoting server as a structure with one of the keys containing the records organised as an array. Each index of this array represents a record, the indexes contain a structure, the keys of that structure are used to represent the columns returned by the query.

Next we set the value of that text property equal to the query result, the department column, which is part of the records retrieved that we are presently looping over. We assign "x.text" equal to "result.items[i].department".

Completed code
Completed code

Running the Flash movie now, by pressing Ctrl + Enter, will show that the values returned by Flash Remoting are displayed on screen in a default serif font.

Code output
Code output

The TextFormat object was introduced with in Flash MX to allow for the formatting of text dynamically. It is used by creating a new instance of the object, assigning values to specific properties, then invoking the object to apply the formatting changes.

getDepartments_Result function revised to include text formatting
getDepartments_Result
function revised to include text formatting

In the code above we're creating a new TextFormat object called "myTextFormat" and setting the font property to "Arial" to change the appearance of our text. We then use the setTextFormat method of the TextField object to apply the changes.

Testing the code in Flash MX returns the following result.

Revised code output, formatting applied
Revised code output, formatting applied

Easy Flash Remoting part four discusses adding interactivity to the dynamic text displayed from the Flash Remoting results.

Download the files for this article. (104kb)

Daemon now does Macromedia certified Flash MX training

Menu

Social Profiles