0

In my cocos2d-android game project, random number of targets falls from top surface on to the ship, when both intersect ship should get deleted, i have done the coding but "ship" is not getting deleted. is that CGRect sprite will not get deleted? does anybody know this?

LinkedList<CCSprite> targetsToDelete = new LinkedList<CCSprite>();

for (CCSprite target : _targets)
{
    CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                    target.getPosition().y - (target.getContentSize().height),
                                    target.getContentSize().width,
                                    target.getContentSize().height);



    CCSprite ship = CCSprite.sprite("ship150.png");
    ship.setPosition(CGPoint.ccp(30,200));
    ship.setAnchorPoint(CGPoint.ccp(0,0));
    ship.setTag(25);
    addChild(ship);
    //  ship.setVisible(false);


    CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width/2),
                                  ship.getPosition().y - (ship.getContentSize().height/2),
                                  ship.getContentSize().width,
                                  ship.getContentSize().height);
    System.out.println("ships to delete continue... : " + volume);
    if (CGRect.intersects(targetRect, shipRect))
    {

        System.out.println("ships intersected:)@@@@@@@@@@@@@@@@@@@@@@@@@@@@@... : " + volume);
        removeChildByTag(25, false);

    }

}
Kreiri
  • 7,840
  • 5
  • 30
  • 36
DD.
  • 973
  • 2
  • 10
  • 32
  • is your adding brand new `ship` for every target in _targets intended? – Kreiri Jun 25 '13 at 11:16
  • thanks for your reply, there is only one ship and many targets @Kreiri – DD. Jun 25 '13 at 11:19
  • is that something like parent.removeChild(node, true); has to be given? @Kreiri – DD. Jun 25 '13 at 11:24
  • I'm talking about lines `CCSprite ship = addChild(ship);`. For every target in _targets you create a "ship150.png" sprite and add it to layer (I assume your code is called in update loop of a layer). Is this intended? Did you really want such behaviour when you wrote this code? – Kreiri Jun 25 '13 at 11:39
  • yes my code is in update loop, since in update method _targets are used so placed this code in this, for every target in _targets i dont want to create a "ship" sprite, only one is enough. @Kreiri – DD. Jun 25 '13 at 11:49
  • are you by any chance setting the position of your parent node? – Parvaz Bhaskar Jun 27 '13 at 03:25
  • should i need to delete the parent node? i found like this, public void removeMe(CocosNode node) { parent.removeChild(node, true); is this correct? @ParvazBhaskar – DD. Jun 27 '13 at 04:54
  • no you don't have to remove the parent node, I asked just because if you have changed the position of your node then you'll have to add the offset of node from origin to the rectangle positions that you're giving. – Parvaz Bhaskar Jun 28 '13 at 04:43
  • i'm not able to get wat ur telling, please can u be more clearer? @ParvazBhaskar – DD. Jun 28 '13 at 07:56
  • is the log printed when the target intersects ship? Is your "If" condition for checking intersections is being met? – Parvaz Bhaskar Jun 28 '13 at 12:29
  • yes i'm able get the log message, ship is getting intersected, but not getting removed @ParvazBhaskar – DD. Jun 28 '13 at 12:37
  • hi Please provide feedback for the answer. – Parvaz Bhaskar Jul 30 '13 at 04:36

1 Answers1

0

Your code is in update loop and this way you are adding ship to your loop every time.Its actually deleting you might not be seeing the effect. Take out the code that adds your ship out of your update code and put it in the initialization code,make the ship sprite global and then check for intersection.

Parvaz Bhaskar
  • 1,367
  • 9
  • 29