Reputation: 111
I'm generally not use addchild. I found that code and I tried to create a circle when mouse pressed and, I tried collision other balls. So I can create ball by the mouse but I didn't collide with other balls.
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;
}
}
Upvotes: 0
Views: 83
Reputation: 4078
With addChild
you are only adding the newly created circle to the stage so that it will be drawn. You will also need to couple it to the logic that handles the ball collisions.
The variable NUMBER_OF_CIRCLES
will need to be updated to represent the new number of circles. You will also need to add the new circle (and all its values needed in the circle_update
function) to the circle_array
array so that the circle's position will be updated each frame. This update is executed in the circle_update
function. Thus, replace the part of your code where you create mcKall
with this:
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)
}
For future reference, please make sure to post code that is readable. That includes formatting the code and naming your variables so that it makes sense. Variable names like zamzam
make it harder for other people to help you, because it's harder for them to understand your code.
Upvotes: 2