as3无法同时创建相同的SimpleButton

时间:2014-03-22 01:39:50

标签: actionscript-3 flash simplebutton

我制作了一个拼图匹配游戏,操作选择三个3x3网格显示1~9,如果三个数字的总和与目标数目的目标相同(通常为6~24, 6 = 1 + 2 + 3,并且24 = 7 + 8 + 9),那么这三个数字将消失。当三个数字消失时,会出现三个新的随机数。

在我的LIBRARY中,我分别创建了9 SimpleButton名为btnO1~O9作为SimpleButton类,另外9名SimpleButton称为btnC1~C9作为另一个类。

我完成游戏的想法是: 在游戏的初始阶段,屏幕上会创建18 SimpleButton,半部分是btnO1~btnO9,另外半部分是btnC1~btnC9,我让btnC1~btnC9被隐藏。就像那样:

Initial of the game

当用户按下按钮时,它会变成绿色,让人们知道他们拿起号码,如下:

Pick up a number

然后只需再次点击即可取消您的选项。

如果拾取的三个数字可以与上面显示的目标相匹配,那么这三个数字将会消失,每次你的分数将增加500分:

Match is correct and three numbers disappear

到目前为止,我的游戏就像我想要的那样,但是,如果我尝试创建新的三个数字来发现三个消失的数字,我发现它将显示没有添加任何内容或只有一两个数字是加入......

在尝试导致问题的原因时,我发现如果数字相同SimpleButton,则无法创建。

例如,如果我在场景中创建了btnO1,那么如果我尝试创建另一个btnO1,即使我将不同的名称btnO1(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之前删除了事件。

到目前为止的游戏链接是:

The bubble of numbers

我会在感谢帮助我的游戏中添加介绍页面,提前感谢!

2 个答案:

答案 0 :(得分:0)

如果你在代码之前定义了newT Timer会发生什么?你说:newT.start()

答案 1 :(得分:0)

我再次安排我的代码,发现我误解了!

创建的按钮都是正确的,但它们是重叠的。

现在我解决了,谢谢你,Craig