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