【问题标题】:AS3: Is it possible to add child in a specific area on the screen which is not a rectangle?AS3:是否可以在屏幕上的特定区域添加子元素,而不是矩形?
【发布时间】:2013-04-16 15:46:05
【问题描述】:

我需要在特定区域添加许多孩子 (addChild())。该区域的形状不规则(图)。我注意到,如果我想在我的图形中添加许多孩子,Flash 会创建代表我的图形的矩形,而我的一些孩子会离开图形但进入这个矩形。我想到了制作许多小矩形来覆盖我的非规则图形并使用数组将所有孩子分布到这些小矩形中。这是正确的方法吗? 我会欣赏一些想法。 谢谢

//--------------------------------------------- -------------------------------------------

                function randomRange(max:Number, min:Number = 0):Number
    {
        return Math.random() * (max - min) + min;
    }

    public function Main()
    {
        var bounds:Rectangle = Area_mc.getBounds(Area_mc.stage);
        var xIncr:int = randomRange(15,320);
        var yIncr:int = randomRange(15,220);
        for (var xPos=bounds.x; xPos <= bounds.x + bounds.width; xPos += xIncr)
        {
            for (var yPos=bounds.y; yPos <= bounds.y + bounds.height; yPos += yIncr)
            {
                var isInsideShape:Boolean = Area_mc.hitTestPoint(xPos,yPos,true);
                if (isInsideShape)
                {
                    //trace(isInsideShape);
                    stage.addChild(_symbol);
                    _symbol.x = xPos;
                    _symbol.y = yPos;
                }

            }
        }
    }    

好的,我有随机的 X 和 Y,但孩子总是在容器的右侧!:)

【问题讨论】:

  • 我认为您需要检查位图(像素)级别的碰撞检测。

标签: actionscript-3 shape addchild


【解决方案1】:

我无法理解您的确切要求。据我了解,如果您在非矩形的影片剪辑上添加子项,您可以使用影片剪辑的 hitTestPoint() 函数。

例如,如果您有“子”影片剪辑,您打算将其添加到非矩形“父”影片剪辑上,您可以使用 hitTestPoint 检查一个点是否在父影片剪辑的形状内,然后将其添加到该影片剪辑上观点。

下面的代码将添加 'Child' 类的实例,它在 'parentMovieClip' 上扩展了影片剪辑。 'Child' 是库中影片剪辑的链接名称,您需要将其实例添加到非矩形父级上。 'parentMovieClip' 是舞台上的影片剪辑的实例名称。

//storing bounds of parent that is added on stage
var bounds:Rectangle = parentMovieClip.getBounds(parentMovieClip.stage);

//these are the x and y gap you need between each child
var xIncr:int = 5;
var yIncr:int = 5;

//Traverse through the rectangular bound, and check what points actually comes within the shape
for(var xPos=bounds.x; xPos <= bounds.x+bounds.width; xPos += xIncr)
{
    for(var yPos=bounds.y; yPos <= bounds.y+bounds.height; yPos += yIncr)
    {
            //check if the point is inside the parent's shape
        var isInsideShape:Boolean = parentMovieClip.hitTestPoint(xPos,yPos,true);
        if(isInsideShape)
        {
            //if point is indise the shape add an instance of 'Child'
            var oChild:Child = new Child();
            //we are adding oChild on stage 
            //since adding on parentMovieClip will increase its bound if oChild goes outside parentMovieClip 
            stage.addChild(oChild);
            oChild.x = xPos;
            oChild.y = yPos;
        }
    }
}

如果您通过提供一些代码示例详细说明您的要求,我也许可以为您提供确切的解决方案。

【讨论】:

  • 非常感谢你。我“翻译”了这段代码 :) 是的,我明白了你的意思。我对此有点担心:“var bounds:Rectangle = parentMovieClip.getBounds(parentMovieClip.stage);”这还是矩形??
  • 我的意思是你的循环检查洞察矩形的坐标吗?还是 getBounds 命令恰好取了容器的边界(我将父影片剪辑称为容器)?谢谢
  • “hitTestPoint”不是使用矩形边界吗?
  • 是的 getBounds() 会给你一个矩形,但我在这里做的是,我遍历该矩形中的所有点,并使用 hitTestPoint 检查哪些点实际上位于非矩形形状内()。如果点在它里面,我们添加孩子。
  • 还有一点,hitTestPoint() 不使用矩形边界,它只是将一个点 (x,y) 作为输入并检查该点是否位于该影片剪辑的命中区域内。非矩形影片剪辑的矩形边界中的任何空白空间都会将 hitTestPoint 设为 false。考虑到这一点,再次查看代码,希望你能明白我的意思
【解决方案2】:

不,这很难。

更好

1) 将区域定义为多边形。

2) 通过一种算法检查要添加的新对象的边界,该算法可以 检测一个点是否在多边形内。

Here is a good algorithm in C, which is short, brief, easy to convert to as3 and educative.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多