Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从随机数组中删除子数组?_Arrays_Actionscript 3_Flash_Random_Actionscript - Fatal编程技术网

Arrays 如何从随机数组中删除子数组?

Arrays 如何从随机数组中删除子数组?,arrays,actionscript-3,flash,random,actionscript,Arrays,Actionscript 3,Flash,Random,Actionscript,我有一个随机创建10个点的数组。但是,我不希望在某个特定区域创建它们。我怎样才能做到这一点?我的代码给了我错误提示 “提供的DisplayObject必须是调用方的子对象。” 它偶尔会按照指示输出totalDots,(trace”“+totalDots),但90%的时间它会告诉我错误 public var numDots:Array = []; public var totalDots:int = numDots.length; public var box:Box = new Box();

我有一个随机创建10个点的数组。但是,我不希望在某个特定区域创建它们。我怎样才能做到这一点?我的代码给了我错误提示

“提供的DisplayObject必须是调用方的子对象。”

它偶尔会按照指示输出totalDots,
(trace”“+totalDots)
,但90%的时间它会告诉我错误

public var numDots:Array = [];
public var totalDots:int = numDots.length;
public var box:Box = new Box();

public function addBox():void
{
     box.x = stageWidth/2;
     box.y = stageHeight/2;
     addChild(box);
}
private function addDot():void
{
    for(var i:int = 0; i < 10; i++)
    {
        var dot:Dot = new Dot();
        dot.x = Math.floor(Math.random() * stageWidth);
        dot.y = Math.floor(Math.random() * stageHeight);
        this.addChild(dot);
        totalDots++;
        trace(""+totalDots);

        for(var j:int = 0; j < totalDots; j++)
        {
            if(numDots[j].hitTestObject(box))
            {
                stage.removeChild(numDots[j]);
                numDots.splice(j, 1);
                totalDots--;
            }
        }
    }

}
公共变量numDots:Array=[]; 公共变量totalDots:int=numDots.length; 公共变量框:框=新框(); 公共函数addBox():void { 方框x=分段宽度/2; 方框y=舞台高度/2; addChild(box); } 私有函数addDot():void { 对于(变量i:int=0;i<10;i++) { 变量点:点=新点(); 点x=Math.floor(Math.random()*stageWidth); dot.y=Math.floor(Math.random()*stageHeight); 这个.addChild(dot); totalDots++; 痕迹(“+”点); 对于(var j:int=0;j您的问题在于嵌套循环。在每次迭代中,添加一个新点,然后在所有现有点上循环,如果与长方体碰撞,则将其删除。我不认为那是你打算做的

看起来您只是想确保这些点没有添加到某个区域内。在这种情况下,使用do while循环保持简单:

for(var i:int = 0; i < 10; i++)
{
    var dot:Dot = new Dot();
    this.addChild(dot);
    do {
        dot.x = Math.floor(Math.random() * stageWidth);
        dot.y = Math.floor(Math.random() * stageHeight);
    } while(dot.hitTestObject(box))
    totalDots++;
    trace(""+totalDots);
}
for(变量i:int=0;i<10;i++)
{
变量点:点=新点();
这个.addChild(dot);
做{
点x=Math.floor(Math.random()*stageWidth);
dot.y=Math.floor(Math.random()*stageHeight);
}while(dot.hitTestObject(框))
totalDots++;
痕迹(“+”点);
}

您的问题在于嵌套循环。在每次迭代中,添加一个新点,然后在所有现有点上循环,如果与长方体碰撞,则将其删除。我不认为那是你打算做的

看起来您只是想确保这些点没有添加到某个区域内。在这种情况下,使用do while循环保持简单:

for(var i:int = 0; i < 10; i++)
{
    var dot:Dot = new Dot();
    this.addChild(dot);
    do {
        dot.x = Math.floor(Math.random() * stageWidth);
        dot.y = Math.floor(Math.random() * stageHeight);
    } while(dot.hitTestObject(box))
    totalDots++;
    trace(""+totalDots);
}
for(变量i:int=0;i<10;i++)
{
变量点:点=新点();
这个.addChild(dot);
做{
点x=Math.floor(Math.random()*stageWidth);
dot.y=Math.floor(Math.random()*stageHeight);
}while(dot.hitTestObject(框))
totalDots++;
痕迹(“+”点);
}

您从不在数组中添加任何点

将点添加到显示列表中,如下所示:

this.addChild(dot);
stage.removeChild(numDots[j]);
然后你试着这样移除它:

this.addChild(dot);
stage.removeChild(numDots[j]);
尽管该点从未添加到数组中,但即使它已添加到数组中,也无法工作。这是因为
这个
不是
阶段
。它们是两种不同的东西


您应该永远不要使用
stage.addChild()
(有关这方面的更多信息,请查看文档)。只要一直调用
addChild()
,这相当于
this.addChild()
。这样可以确保始终在同一个
DisplayObjectContainer

上操作,而不会向数组中添加任何点

将点添加到显示列表中,如下所示:

this.addChild(dot);
stage.removeChild(numDots[j]);
然后你试着这样移除它:

this.addChild(dot);
stage.removeChild(numDots[j]);
尽管该点从未添加到数组中,但即使它已添加到数组中,也无法工作。这是因为
这个
不是
阶段
。它们是两种不同的东西


您应该永远不要使用
stage.addChild()
(有关这方面的更多信息,请查看文档)。只要一直调用
addChild()
,这相当于
this.addChild()
。这确保了您始终对同一个
DisplayObjectContainer

进行操作。为了避免尝试循环,您可以使用适当的间隔(包含和排除区域之间的差异)计算一个随机值,并由此导出
x
y
坐标

下面的代码(用一种我不知道的语言编写,如果语法有错误,请道歉)有两种情况。
if
案例处理点将出现在排除框左侧或右侧的情况,并且
x
值的范围限制在该框的左侧或右侧。
else
情况是点将出现在框的上方或下方,并且
x
值不受限制

var interval:int = stageWidth * stageHeight - box.w * box.h;
var cut:int = interval - (stageWidth - box.w) * box.h;
for (var i:int = 0; i < 10; i++) {
    var r:int = Math.floor(Math.random() * interval);
    var x:int;
    var y:int;
    if (r >= cut) {
        r -= cut;
        y = r / (stageWidth - box.w);
        x = r - y * (stageWidth - box.w);
        y += box.y;
        if (x >= box.x) x += box.w;
    } else {
        y = r / stageWidth;
        x = r - y * stageWidth;
        if (y >= box.y) y += box.h;
    }
    var dot:Dot = new Dot();
    dot.x = x;
    dot.y = y;
    this.addChild(dot);
    totalDots++;
    trace(""+totalDots);
}
var区间:int=stageWidth*stageHeight-box.w*box.h;
var cut:int=间隔-(分段宽度-方框.w)*方框.h;
对于(变量i:int=0;i<10;i++){
var r:int=Math.floor(Math.random()*interval);
var x:int;
变量y:int;
如果(r>=切割){
r-=切割;
y=r/(分段宽度-方框w);
x=r-y*(分段宽度-box.w);
y+=box.y;
如果(x>=box.x)x+=box.w;
}否则{
y=r/分段宽度;
x=r-y*分段宽度;
如果(y>=box.y)y+=box.h;
}
变量点:点=新点();
点x=x;
点y=y;
这个.addChild(dot);
totalDots++;
痕迹(“+”点);
}
这假设
box
完全位于
stageWidth、stageHeight

同样值得注意的是,它允许点重叠;与原始代码相同


对于更复杂的形状,您可以将
box
设置为该形状完全包围的最大矩形,以避免许多但不是所有的重试情况。这对较大的形状和许多点很有帮助。或者,也有一些变体可能会与另一个形状(例如椭圆)完美匹配。

对于它的价值,您可以通过计算具有适当间隔的随机值(包含区域和排除区域之间的差异)并从中导出
x
y
坐标,从而完全避免尝试循环

以下代码(用I语言编写)