Sprite Kit contact count - objective-c

I have an object (ball) contacting the ground: the ball is very bouncy & contacts ground more then maybe 7 times.
I've utilized the categoryBitMask & contactTestBitMask and set everything up nicely in -(void)didBeginContact:(SKPhysicsContact *)contact.
I've created a property SKAction to handle sound effects & assigned an audio file to the action:
self.sfxBounce1 = [SKAction playSoundFileNamed:#"bounce1.caf" waitForCompletion:NO];
I call upon this sfxBounce1 in the didBeginContact. Point is it's all working great. Except one problem is that my ball bounces a lot, but for personal reasons, I need the auddio/SKAction to stop playing after the ball has contacted the ground 3 times.
In game, the ball is still bouncing after 3 times it's just I need the sound to stop playing.
But because the code is in an IF statement in the didBeginContact method, the audio keeps playing and playing after every contact with the ground. I'm still very green in programming, especially with Objective-C.
Below is some code
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if ((firstBody.categoryBitMask == ballCategory) && (secondBody.categoryBitMask == groundCategory)) //Ball & Ground contact
{
int random = arc4random() %6; //Random # generator of 7 possibilites.
switch (random) //Generated # represents a case, which in turn initiates a bounce sound upon contact.
{
case 0:
[self runAction:self.sfxBounce1];
break;
case 1:
[self runAction:self.sfxBounce2];
break;
case 2:
[self runAction:self.sfxBounce3];
break;
case 3:
[self runAction:self.sfxBounce4];
break;
case 4:
[self runAction:self.sfxBounce5];
break;
case 5:
[self runAction:self.sfxBounce6];
break;
case 6:
[self runAction:self.sfxBounce7];
break;
}
[self deleteNode];
}
I can't figure out what code to type in each of the 6 cases (between [self runAction:self.sfxBounce1]; & break;) that will stop the action after a set amount of bounces/contacts with the ground.

At the top in your #interface create an int you can reference throughout your code:
property (nonatomic) int bounceCount;
Here is an updated version of your didBeginContact:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if ((firstBody.categoryBitMask == ballCategory) && (secondBody.categoryBitMask == groundCategory)) //Ball & Ground contact
{
self.bounceCount++;
if(self.bounceCount>3)
return;
//Your Code
}
}

Related

Trigger audio files based on a method call or other variables

I am designing a game that changes scenes as the user is playing. The scenes change based on a NSTimer. I can ge everything to change accordingly in regards to graphics. Can even get background music to change.
Where I seem to be hitting a roadblock is changing the score sound and the sound for when the player collides with an object and dies.
As it it's right now I can cancel the sound for scene one but cannot get another one to play.
* on phone don't know how to hand code a code block on here*
If([_timer isValid]) {
//plays correct audio and does not continue when scene changes
}
Else if ([_timer == nil ]){
//suppose to play other sound but does nothing.
}
I would really prefer to call the different sounds based on if a method is called to change something along the lines of
Else if ([ _method1 == TRUE]) {
//play sound
}
Any advice in the right direction appreciated.
EDIT
- (void)didBeginContact:(SKPhysicsContact *)contact {
if( _moving.speed > 0 ) {
if( ( contact.bodyA.categoryBitMask & scoreCategory ) == scoreCategory || ( contact.bodyB.categoryBitMask & scoreCategory ) == scoreCategory ) {
// has contact with score entity
_score++;
_scoreLabelNode.text = [NSString stringWithFormat:#"%d", _score];
//Pick Up Egg
//Sound for Score
SKAction* teslaPoint = [SKAction playSoundFileNamed:#"Point1" waitForCompletion: YES];
[self runAction:teslaPoint withKey:#"teslaPoint"];
I have been trying to structure an IF statement based on the background image because that changes with the sounds.
EDIT
_scoreSound = [SKAudioNode node];
SKAudioNode* teslaPoint = [SKAudioNode nodeWithFileNamed:#"Point1"];
[_scoreSound addChild:teslaPoint];
EDIT
THIS IS ADDED IN initwithSize:
_scoreSound = [SKNode node];
[self addChild:_scoreSound];
Then its called in the contacts
EDIT
//Sound for Score
if([_timer isValid]){
SKAction* teslaPoint = [SKAction playSoundFileNamed:#"Point1" waitForCompletion:YES];
[self runAction:teslaPoint withKey:#"teslaPoint"];
}
if ([_bgImage isEqual: #"bgimage2.png"]){
SKAction* teslaPoint = [SKAction playSoundFileNamed:#"grassypoint" waitForCompletion:YES];
[self runAction:teslaPoint withKey:#"teslaPoint"];
}

How to check whether some SKPhysicsBody in contained in another

I've reviewed similar questions and do believe I'm asking about different thing.
I have a player node which has it's own body and I want to check when it's been moved to some part of the level (exit node). Examples of this may be putting some dynamic item in a box, or parking a car at the specific place.
In my case player is SKSpriteNode with physicsBody created from similar texture (bodyWithTexture) and exit is SKNode with no visual and physicsBody created from polygon path (4 points, non rectangle).
I have a code which does something I wan't, but I do believe there maybe some cases it's doing something I don't want and there are better ways to do it.
int contactsCount = 0;
- (void)didBeginContact:(SKPhysicsContact *)contact{
if ((contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) & exitCategory) {
contactsCount++;
if (contactsCount == 4) {
[player runAction:[SKAction colorizeWithColor:[UIColor greenColor] colorBlendFactor:1.0 duration:1.0] completion:^{
self.physicsWorld.speed = 0;
}];
}
return;
}
[player runAction:[SKAction colorizeWithColor:[UIColor redColor] colorBlendFactor:1.0 duration:1.0] completion:^{
self.physicsWorld.speed = 0;
}];
}
- (void)didEndContact:(SKPhysicsContact *)contact{
if ((contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) & exitCategory) {
contactsCount--;
return;
}
}

SpriteKit Removing sprites to redraw

Currently in my application I've been adopting a technique to remove/re-draw certain sprites.
in example, the app is a poker app. So when a call/raise/check is made, there is a chip that is placed in-front of the player with an SKLabelNode containing the bet,check etc.. However, removing the previous to then re-add the new is inconsistent and causes a lot of EXC_BAD_ACCESS errors. Now, I guess I could program it to nest search for that node and alter the value instead of redrawing. However, it's used in multiple occasions and will at some point rely on removing the child from it's parent.
What I'm asking is what is the best technique to achieve this without the possibility of inconsistent crashes..?
-(void)removePreviousChips:(NSString *)player {
NSString *string = [NSString stringWithFormat:#"chipLabel:%#", player];
SKNode *node = [tableSprite childNodeWithName:string];
[node removeFromParent];
}
-(void)updateChipStacksAndPlayerPositionsWith:(NSDictionary *)dict {
// [tableSprite removeAllChildren];
for (int i = 0; i < 8; i++) {
//loop through seats taken if value then draw.
if (![[dict valueForKey:_seatNames[i]] isEqualToString:#""]) {
[self drawAvatarWithPlayerName:[dict valueForKey:_seatNames[i]] withSeatPosition:[[seatPositions valueForKey:_seatNames[i]] CGPointValue] withChipStack:[[dict valueForKey:#"startingChips"] intValue]];
}
}
if ([self displayStartGameButton]) {
[startGameBG runAction:[SKAction fadeAlphaTo:1 duration:0.5]];
} else {
[startGameBG runAction:[SKAction fadeAlphaTo:0 duration:0.5]];
}
}
Them two examples are consistent ways to crash my app.
EDIT
for example, a better approach would be to detect whether the node is present before the requirement to remove it from it's parent and redraw it. However, something to detect it's presence is not working out for me
SKNode *node = [tableSprite childNodeWithName:[dict valueForKey:_seatNames[i]]];
if (node) {
NSLog(#"node for %# exists", [dict valueForKey:_seatNames[i]]);
} else {
NSLog(#"node for %# doesn't exist", [dict valueForKey:_seatNames[i]]);
}

using cocos2d to design shootgame, i can't invoke stopGame method

-(void)spriteMoveFinishedid)sender //when sprite is outside of screen,it's deleted;
{
CCSprite *sprite = (CCSprite *)sender;
if (sprite.tag == 1) //it's enemy;
{
escapeNum++;
[self removeChild:sprite cleanup:YES];
if (escapeNum == 10)
{
[self stopGameEx]; //Because the speed of every enemy isn't same to other and there are bullets,it maybe happens that two sprites disappear at the same time, and the program stop at this with error --- program received signal:“EXC_BAD_ACCESS”。
}
//...........
}
}
How to resolve it?
Inside if loop, change sprite tag to mark it as expired sprite, and some where else remove.
enum
{
kTagExpiredSprite = 999 //any number not ur sprite tag(1)
};
.....
-(void)spriteMoveFinishedid)sender //when sprite is outside of screen,it's deleted;
{
CCSprite *sprite = (CCSprite *)sender;
if (sprite.tag == 1) //it's enemy;
{
escapeNum++;
sprite.tag = kTagExpiredSprite;
if (escapeNum == 10)
{
[self stopGameEx]; //Because the speed of every enemy isn't same to other and there are bullets,it maybe happens that two sprites disappear at the same time, and the program stop at this with error --- program received signal:“EXC_BAD_ACCESS”。
}
//...........
}
}

Handling Double Tap in Cocos2d

This question is too primitive, but I have been trying for the last 8 hours, and it is eating away my energy ( and confidence levels too :))
In my class, I have registered for Targeted touches.
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
I would like to check if the user is performing single tap or double tap. In ccTouchBegan method, first gets a touch with singleTap, then gets a touch with doubleTap. I found a very interesting solution for this one, in one of the Cocos2d Forums. ( I could not locate it now.. )
The solution goes like this.
switch (touch.tapCount) {
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(handleSingleTouch:) object:singleTouchLocation];
// Handle Double Touch here
break;
case 1:
self.singleTapLocation = ccp(location.x,location.y);
self.singleTouchLocation = touch;
[self performSelector:#selector(handleSingleTouch:) withObject:touch afterDelay:0.3
];
break;
}
Everything looks to be working fine. Essentially, you schedule a single touch handler with a delay and see if it is actually a double tap. If it is a double tap, then cancel singletap handler and perform logic for double tap.
But my problem is, In the single touch handler (handleSingleTouch), I am adding a CCSprite to the UI. This action is not working. No sprite gets added. (Although the method is called.). In fact this works, If I call the selector with no delay, but with delay, the sprite is not added.
I am not a Objective C expert, So, I apologize if the question is too primitive.
Edit 1: The original thread.
Edit 2: Posting handleSingleTouch.. only relevant code.
-(void) handleSingleTouch:(UITouch * ) touch {
CGPoint location = [touch locationInView: [touch view]];
CGPoint glLocation = [MainSceneLayer locationFromTouch:touch];
//Single tap
CCLOG(#"Single tap: Adding marker image");
if(zoomedOut) {
CGPoint globalCoordinates = [quadConvertor convertLocalToGlobal:location
inActiveQuadrant:activeQuadrant];
if( [self isTouchValidForSelectedItem:globalCoordinates] == Matched) {
[self addMarkerImages:glLocation];
} else if([self isTouchValidForSelectedItem:globalCoordinates] == NotMatched) {
[missedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
missedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:#selector(makeInvisible:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[missedLabel runAction:seq];
} else {
[alreadyAccountedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
alreadyAccountedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:#selector(makeInvisible1:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[alreadyAccountedLabel runAction:seq];
}
}
swipeStartPoint = [touch locationInView:touch.view];
}
i dont know if thats a typo in your question only but your delay method is all wrong. There is no withDelay: argument. it should look like this:
[self performSelector:#selector(handleSingleTouch:) withObject:touch afterDelay:0.1];
EDIT:
Since your problem is most probably with the touch getting lost. Try [touch retain] before calling the delayed method. Another way is changing the handleSingleTouch: method to take two floats x and y or a CCPoint as arguments instead of a UITouch. Then you create the floats before the delayed method and pass them in the delayed method. This way you will avoid a memory leak from retaining a touch as well since most probably you cant release it after calling the delayed method.
hope this helps