如何为这段代码添加孩子?

时间:2017-06-11 13:43:44

标签: actionscript-3 flash

我一般不使用addchild。我发现代码和我试图在鼠标按下时创建一个圆圈,我尝试碰撞其他球。 所以我可以用鼠标创造球,但我没有碰到其他球。

import flash.geom.Point;

var NUMBER_OF_CIRCLES:Number = 2;
var NUMKEKKO:Number = 1;

var circle_array:Array;
var boru_array:Array;
var vector_array:Array;
var mass_array:Array;
var mouseuo:Boolean=false;

var the_kall:kall;      //Big ball ( make by player)
var mcKall:boru; 
var barz:circle;//
var drag:Boolean=false;
var mcKallCont:MovieClip;
    var kao:Number=10;
    var kao2:Number=10;
    var topisler:Number=2;

    var gonzo:Number=0;
    var topyap:Boolean=true;









//stage.addEventListener(Event.ENTER_FRAME, init); 
//function init(e:Event){
function init(){    

if(topyap==true){
    circle_array = new Array();
    vector_array = new Array();
    mass_array = new Array();
    boru_array = new Array();




    for(var i:Number = 0; i < NUMBER_OF_CIRCLES; i++){

        circle_array[i] = new circle();

        circle_array[i].width = 55;// circle_array[i].width = (Math.random() * 50);
        circle_array[i].height = circle_array[i].width;

        circle_array[i].x = (circle_array[i].width/2) + (Math.random() * (550 - circle_array[i].width*(3/2)));
        circle_array[i].y = (circle_array[i].height/2) + (Math.random() * (400 - circle_array[i].height*(3/2)));






        var new_color:uint = 0x000077 * Math.random();
        new_color += 0x000088;

        switch(i%3){
            case 0:
                new_color = new_color << 16;
                break;
            case 1:
                new_color = new_color << 8;
                break;
        }







        //new_color = new_color << 16;
        var myColor:ColorTransform = this.transform.colorTransform;
        myColor.color = new_color;
        circle_array[i].inner.transform.colorTransform = myColor;

        addChild(circle_array[i]);

        circle_array[i].id = i;

        vector_array[i] = new Array(Math.random() * 10, Math.random() * 10);
        mass_array[i] = circle_array[i].width * 0.10;
    }


}   


topyap=false;





















    stage.addEventListener(Event.ENTER_FRAME, circle_update); 








}





function circle_update(e:Event){

    for(var i:Number = 0; i < NUMBER_OF_CIRCLES; i++){
        circle_array[i].x += vector_array[i][0];
        circle_array[i].y += vector_array[i][1];

        //bounding wall collisions
        if(circle_array[i].x + (circle_array[i].width * 0.50) >= 550){ vector_array[circle_array[i].id][0] *= -1; circle_array[i].x = 550 - circle_array[i].width * 0.50; }
        if(circle_array[i].x - (circle_array[i].width * 0.50) <= 0){ vector_array[circle_array[i].id][0] *= -1;  circle_array[i].x = circle_array[i].width * 0.50; }

        if(circle_array[i].y + (circle_array[i].width * 0.50) >= 400){ vector_array[circle_array[i].id][1] *= -1; circle_array[i].y = 400 - circle_array[i].height * 0.50;  }
        if(circle_array[i].y - (circle_array[i].width * 0.50) <= 0){ vector_array[circle_array[i].id][1] *= -1; circle_array[i].y = circle_array[i].height * 0.50;   }


    }







    //ball to ball collisions
    for(var g:Number = 0; g < NUMBER_OF_CIRCLES; g++){
        var g_pos:Point = new Point(circle_array[g].x, circle_array[g].y)
        for(var k:Number = g + 1; k < NUMBER_OF_CIRCLES; k++){
            var dist = Point.distance(g_pos, new Point(circle_array[k].x, circle_array[k].y));
            if( dist < ((circle_array[g].width * 0.50) + (circle_array[k].width * 0.50)) ){
                //they're colliding!
                //elastic response
                var mass1:Number = mass_array[g];
                var mass2:Number = mass_array[k];
                var mass_sum = mass1 + mass2;
                var velX1:Number = vector_array[g][0];
                var velX2:Number = vector_array[k][0];
                var velY1:Number = vector_array[g][1];
                var velY2:Number = vector_array[k][1];

                var new_velX1 = (velX1 * (mass1 - mass2) + (2 * mass2 * velX2)) / mass_sum;
                var new_velX2 = (velX2 * (mass2 - mass1) + (2 * mass1 * velX1)) / mass_sum;
                var new_velY1 = (velY1 * (mass1 - mass2) + (2 * mass2 * velY2)) / mass_sum;
                var new_velY2 = (velY2 * (mass2 - mass1) + (2 * mass1 * velY1)) / mass_sum;

                vector_array[g][0] = new_velX1;
                vector_array[k][0] = new_velX2;
                vector_array[g][1] = new_velY1;
                vector_array[k][1] = new_velY2;

                circle_array[g].x += new_velX1;
                circle_array[g].y += new_velY1;
                circle_array[k].x += new_velX2;
                circle_array[k].y += new_velY2;
            }
        }



    }
    gonzo=dist;








    //Kall to ball collisions
    for(var s:Number = 0; s < NUMBER_OF_CIRCLES; s++){
        var t_pos:Point = new Point(the_kall.x, the_kall.y)
        for(var t:Number = s + 1; t < NUMBER_OF_CIRCLES; t++){
            var distf = Point.distance(t_pos, new Point(circle_array[t].x, circle_array[t].y));
            if( distf < ((the_kall.width * 0.50) + (circle_array[t].width * 0.50)) )
            {
                dmm.text = "touched";

            //elastic response
            kao +=0;
           kao2 +=0;

                drag=false;
                mouseuo=false;



            }
            }
            } 







}



init();




















































stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
function mousePressed(e:MouseEvent) {
    the_kall = new kall();
            the_kall.x=mouseX;
            the_kall.y=mouseY;

            addChild(the_kall) 
    drag=true;
    mouseuo=true;

    stage.addEventListener(MouseEvent.MOUSE_UP,mousebrk)



}












stage.addEventListener(Event.ENTER_FRAME,Update);

 function Update(e:Event) {


    if(drag==true){


            the_kall.width =kao;
            the_kall.height =kao2;
            kao ++;
           kao2 ++;

     }


     if(drag==false){

    removeChild(the_kall)

     }


      if(drag==false && mouseuo==true){


        mcKallCont = new MovieClip () ;
            stage.addChild(mcKallCont);

    mcKall = new boru ();
    mcKallCont.x =the_kall.x ;
    mcKallCont.y = the_kall.y;
    mcKall.width =the_kall.width;
    mcKall.height =the_kall.height;
    mcKallCont.addChild ( mcKall );
    mouseuo=false;



        NUMBER_OF_CIRCLES +=1;
        topisler +=1;

     removeChild(the_kall)

     }

}






















stage.addEventListener(MouseEvent.MOUSE_UP,mousebrk);
function mousebrk(e:MouseEvent) {

    drag=false;


    kao =1;
    kao2 =1;






    //for ( var p = 0; p < mcKallCont.numChildren; p++ ) 

    //var mcKall1:* = mcKallCont.getChildAt( p );




}









stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
function mouseMoved(e:MouseEvent) {
    if(drag==true){


    the_kall.x=mouseX;
    the_kall.y=mouseY;

                }
}

1 个答案:

答案 0 :(得分:2)

使用addChild,您只是将新创建的圆圈添加到舞台中,以便绘制它。您还需要将其耦合到处理球碰撞的逻辑。

需要更新变量NUMBER_OF_CIRCLES以表示新的圈数。您还需要将新圆圈(及其circle_update函数中所需的所有值)添加到circle_array数组,以便每帧更新圆圈的位置。此更新在circle_update函数中执行。因此,请使用以下代码替换您创建mcKall的代码部分:

if(drag==false && mouseuo==true){
   var i:Number = NUMBER_OF_CIRCLES;
   NUMBER_OF_CIRCLES+=1;

   circle_array[i] = new circle();
   circle_array[i].width = the_kall.width;
   circle_array[i].height = circle_array[i].width;
   circle_array[i].x = the_kall.x;
   circle_array[i].y = the_kall.y;

   var myColor:ColorTransform = this.transform.colorTransform;
   myColor.color = 0x000077 * Math.random() + 0x000088;
   circle_array[i].inner.transform.colorTransform = myColor;
   addChild(circle_array[i]);

   circle_array[i].id = i;
   vector_array[i] = new Array(0,0);
   mass_array[i] = circle_array[i].width * 0.10;

   removeChild(the_kall)
}

为了将来参考,请务必发布可读的代码。这包括格式化代码和命名变量,以便它有意义。像zamzam这样的变量名让其他人更难帮助你,因为他们更难理解你的代码。