actionscript 3 - Box color Changing Game -
what want accomplish through code whenever click on of 16 boxes reddish color next time on other(or same) box bluish color. code if click other box first gives me reddish color (always) , next color when click same box again, is, boxes' colors independent of each other. please help, beginner actionscript. in advance.
var i=0; box1.addeventlistener(mouseevent.click, func1(i)); box2.addeventlistener(mouseevent.click, func2(i)); box3.addeventlistener(mouseevent.click, func3(i)); box4.addeventlistener(mouseevent.click, func4(i)); box5.addeventlistener(mouseevent.click, func5(i)); box6.addeventlistener(mouseevent.click, func6(i)); box7.addeventlistener(mouseevent.click, func7(i)); box8.addeventlistener(mouseevent.click, func8(i)); box9.addeventlistener(mouseevent.click, func9(i)); box10.addeventlistener(mouseevent.click, func10(i)); box11.addeventlistener(mouseevent.click, func11(i)); box12.addeventlistener(mouseevent.click, func12(i)); box13.addeventlistener(mouseevent.click, func13(i)); box14.addeventlistener(mouseevent.click, func14(i)); box15.addeventlistener(mouseevent.click, func15(i)); box16.addeventlistener(mouseevent.click, func16(i)) function func1(i:int):function{ homecoming 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++; } } function func2(i:int):function{ homecoming 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; } box2.transform.colortransform = mycolortransform; i++; } } function func3(i:int):function{ homecoming 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; } box3.transform.colortransform = mycolortransform; i++; } } function func4(i:int):function{ homecoming 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; } box4.transform.colortransform = mycolortransform; i++; } } function func5(i:int):function{ homecoming 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; } box5.transform.colortransform = mycolortransform; i++; } } function func6(i:int):function{ homecoming 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; } box6.transform.colortransform = mycolortransform; i++; } } function func7(i:int):function{ homecoming 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; } box7.transform.colortransform = mycolortransform; i++; } } function func8(i:int):function{ homecoming 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; } box8.transform.colortransform = mycolortransform; i++; } } function func9(i:int):function{ homecoming 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; } box9.transform.colortransform = mycolortransform; i++; } } function func10(i:int):function{ homecoming 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; } box10.transform.colortransform = mycolortransform; i++; } } function func11(i:int):function{ homecoming 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; } box11.transform.colortransform = mycolortransform; i++; } } function func12(i:int):function{ homecoming 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; } box12.transform.colortransform = mycolortransform; i++; } } function func13(i:int):function{ homecoming 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; } box13.transform.colortransform = mycolortransform; i++; } } function func14(i:int):function{ homecoming 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; } box14.transform.colortransform = mycolortransform; i++; } } function func15(i:int):function{ homecoming 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; } box15.transform.colortransform = mycolortransform; i++; } } function func16(i:int):function{ homecoming 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; } box16.transform.colortransform = mycolortransform; i++; } }
to this, have declare i
object-wide, , don't utilize 16 functions, create 1 function , employ event.target
property find out box clicked, alter colortransform
. also, it'll improve if utilize 4 static color transform objects instead of creating 1 each time click, can wait.
static var cts:vector.<colortransform>=vector.<colortransform>([]); cts.push(new colortransform(0,0,0,1,0xff,0,0,0)); cts.push(new colortransform(0,0,0,1,0,0,0xff,0)); cts.push(new colortransform(0,0,0,1,0,0xff,0,0)); cts.push(new colortransform(0,0,0,1,0xff,0xff,0,0)); // these short forms of color transform initialization, // instead of making 1 , assigning "color", pass required parameters // @ creation time. // add together more in case need static var nexttransform:int=0; function paint(e:event):void { var dest:displayobject = e.target displayobject; if (!dest) return; // should never trigger, precaution anyway dest.transform.colortransform = cts[nexttransform]; nexttransform++; if (nexttransform>=cts.length) nexttransform=0; }
in case need function behave originally, should cast dest
movieclip
or class boxes instead of displayobject
, , employ dest.nexttransform
in place of nexttransform
everywhere within function.
also, own education, should seek , find out how code behave in case you'd decide add together more colors, , why have alter much in original code if so.
actionscript-3 function colors addeventlistener
No comments:
Post a Comment