碰撞测试与舞台上的多个对象多个、对象、测试、舞台上

2023-09-08 15:17:47 作者:素手执白扇

我想创建这样的,如果你点击某个圈子中一种树形图中,其子圆小号$ P $垫向外由原来的圆形一定程度的随机性,用线连接起来。我有这个工作,但现在我想要做的是确保没有圆的相互碰撞,并没有一行的十字交叉。你可以看到屏幕截图什么是目前发生的事情。我的code是如下。我要如何改变这种code,以便它检查碰撞和避免它们吗?我已经在闪光灯的hitTestObject命令读了,但是这只能在上下文一个对象到另一个。我想测试一个物体接触任何显示对象。

 进口com.greensock.TweenMax;

变种SW = stage.stageWidth;
VAR SH = stage.stageHeight;
VAR CR = 3; //圆的半径
VAR moveRange = 25;
VAR circleColor = 0xcccccc;
变种numCircles = 4;
VAR lineCanvas:雪碧=新的Sprite();
的addChild(lineCanvas);
VAR lineColor = 0xe9e9e9;
VAR线宽= 1;

功能的init(){
    firstCircle();
}

功能firstCircle(){
    VAR XPOS = randomRange(CR,SW-CR);
    VAR yPos = randomRange(CR,SH-CR);
    VAR newCircle:形状=新形状();
    newCircle.graphics.beginFill(circleColor);
    newCircle.graphics.drawCircle(0,0,CR);
    newCircle.graphics.endFill();

    VAR circleClip:影片剪辑=新的MovieClip();
    circleClip.childCircles = 2;
    circleClip.x = XPOS;
    circleClip.y = yPos;
    circleClip.addChild(newCircle);

    的addChild(circleClip);
    circleClip.addEventListener(MouseEvent.CLICK,clickCircle);
}

功能clickCircle(E:的MouseEvent):无效{
    VAR thisCircle = e.target;
    对于(VAR I = 0; I< thisCircle.childCircles;我++){
        画圆(thisCircle);
    }
}

功能画圆(parentCircle){
    VAR XPOS = parentCircle.x;
    VAR yPos = parentCircle.y
    // VAR XPOS = randomRange(CR,SW-CR);
    // VAR yPos = randomRange(CR,SH-CR);
    VAR newCircle:形状=新形状();
    newCircle.graphics.beginFill(circleColor);
    newCircle.graphics.drawCircle(0,0,CR);
    newCircle.graphics.endFill();

    VAR circleClip:影片剪辑=新的MovieClip();
    circleClip.childCircles = 2;
    circleClip.x = XPOS;
    circleClip.y = yPos;
    circleClip.addChild(newCircle);
    的addChild(circleClip);
    circleClip.addEventListener(MouseEvent.CLICK,clickCircle);
    moveCircle(circleClip,XPOS,yPos);
}

功能的drawLine(childCircle,parentX,帕朗蒂){
        lineCanvas.graphics.lineStyle(线宽,lineColor);
        lineCanvas.graphics.moveTo(parentX,帕朗蒂);
        lineCanvas.graphics.lineTo(childCircle.x,childCircle.y);
 

//要检查如果任一线路或圆在这里接触任何东西。如果是,我要杀死补间转了一圈(因此也停止生产线的绘制)。

 }

功能moveCircle(childCircle,parentX,帕朗蒂){
    VAR CURX = childCircle.x;
    VAR CURY = childCircle.y;
    VAR的Movex = randomRange(CURX-moveRange,CURX + moveRange);
    VAR moveY = randomRange(CURY-moveRange铬,CURY + moveRange + CR);
    TweenMax.to(childCircle,5,{X:MOVEX,Y:moveY,OnUpdate中:的drawLine,onUpdateParams:[childCircle,parentX,帕朗蒂]});
}

功能randomRange(minNum:编号,MAXNUM:号码):数{
    返回(Math.floor(的Math.random()*(MAXNUM  -  minNum + 1))+ minNum);
}

在里面();
 

解决方案

有2种方式,你可以这样做:

重新因素的code到使用ActionScript物理库(比如Box2D的或任何你preFER)。然后,请考虑您的圈子和行,因为他们会相互碰撞,很可能有同样的效果,你想在这里做什么物体。临去,这是该库自带的扩展类的分类来处理物理相互作用。 con是在执行的开销。 在手动/自定义的方式来做到这一点是通过它的孩子在每个孩子圈添加一个事件监听器为ENTER_FRAME,在舞台上或初始圈,这将循环,并调用hitTestObject方法,每隔比较孩子圈。该行是孩子们的圈子之内,所以应该没事。

我注意到你没有实际添加childCircles最初的圈子,而不是将其添加到舞台上。您可能希望这些圈子推入一个数组以后引用这个collisionDetection的方法。

软件测试包括哪些步骤,这些步骤的测试对象是什么

I'm trying to create a sort of tree diagram such that, if you click on one of the circles, its child circles spread outward with some degree of randomness from the original circle, connected by lines. I have this working, but now what I want to do is make sure that none of the circles collide with each other and none of the lines criss cross. You can see the screenshot for what is currently happening. My code is below. How do I change this code so that it checks for collisions and avoids them? I've read up on flash's hitTestObject command, but that only works in the context one object to another. I want to test for one object contacting any display object.

import com.greensock.TweenMax;

var sw = stage.stageWidth;
var sh = stage.stageHeight;
var cr = 3; //circle radius
var moveRange = 25;
var circleColor = 0xcccccc;
var numCircles = 4;
var lineCanvas:Sprite = new Sprite();
addChild(lineCanvas);
var lineColor = 0xe9e9e9;
var lineWeight = 1;

function init(){
    firstCircle();
}

function firstCircle(){
    var xPos = randomRange(cr, sw-cr);
    var yPos = randomRange(cr, sh-cr);
    var newCircle:Shape = new Shape();
    newCircle.graphics.beginFill(circleColor);
    newCircle.graphics.drawCircle(0,0,cr);
    newCircle.graphics.endFill();

    var circleClip:MovieClip = new MovieClip();
    circleClip.childCircles = 2;
    circleClip.x = xPos;
    circleClip.y = yPos;
    circleClip.addChild(newCircle);

    addChild(circleClip);
    circleClip.addEventListener(MouseEvent.CLICK,clickCircle);
}

function clickCircle(e:MouseEvent):void {
    var thisCircle = e.target;
    for (var i=0; i<thisCircle.childCircles;i++){
        drawCircle(thisCircle);
    }
}

function drawCircle(parentCircle){
    var xPos = parentCircle.x;
    var yPos = parentCircle.y
    //var xPos = randomRange(cr, sw-cr);
    //var yPos = randomRange(cr, sh-cr);
    var newCircle:Shape = new Shape();
    newCircle.graphics.beginFill(circleColor);
    newCircle.graphics.drawCircle(0,0,cr);
    newCircle.graphics.endFill();

    var circleClip:MovieClip = new MovieClip();
    circleClip.childCircles = 2;
    circleClip.x = xPos;
    circleClip.y = yPos;
    circleClip.addChild(newCircle);
    addChild(circleClip);
    circleClip.addEventListener(MouseEvent.CLICK,clickCircle);
    moveCircle(circleClip,xPos,yPos);
}

function drawLine(childCircle,parentX,parentY){
        lineCanvas.graphics.lineStyle(lineWeight,lineColor);
        lineCanvas.graphics.moveTo(parentX,parentY);
        lineCanvas.graphics.lineTo(childCircle.x,childCircle.y);

//Want to check if either the line or the circle is contacting anything here. If it is, I want to kill the tween to the circle (thus also stopping the drawing of the line).

}

function moveCircle(childCircle,parentX,parentY){
    var curX = childCircle.x;
    var curY = childCircle.y;
    var moveX = randomRange(curX-moveRange,curX+moveRange);
    var moveY = randomRange(curY-moveRange-cr,curY+moveRange+cr);
    TweenMax.to(childCircle,.5, { x: moveX, y: moveY, onUpdate:drawLine, onUpdateParams:[childCircle,parentX,parentY]});
}

function randomRange(minNum:Number, maxNum:Number):Number {  
    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);  
}

init();

解决方案

There's 2 ways you could do this:

Re-factor your code to use an Actionscript Physics Library (i.e. Box2D or whichever you prefer). Then, consider your "circles" and "lines" as physical objects that will collide with each other, probably having the same effect as what you're trying to do here. The PRO to this is that the library comes with an assortment of extended classes to handle physical interaction. The CON is the overhead in implementation. The manual/custom way to do this is to add an eventlistener for ENTER_FRAME, on the stage OR the initial circle, that will loop through it's children, and call the hitTestObject method on each "child" circle, compared with every other "child" circle. The lines are within the children circles, so should be fine.

I notice you're not actually adding the childCircles to the initial circle, and instead adding them to the stage. You might want to push these circles into an Array to reference later for this "collisionDetection" method.