我做了一个拼图游戏,操作是选择三个显示为1~9的3x3网格数字,如果三个数字的和与目标数字的目标相同(通常为6~24,6=1 +2+3, and 24=7+8+9),那么三个数字就会消失。当三个数字消失时,会出现三个新的随机数字。
在我的 LIBRARY 中,我分别创建了 9 个SimpleButton
名为 btnO1~O9 的SimpleButton
类,另外 9 个SimpleButton
名为 btnC1~C9 作为另一个类。
我实现我的游戏的想法是:游戏开始时,SimpleButton
屏幕中有18个被创建,一半是btnO1~btnO9,另一半是btnC1~btnC9,我让btnC1~btnC9隐藏。所以它是这样的:
当用户按下按钮时,它会变成绿色,让人们知道他们拿起了号码,如下所示:
然后只需再单击一次即可取消您的选项。
如果拾取的三个号码与上面显示的目标一致,则三个号码将消失,您的分数将每次增加 500 分:
到目前为止,我的游戏就像我想要的那样制作,但是,如果我尝试创建新的三个数字来代替三个消失的数字,我发现它要么不显示任何添加,要么只添加一个或两个数字。 ……
在尝试导致问题的原因时,我发现如果数字相同SimpleButton
,则无法创建。
例如,如果我在场景中创建了btnO1,那么如果我尝试创建另一个btnO1,即使我将btnO1(the SimpleButton
)的不同名称设置为actionscript(我不确定它是只出现在3还是2和3都出现),这将导致错误或异常不显示在调试窗口中。
有谁知道同时创建相同定制的问题是什么,SimpleButton
或者可以解决吗?
我的代码是:
var goal:int=6+Math.random()*19; // match number, 6~24 usually
txGoal.text=""+goal;
var numArr:Array=new Array(1,2,3,4,5,6,7,8,9); // original 1~9 number
var pickArr:Array=new Array(0,0,0); // array to find the position be clicked
var pickNum:Array=new Array(0,0,0); // array to find the number be clicked
var count:int=0; // index for above array
var sum:int=0; // check with goal
var score:int=0;
var canClick:Boolean=true; // can click only after anime stop
initNum(); // first round is 1~9, after first round will random
function initNum()
{
for(var i:int=0;i<numArr.length;i++)
{
origin(numArr[i],i);
clicked(numArr[i],i); // the trick for if number be clicked will show green one
}
}
var bub:int=0;
var bubbleT:Timer = new Timer(25);
var newW:int=0;
var newH:int=0;
// Timer to do animation of disappear
bubbleT.addEventListener(TimerEvent.TIMER, bubbleoutHandler);
function bubbleoutHandler(event:TimerEvent):void
{
bub++;
for(var i:int=0;i<pickArr.length;i++)
{
var nam:String=pickArr[i]+"_"+pickNum[i];
if(bub==1)
{
getChildByName("c"+nam).removeEventListener(MouseEvent.CLICK,cHandler); // remove the event of the three numbers
removeChild(getChildByName("c"+nam)); // remove the three numbers
getChildByName("o"+nam).visible=true; // show original number that do animation of disappear
newW=getChildByName("o"+nam).width;
newH=getChildByName("o"+nam).height;
getChildByName("o"+nam).width=newW*(0.8); // change bubble's size smaller at first
getChildByName("o"+nam).height=newH*(0.8);
}
else
{
var scale:Number=1+(bub*0.05);
getChildByName("o"+nam).width=newW*scale; // change bubble's size bigger gradually as bubble will blow up
getChildByName("o"+nam).height=newH*scale;
}
if(bub==4)
{
getChildByName("o"+nam).removeEventListener(MouseEvent.CLICK,oHandler); // remove event of origin numbers
removeChild(getChildByName("o"+nam)); // remove numbers as it disappeared
}
}
if(bub==4)
{
count=0;
sum=0;
bub=0;
bubbleT.stop(); // the three numbers disappeared
newT.start(); // try to new three numbers
}
}
var bnew:int=0;
// timer to do animation of create new numbers
var newT:Timer = new Timer(100);
newT.addEventListener(TimerEvent.TIMER, bubblenewHandler);
function bubblenewHandler(event:TimerEvent):void
{
bnew++;
for(var i:int=0;i<pickArr.length;i++)
{
var newnum:int=Math.random()*9+1; // random number from 1~9
var newpos=parseInt(pickArr[i]); // the new number should appear at original place
origin(newnum,newpos);
clicked(newnum,newpos);
}
// Originally do animation of adding new numbers, just change bubble size
//var nam:String=pickArr[i]+"_"+pickNum[i];
//var scale:Number=(bnew*0.2);
//getChildByName("o"+nam).width=newW*scale;
//getChildByName("o"+nam).height=newH*scale;
if(bnew>=2)
{
canClick=true;
bnew=0;
newT.stop();
}
}
// the function of create numbers( SimpleButton)
function origin(n:int,i:int)
{
var o:SimpleButton;
if(n==1)
o=new btnO1();
else if(n==2)
o=new btnO2();
else if(n==3)
o=new btnO3();
else if(n==4)
o=new btnO4();
else if(n==5)
o=new btnO5();
else if(n==6)
o=new btnO6();
else if(n==7)
o=new btnO7();
else if(n==8)
o=new btnO8();
else
o=new btnO9();
o.name="o"+i+"_"+n;
o.x=numX(n);
o.y=numY(n);
o.tabEnabled=false;
o.visible=true;
o.addEventListener(MouseEvent.CLICK,oHandler);
addChild(o);
}
// if press the number, the number turn to green, that means, origin number will hedden, and clicked number show up
function oHandler(e:Event):void{
if(canClick==true)
{
e.currentTarget.visible=false;
var nam:String=e.currentTarget.name.substr(1);
getChildByName("c"+nam).visible=true;
var val:int=parseInt(e.currentTarget.name.substr(3));
var pos:int=parseInt(e.currentTarget.name.substr(1,1));
// to record which three numbers be pressed
pickNum[count]=val;
// to record where the three numbers be pressed, then the new three numbers be created should take place the number in the same place.
pickArr[count]=pos;
count++;
sum+=val;
// if pressed three number that match the goal, do animation of disappear
if(count==3)
{
if(sum==goal)
{
score+=500;
txScore.text=""+score;
canClick=false;
bubbleT.start();
}
}
}
}
// function of create green numbers, show up when user click origin numbers
function clicked(n:int,i:int)
{
var c:SimpleButton;
if(n==1)
c=new btnC1();
else if(n==2)
c=new btnC2();
else if(n==3)
c=new btnC3();
else if(n==4)
c=new btnC4();
else if(n==5)
c=new btnC5();
else if(n==6)
c=new btnC6();
else if(n==7)
c=new btnC7();
else if(n==8)
c=new btnC8();
else
c=new btnC9();
c.x=numX(n);
c.y=numY(n);
c.name="c"+i+"_"+n;
c.tabEnabled=false;
c.visible=false;
c.addEventListener(MouseEvent.CLICK,cHandler);
addChild(c);
}
// if user click the green number( SimpleButton) again, then cancel their option
function cHandler(e:Event):void{
e.currentTarget.visible=false;
var nam:String=e.currentTarget.name.substr(1);
getChildByName("o"+nam).visible=true;
var num:int=parseInt(e.currentTarget.name.substr(1,1));
popout(num);
}
// pop out the numbers that user cancelled their option of the recording array
function popout(num:int)
{
for(var i in pickArr)
{
if(pickArr[i]==num)
{
sum-=pickNum[i];
pickNum[i]=0;
pickArr[i]=0;
count--;
break;
}
}
}
// fix position of numbers
function numX(n:int)
{
if(n==1||n==4||n==7)
return 109.3;
else if(n==2||n==5||n==8)
return 274.3;
else
return 440.3;
}
function numY(n:int)
{
if(n==1||n==2||n==3)
return 161;
else if(n==4||n==5||n==6)
return 245;
else
return 330;
}
stop();
代码在第一帧,所以我在最后添加了 stop(),在删除旧的SimpleSutton
.
到目前为止的游戏链接是:
我将添加介绍页面以感谢在游戏中帮助我的人,提前谢谢!