0

我正在使用 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);
        }
    }
}
4

2 回答 2

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 作为变量名。在与其他开发人员合作推广最佳实践时,这很有帮助。

希望这可以帮助。

于 2012-05-29T22:36:38.060 回答
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);
    }
}
于 2012-05-29T18:10:39.203 回答