Reputation: 39
for (var i=0; i<=98; i++) {
box1.addEventListener(MouseEvent.CLICK, func(i));
}
function func(i:int):Function{
return function paint(e:MouseEvent):void{
var myColorTransform:ColorTransform = new ColorTransform();
if (i%4==0) {
myColorTransform.color = 0xFF0000;
}
else if (i%4==1) {
myColorTransform.color = 0x0000FF;
}
else if (i%4==2) {
myColorTransform.color = 0x00FF00;
}
else if (i%4==3) {
myColorTransform.color = 0xFFFF00;
}
box1.transform.colorTransform = myColorTransform;
}
}
I want to run a loop through the event listeners in such a way that whenever I click the object its color transforms into the next one. The above code just changes the color once and produces the color for the last value of i (98) which is green. Please help. I am beginner to ActionScript.
Upvotes: 0
Views: 93
Reputation: 1
Well.. you shouldn't do it this way, in your example, each time you click the box1, you will trigger 98 times func() , and the last time it's trigger, i is equal to 98, I don't think it's what you expect...
A best way to do this, is to store your i somewhere, you can store it into box1 for example ( if it's a movieclip ) , then you can retrieve it, read and increase it easily in your paint() method. This way you only have to have 1 addEventListener on your box1.
Upvotes: 0
Reputation: 1857
I think its will be enough one event listener and some counter. Something like that (not tested):
var clickCounter:uint = 0;
box1.addEventListener(MouseEvent.CLICK, onBoxClick);
function onBoxClick(event:MouseEvent):void {
var myColorTransform:ColorTransform = new ColorTransform();
if (clickCounter%4==0) {
myColorTransform.color = 0xFF0000;
}
else if (clickCounter%4==1) {
myColorTransform.color = 0x0000FF;
}
else if (clickCounter%4==2) {
myColorTransform.color = 0x00FF00;
}
else if (clickCounter%4==3) {
myColorTransform.color = 0xFFFF00;
}
box1.transform.colorTransform = myColorTransform;
clickCounter++;
}
Upvotes: 1