September 16, 2005

Pulling random elements from an array

A handy bit of action script to pull random elements from an array, I recently used this function for a flash piece where I needed 3 random images from a pool of 9 images given from an XML file:

The ActionScript:

var tstArray:Array = new Array(1,2,3,4,5,6,7,8,9);
trace(tstArray)
for(var i:Number = 0; i<3; i++){
          var x:Number = Math.floor((Math.random()*tstArray.length));
          trace("X :: " + x);
          trace("ARRAY VALUE :: " + tstArray[x]);
          tstArray.splice(x,1);          
     }
delete tstArray;
trace("----------------------------------");

Hope someone finds use for this!

-DesDev    

September 9, 2005

LoadVars, C# and HTML

This may or may not be a commonly known issue with Flash/.Net developers.

It was brought to my attention from a friend of mine that he was having an issue when using LoadVars.SendAndLoad() to send a flash form with information and retrieve a status code. What was happening was the result was returning, but with all of the HTML appended to the value of the result.

Immediately, I asked him if the ASPX page was calling Response.End() after the result was written to the page. Sure enough it wasn't. After making the small change to the ASPX code, all was working perfectly.

Below is an example illustrating corrected implementation:

The Flash Code:

var lv:LoadVars = new LoadVars();
var res_lv:LoadVars = new LoadVars();
res_lv.onLoad = function(success:Boolean){
if(success){
          trace(this.nameSent);
     }
}

lv.strName="DesDev";
lv.sendAndLoad("http://localhost/loadVarsExample/vars.aspx",res_lv);


The ASPX (C#) Code:

<head>
<title>An HTML Page</title>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
  if (!IsPostBack){

     string strName = Request.Form["strName"];
     
     Response.Write("nameSent=" + strName);
     Response.End();
  }
}
</script>
</head>
<body>
<p>This is a paragraph</p>
<p><strong>Bold Text </strong></p>
</body>
</html>


Test out the differences by commenting out Response.End() in the ASPX page.

-DesDev

September 7, 2005

Editable combo box based on a selected index

I'm currrently working on a simple application that includes a combo box list of choices. One of the options is "other" which would then allow the user to input their own.

At first I thought if the "other" option was selected, then have an additional text field added dynamically and allow the user to enter their custom choice there.

I retracted that idea in favor of having the user to be able to add their option on the fly in the dropdown.

Here is the code below in order to get this to function the way I needed it to:

Create a combo box on the stage and name it "my_cb".
my_cb.addItem({label:"Option 1"});
my_cb.addItem({label:"Option 2"});
my_cb.addItem({label:"Option 3"});
my_cb.addItem({label:"Option 4"});
my_cb.addItem({label:"Option 5"});
my_cb.addItem({label:"Option 6"});
my_cb.addItem({label:"Other (click here to type your own)"});

var aObj:Object = new Object();
aObj.close = function(evt:Object) {
var selIndex:Number = evt.target.selectedIndex;
selIndex == 6 ? evt.target.editable=true : evt.target.editable=false;
};

my_cb.addEventListener("close", aObj);
Now when I process my form options, I couldn't use the standard "my_cb.getSelectedItem" method to retrieve the value. So use of the (hidden) "getValue()" method does the trick.
my_cb.getValue();

Hope that helps anyone else looking to achieve this functionality as well!

-DesDev

August 9, 2005

Adding Icons to a List Component

I haven't done too much with Flash Communication Server, but I had fun on a project building a chat application without using the built-in components.

One of the requests I had was to have an icon of sorts appear next to a moderators name in a list component that was logged into the chat.

So a little trial and error eventually lead me to search online and finally came to the code below:

Create a list called myList;

Create a MovieClip and make sure the linkage ID is named "myIcon"

Cut and paste the following code:

myData = [{label:"Label1",data:"Data1",icon:"myIcon"}];
myList.iconField = "icon";
myList.dataProvider = myData;

FLV and Keyframes

So I was working on a project that involved encoding some quicktime movies to FLV. No big deal except that the FLV's had to be progressive and when I built my FLV viewer using the NetStream object, I could not for the life of me figure out why everything worked great with the exception of my seek bar to scrub the downloaded portion of the movie.

Apparently, when working with progressive FLV's, it's best to encode them with a frequent keyframe rate so that a seek control will work well.

Wonder where that was in the documentation ;)

Thanks to PixelFumes for letting me in on that.