Groups | Blog | Home
all groups > flash actionscript > november 2006 >

flash actionscript : Map problem


FrankvZ
11/4/2006 10:50:04 PM
I am trying to make a map game where you get the name of an area and have to
click on the correct area. I have programming experience but am fairly new to
Actionscript. At this point I want every area to change color when i click on
it. I have now 3 areas (MovieClips) named area0, area1 and area2 and the
following code:

import flash.geom.ColorTransform;
import flash.geom.Transform;
for(var i = 0; i < 3; i++){
// Loop through every area.
var thisArea = this['area'+i];
// Do something if the area is clicked.
thisArea.onRelease = function(){
trace("Clicked:"+this._name);
var colorTrans:ColorTransform = new ColorTransform();
var trans:Transform = new Transform(thisArea);
colorTrans.rgb = 0x1AFF00;
trans.colorTransform = colorTrans;
}
}

However, this only changes the color of the last area in the for-loop
(area2). I don't understand why, as according to the documentation variables
within functions should be local. Any help would be greately appreciated.
Frank
DazFaz
11/4/2006 11:35:18 PM
Hi Frank,

Ive found your problem. I hope its clear enough.

You have written:-
//--------------------------------------------
import flash.geom.ColorTransform;
import flash.geom.Transform;
for (var i = 0; i<3; i++) {
// Loop through every area.
var thisArea = this['area'+i];
// Do something if the area is clicked.
thisArea.onRelease = function() {
trace("Clicked:"+this._name);
var colorTrans:ColorTransform = new ColorTransform();
var trans:Transform = new Transform(thisArea);//<<---- this being where your
problem is
colorTrans.rgb = 0x1AFF00;
trans.colorTransform = colorTrans;
};
}
//-----------------------------------------

When it should be:-
//----------------------------------------
import flash.geom.ColorTransform;
import flash.geom.Transform;
for (var i = 0; i<3; i++) {
// Loop through every area.
var thisArea = this['area'+i];
// Do something if the area is clicked.
thisArea.onRelease = function() {
trace("Clicked:"+this._name);
var colorTrans:ColorTransform = new ColorTransform();
var trans:Transform = new Transform(this);//<<--- "this" refering to this
movie instance
colorTrans.rgb = 0x1AFF00;
trans.colorTransform = colorTrans;
};
}
//---------------------------------------
FrankvZ
11/5/2006 5:29:06 AM
AddThis Social Bookmark Button