看看舞台上是否有movieclip的孩子

时间:2012-05-29 16:42:57

标签: actionscript-3

我正在使用as3创建一个平台游戏,并且需要查看movieclip _boundaries的子节点是否在舞台上,这样我就可以删除它们并降低计数器,以便更多地继续生成。到目前为止我所有的都在下面。请帮帮忙,坚持了几个星期。

var ObjectArray:Array = [];
var ChildrenColliding:Boolean = false;
var onStageCount:Number = 0;
function generateObjects():void{    
    if(_vx > 0 && onStageCount < 20){
        var Square:MovieClip;
        Square = new mcSquare();
        Square.x = Math.random() * 1000 + (Math.abs(_boundaries.x) + 50);
        Square.y = Math.random() * stage.stageHeight/2.5 + (stage.stageHeight/2.5);
        ObjectArray.push(Square);
        _boundaries.addChild(Square);
        onStageCount += 1;
    }
    for(var i in ObjectArray){
        Square[i] = Square.name;
        for(var a in ObjectArray){
            if(ObjectArray[i].hitTestObject(ObjectArray[a]) && a != i){ChildrenColliding = true;}
            while(ChildrenColliding){
            ObjectArray[i].x += (ObjectArray[a].height + 25);
            ObjectArray[i].y += (ObjectArray[a].width + 25);
            ChildrenColliding = false;
                if(ObjectArray[a].hitTestObject(ObjectArray[i]) && a != i){ChildrenColliding = true;}
            }
        }
    }
    //CHECK TO SEE IF CHILDREN ARE ON STAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    for(var w in ObjectArray){
        if(_boundaries){
            onStageCount -= 1;
            trace("removed");
            _boundaries.removeChild(ObjectArray[w]);
            ObjectArray.splice(w, 1);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可能需要使用localToGlobal方法来确定方形对象的位置。类似的东西:

for (var w in ObjectArray) {
    if (_boundaries) {
        var sq:MovieClip = ObjectArray[w];
        var pnt:Point = _boundaries.localToGlobal(new Point(sq.x, sq.y));
        if (pnt.x <= 0 || pnt.x >= _boundaries.stage.stageWidth || 
            pnt.y <= 0 || pnt.y >= _boundaries.stage.stageHeight) {

            // remove square
            onStageCount -= 1;
            trace("removed");
            _boundaries.removeChild(ObjectArray[w]);
            ObjectArray.splice(w, 1);
        }
    }
} 

在一般性最佳实践的旁注中,保留以类的大写字母开头的单词(如MovieClip,Sprite或MyCustomClass),并使用camelCase作为变量名。与其他开发人员合作推广最佳实践时,这很有帮助。

希望这有帮助。

答案 1 :(得分:0)

试试这个:

//CHECK TO SEE IF CHILDREN ARE ON STAGE!!!!!!!!!!
for(var w in ObjectArray){
    if(_boundaries && _boundaries.contains(ObjectArray[w])){
        onStageCount -= 1;
        trace("removed");
        _boundaries.removeChild(ObjectArray[w]);
        ObjectArray.splice(w, 1);
    }
}
相关问题