Skip to main content
Participant
March 24, 2011
Question

Need to remove a sprite object when another sprite object hits it

  • March 24, 2011
  • 1 reply
  • 716 views

Hello All,

I have a brick class where I have created a rectangle using flash.display.graphics.

I am using this brick class to create a grid of 10 X 10 in another class called grid using new brick() in a for loop.

I have another class called ball where I have created a circle with flash.display.graphics.

The problem is that I want to remove / destroy individual bricks when the ball hits the bricks on Event.ENTER_FRAME which is not happening.

the error I get is shown only for the last brick that is created in a for loop.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

at flash.display:isplayObjectContainer/removeChild()

at grid/ball_movement()// my function in enter frame event

The code I wrote in ENTER_FRAME is

if (Ball.hitTestObject(bricks)) {

removeChild(bricks);

}

where

var Ball:ball=new ball()

var bricks:brick=new brick()

any help is highly appreciated.

thanks

iceheros

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 24, 2011

if (Ball.hitTestObject(bricks)) {

if(bricks.stage){

bricks.parent.removeChild(bricks);

}

}


Participant
March 24, 2011

Thanks a lot Kglad for your help. But how can I check for the entire grid, this works but how to detect the hit for all the bricks in my 10X10 grid? It is currently removing only the last brick (element) =100 (10X10) when I form the grid.

Thanks Again

iceheros

kglad
Community Expert
Community Expert
March 24, 2011

loop through all your bricks and test for a hit.