Domanda

I am using SpriteBuilder to make a game. The objective is to destroy some CCSprites. I have 3 sprites on screen and are destroyed by another sprite, so the code must have something to do with when there are no more 'enemy' sprites remaining a next button must show. I have looked on the internet and are inexperienced with Cocos2D coding. Here is the code I have used to get rid of the 'enemy'

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair danald:(CCNode *)nodeA wildcard:(CCNode *)nodeB {

    float energy = [pair totalKineticEnergy];

    if (energy > 5000.f) {
        [self danaldRemoved:nodeA];
    }

}

If the object is hit with a certain speed it will call the method below

- (void)danaldRemoved:(CCNode *)Danald {

    CCParticleSystem *explosion = (CCParticleSystem *)[CCBReader load:@"Explosion"];

    explosion.autoRemoveOnFinish = TRUE;

    explosion.position = Danald.position;

    [Danald.parent addChild:explosion];

    [Danald removeFromParent];
}

Thanks in an advanced, sorry if this question has been asked before but I cannot find it

È stato utile?

Soluzione

Well I would suggest this method:

Create a variable where you store the number of sprites left. For example:

int spritesLeft;

And then initialize it to 0:

-(void) didLoadFromCCB{
 //REST OF CODE
 spritesLeft=3; //3 because you said there are only 3.
}

Now when you call danaldRemoved: method, just subtract 1 to spritesLeft, and check if spritesLeft is equal to 0. If it's true, just call your method to make a button appear:

- (void)danaldRemoved:(CCNode *)Danald {

spritesLeft--; //substract 1

CCParticleSystem *explosion = (CCParticleSystem *)[CCBReader load:@"Explosion"];

explosion.autoRemoveOnFinish = TRUE;

explosion.position = Danald.position;

[Danald.parent addChild:explosion];

[Danald removeFromParent];

//check if game is over.
  if (spritesLeft == 0){
     [self printButton];
  }
}

Now create the method printButton, but before go to SpriteBuilder, create the button and place it where you want. Now uncheck 'Visible' value, and then go to code connections, and select 'Doc root var' (under custom class) and write a name for the button, for example: nextButton. At the selector value write: changeLevel and target: document root

Now declare it at the top of your .m file as you did with any other objects:

CCButton *nextButton;

Method for button (just set visibility ON)

-(void) printButton{
   nextButton.visible = YES;
}

And now your method to change level:

-(void) changeLevel{
  CCScene *nextLevel = [CCBReader loadAsScene:@"YOUR LEVEL"];
    [[CCDirector sharedDirector] replaceScene:nextLevel];
}

Hope this helps!

EDIT: HOW TO DETECT WHEN A SPRITE GOES OFF THE SCREEN

As I said, create any kind of physic object in spritebuilder. For example, I use CCNodeColor. Then make it a rectangle and place it at left of the screen. Now go to physics, enable physics, polygon type and static. Now in connections, select doc root var and call it _leftNode. Now repeat with top,right and bottom and call them _topNode, etc.

Now go to code, declare your new nodes: CCNode *_leftNode; and so... Now let's make a collision type:

_bottomNode.physicsBody.collisionType = @"_bound";
_leftNode.physicsBody.collisionType = @"_bound";
_rightNode.physicsBody.collisionType = @"_bound";
_topNode.physicsBody.collisionType = @"_bound";

And do the same with your sprite, but I think you have done that before. Let's make an example:

spritename.physicsBody.collisionType = @"_sprite";

So now implement the method:

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair _sprite:(CCNode *)nodeA _bound:(CCNode *)nodeB {

    [_physicsNode removeChild:nodeA cleanup:YES];

}

And that's all.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top