我试图阻止球的开槽并且当球击到另一个球时掉到舞台的底部。但是我不能。 我尝试了设定距离,但没有奏效。
我添加了游戏图片
我的代码
敌人球(绿色)相互接触很好。但不要用鼠标触摸圆圈。球触摸黑球但没有看到触摸按摩。只有触摸鼠标箭头才会出现触摸信息。
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import Ball;
import Tekno;
var nSpeedX:Number;
var nSpeedY:Number;
var nStageWidth:Number = 500;
var nStageHeight:Number = 400;
var NSpeedX:Number;
var NSpeedY:Number;
var the_kall:kall; //Big ball ( make by player)
var mcKall:boru; //Red ball
var drawing:Boolean=false;
var nNumBalls:Number = 2;
var initX:Number=0.0;
var initY:Number=0.0;
var topisler:Number=0;
var pressed:Boolean=false;
var MCBallA:MovieClip;
var MCBallB:MovieClip;
var zamzam:Boolean=false;
var kao:Number=10;
var kao2:Number=10;
var mcBallContainer:MovieClip;
var mcfakContainer:MovieClip;
var mcKallCont:MovieClip;
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
function mousePressed(e:MouseEvent) {
zamzam=true;
}
//********* Create red ball if mouse press up
stage.addEventListener(MouseEvent.MOUSE_UP,mousebrk);
function mousebrk(e:MouseEvent) {
zamzam=false;
topisler +=1;
kao =1;
kao2 =1;
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 );
//for ( var p = 0; p < mcKallCont.numChildren; p++ )
//var mcKall1:* = mcKallCont.getChildAt( p );
}
//********* Create big ball
stage.addEventListener(Event.ENTER_FRAME,Update);
function Update(e:Event) {
if(zamzam==true){ //********check if mouse pressed
the_kall = new kall();
the_kall.x=mouseX;
the_kall.y=mouseY;
the_kall.width =kao;
the_kall.height =kao2;
addChild(the_kall);
kao +=1;
kao2 +=1;
///////////
}
}
//**************Drag
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
function mouseMoved(e:MouseEvent) {
if (drawing) {
the_kall.x=mouseX;
the_kall.y=mouseY;
}
}
//************Add black balls and check collision
stage.addEventListener(Event.ENTER_FRAME,BallCollision);
function BallCollision(oly:Event) {
mcBallContainer = new MovieClip ( ) ;
mcBallContainer.x = mcBallContainer.y = 0;
stage.addChild(mcBallContainer);
this.addEventListener ( Event.ENTER_FRAME, enterFrameHandler );
for ( var i = 0; i < nNumBalls; i++ )
{
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 12 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
mcBall.x = Math.random() * nStageWidth;
mcBall.y = Math.random() * nStageHeight;
mcBallContainer.addChild ( mcBall );
}
stage.removeEventListener(Event.ENTER_FRAME,BallCollision);
}
function enterFrameHandler(oly:Event) {
for ( var i = 0; i < mcBallContainer.numChildren; i++ )
{
var mcBall1:* = mcBallContainer.getChildAt( i );
for ( var j = i + 1; j < mcBallContainer.numChildren; j++ )
{
var mcBall2:* = mcBallContainer.getChildAt( j );
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
//***********I Tried big ball vs black ball
var sDistx:Number = Math.abs ( mcBall1.x - the_kall.x );
var sDisty:Number = Math.abs ( mcBall1.y - the_kall.y );
var nsistance:Number = Math.sqrt ( sDistx * sDistx + sDisty * sDisty );
if ( nsistance < 20 )
{
touch.text="balls are touch";
}
if ( nDistance < 20 )
{
solveBalls ( mcBall1, mcBall2 ); //*****if black balls touch each other
}
}
}
onda.text=String(nsistance);
}
function solveBalls ( MCBallA:MovieClip, MCBallB:MovieClip ) : void
{
var nX1:Number = MCBallA.x;
var nY1:Number = MCBallA.y;
var nDistX:Number = MCBallB.x - nX1;
var nDistY:Number = MCBallB.y - nY1;
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
var nRadiusA:Number = MCBallA.width/2;
var nRadiusB:Number = MCBallB.width/2;
//var nRadius:Number = 10;
var nNormalX:Number = nDistX/nDistance;
var nNormalY:Number = nDistY/nDistance;
var nMidpointX:Number = ( nX1 + MCBallB.x )/2;
var nMidpointY:Number = ( nY1 + MCBallB.y )/2;
MCBallA.x = nMidpointX - nNormalX * nRadiusA;
MCBallA.y = nMidpointY - nNormalY * nRadiusA;
MCBallB.x = nMidpointX + nNormalX * nRadiusB;
MCBallB.y = nMidpointY + nNormalY * nRadiusB;
var nVector:Number = ( ( MCBallA.nSpeedX - MCBallB.nSpeedX ) * nNormalX )+ ( ( MCBallA.nSpeedY - MCBallB.nSpeedY ) * nNormalY );
var nVelX:Number = nVector * nNormalX;
var nVelY:Number = nVector * nNormalY;
MCBallA.nSpeedX -= nVelX;
MCBallA.nSpeedY -= nVelY;
MCBallB.nSpeedX += nVelX;
MCBallB.nSpeedY += nVelY;
}
//just test blue ball
/* stage.addEventListener(Event.ENTER_FRAME,test);
function test(oly:Event) {
if(the_kall.hitTestObject(keko)){
zacma.text="llj";
}
}*/
stop();