我希望通过这个代码实现,每当我点击16个盒子中的任何一个时,我会得到一个红色,但是下次我在任何其他(或相同)盒子上这样做时,我会得到一个蓝色。这段代码的作用是,如果我点击任何其他框,它首先给我一个红色(总是),只有当我再次点击同一个框时才会得到下一个颜色,即所有框'颜色彼此独立。请帮忙,我是动作脚本的初学者。提前谢谢。
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{
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++;
}
}
function func2(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;
}
box2.transform.colorTransform = myColorTransform;
i++;
}
}
function func3(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;
}
box3.transform.colorTransform = myColorTransform;
i++;
}
}
function func4(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;
}
box4.transform.colorTransform = myColorTransform;
i++;
}
}
function func5(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;
}
box5.transform.colorTransform = myColorTransform;
i++;
}
}
function func6(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;
}
box6.transform.colorTransform = myColorTransform;
i++;
}
}
function func7(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;
}
box7.transform.colorTransform = myColorTransform;
i++;
}
}
function func8(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;
}
box8.transform.colorTransform = myColorTransform;
i++;
}
}
function func9(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;
}
box9.transform.colorTransform = myColorTransform;
i++;
}
}
function func10(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;
}
box10.transform.colorTransform = myColorTransform;
i++;
}
}
function func11(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;
}
box11.transform.colorTransform = myColorTransform;
i++;
}
}
function func12(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;
}
box12.transform.colorTransform = myColorTransform;
i++;
}
}
function func13(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;
}
box13.transform.colorTransform = myColorTransform;
i++;
}
}
function func14(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;
}
box14.transform.colorTransform = myColorTransform;
i++;
}
}
function func15(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;
}
box15.transform.colorTransform = myColorTransform;
i++;
}
}
function func16(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;
}
box16.transform.colorTransform = myColorTransform;
i++;
}
}
答案 0 :(得分:0)
要执行此操作,您必须在对象范围内声明i
,并且也不要使用16个函数,只需创建一个函数并使用Event.target
属性来查找单击了哪个框,然后改变其colorTransform
。此外,如果您使用4个静态颜色转换对象而不是每次单击时创建一个对象会更好,但这可以等待。
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 are short forms of color transform initialization,
// instead of making one and assigning "color", you pass required parameters
// at its creation time.
// add more in case you need
static var nextTransform:int=0;
function paint(e:Event):void {
var dest:DisplayObject = e.target as DisplayObject;
if (!dest) return; // should never trigger, but a good precaution anyway
dest.transform.colorTransform = cts[nextTransform];
nextTransform++;
if (nextTransform>=cts.length) nextTransform=0;
}
如果您需要此功能的行为与原来一样,您应该将dest
投射到MovieClip
或您的框而不是DisplayObject
,并使用dest.nextTransform
代替nextTransform
这个函数中的任何地方。
此外,对于您自己的教育,您应该尝试查看此代码如何处理,以防您决定添加更多颜色,如果是这样,为什么您必须在原始代码中进行如此多的更改。