Reputation: 71
I am creating a platformer with as3 and need to see if children of the movieclip _boundaries are on the stage or not, that way I can remove them and lower the counter so that more will continually generate. So far all I have is below. Please help, been stuck on this for a couple of weeks.
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);
}
}
}
Upvotes: 0
Views: 270
Reputation: 5818
You may need to use the localToGlobal
method to determine the position of the Square Objects. Something like:
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);
}
}
}
On a side note for general best practice, reserve words starting with capital letters for class names (like MovieClip, Sprite, or MyCustomClass) and use camelCase for variable names. It's helpful when working with other devs to promote best practice.
Hope this helps.
Upvotes: 1
Reputation: 6742
Try this:
//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);
}
}
Upvotes: 0