Attempting Flash ActionScript

Zalbag

Limp Gawd
Joined
Aug 24, 2002
Messages
330
Ok, I am going nuts....

I have this flash file that's a map of counties in Alabama, I've made each county a Button symbol and gave each instance a name like [countyname]_btn, for example baldwin_btn. I am trying to set the onRelease event to call a function that takes the name of a county and calls an external JavaScript function. Instead of adding code for each button (there's 67 countys in Alabama, 67 * 3 lines of code = 201 lines of code :( ) I am attempting to write a small piece of code that automatically cycles through all the button instances and sets the correct onRelease call.

Here's what I have so far:
Code:
import flash.external.ExternalInterface;

function loadCompanies(county:String):Void {
	// ExternalInterface.call("loadcompanies", "al", county);
	trace(county); // testing
}

for (i in this) {
    if (this[i] instanceof Button) {
		var temp:String = this[i]._name;
		var countyName:String = temp.slice(0, temp.indexOf('_'));
		
		this[i].onRelease = function():Void {
			loadCompanies(county);
		}
    }
}

I thought this would work OK but in reality, it doesn't :( Clicking on any county results in one county name being printed. I've set a breakpoint and ran through the debugger and everything looks OK, it's setting the correct values and all but each anonymous function isn't retaining the correct value.... Any ideas?

EDIT: For some reason there's a gaping space problem on the loadCompanies function line, this isn't present in my actual code in Flash just when posting the code to the board. I tried deleting + retyping that line but it's not fixing the space :(

Thanks,

Zalbag
 
>.>

<.<

>.<

I am hella dumb. I figured out a better way to do what I need AND it works:

Code:
import flash.external.ExternalInterface;

for (i in this) {
    if (this[i] instanceof Button) {
		this[i].onRelease = function() {
			var countyName:String = this._name.slice(0, this._name.indexOf('_'));
			trace(countyName); // testing for right now...line will change to use ExternalInterface
		}
	}
}

yeah, so I am officialy retarded........
 
Zalbag,

Your first try at it would have worked, but you made one mistake.

You are passing a variable "county" to the loadCompanies function, but the variable county does not exist. I think you meant to use countyName instead of county.

So the correct code would be:
Code:
for (i in this) {
    if (this[i] instanceof Button) {
		var temp:String = this[i]._name;
		var countyName:String = temp.slice(0, temp.indexOf('_'));
		
		this[i].onRelease = function():Void {
			loadCompanies([COLOR=Red]countyName[/COLOR]);
		}
    }
}

Other than that I can't see a real difference between your two posts.

-Heaton00
 
Back
Top