as3 passing a string from an array to a function

Tag: arrays , string , actionscript-3 Author: tshxy1101 Date: 2012-09-01

I have an empty array collecting a hex value thats randomly selected from another array within a function. i am trying to pull the hex value from the array that gets filled and pass it to another function to randomly change the color value of a particle system...

private var ca:Array = new Array();
private var rc:String = ca; // pseudo...this is the string that needs to get passed

    public function addCursor(cursor:Cursor):void {

    var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
    var rcc:String = cc[Math.floor(Math.random() * (cc.length))];
    ca.push(rcc); // 

    trace(rcc + ' 1st array');
    trace(ca + ' 2nd array');
    trace(rc + ' string to pass');

// unrelated stuff happens down here...

the 1st and 2nd arrays both trace the same hex value, but i cant find the right way to capture that string...ive tried several different methods that all return 'null' ...which makes me think maybe the value is leaving the array before i try to snag it?

i removed ca.pop() which is called in a later function, just to see if rc would still return a null and it does.

Best Answer

rcc is a string, not an array.


trace(rcc + ' chosen hex as a string');

But I don't see why you need an array (ca) just to hold a single string.

This would work:

private var rc:String;
private var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");

public function addCursor(cursor:Cursor):void {

rc = cc[Math.floor(Math.random() * (cc.length))];

trace(rc + ' the chosen hex');



ah! duh. cheers... overthinking it i suppose. works just dandy :) thanks.