Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
james baxter
30 PointsSprite Kit - Denoting a Hit Streak in a Game
I've built a game with SpaceCat as the basis. I built a simple streak multiplier into the game using the method below.
- (void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask ) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ( firstBody.categoryBitMask == THCollisionCategoryEnemy &&
secondBody.categoryBitMask == THCollisionCategoryProjectile ) {
NSLog(@"BAM!");
THSpaceDogNode *spaceDog = (THSpaceDogNode *)firstBody.node;
THProjectileNode *projectile = (THProjectileNode*)secondBody.node;
// increase the counter
_streakCount++;
// add points considering the streak
if (_streakCount >= 5) {
[self addPoints:THPointsPerHit * 2];
}
if (_streakCount >= 10) {
[self addPoints:THPointsPerHit * 3];
}
if (_streakCount >= 15) {
[self addPoints:THPointsPerHit * 5];
}
if (_streakCount >= 20) {
[self addPoints:THPointsPerHit * 10];
}
else {
[self addPoints:THPointsPerHit];
}
This code works great and the score is multiplying exactly how I want it to.
Now I'm trying to add a SKSpriteNode image that will pop up to show the user if they have a streak.
I was able to get the image to pop up by adding this code.
if (_streakCount == 5) {
SKSpriteNode *streakFive= [SKSpriteNode spriteNodeWithImageNamed:@"StreakFiveimg"];
[streakFive setPosition:CGPointMake(self.size.width / 1.0366, self.size.height/1.46)];
[self addChild:streakFive];
This works but I need to be able to make the image go away once their streak is broken. I tried adding some code to the "else" section that resets the point multiplier but that wasn't working for me. The score multiplier resets perfectly, but the streak image stays up on the screen.
If it's too complicated to make the images appear and disappear maybe it would be simpler to add a counter that displays the current streak and then resets to zero when the streak is broken. I'm not sure how to go about that either though.
I'm still learning and I would definitely appreciate any help that anyone is willing to offer!
3 Answers
Ryan Ackermann
14,665 PointsI think your looking for something like:
// Your code from above modified
SKSpriteNode *streakFive= [SKSpriteNode spriteNodeWithImageNamed:@"StreakFiveimg"];
if (_streakCount == 5) {
streakFive.name = @"streakImage";
[streakFive setPosition:CGPointMake(self.size.width / 1.0366, self.size.height/1.46)];
[self addChild:streakFive];
SKNode *n = [self childNodeWithName: @"n"];
if (_streakCount == 0 && n) {
[streakFive removeFromParent];
}
This will check if your steak count variable is back to zero and that your streakeFive variable is instantiated!
Hope this helps :)
james baxter
30 PointsThanks Ryan, Should that go in with the other if statements or somewhere else? When I put it in where I had it before, it's breaking everything.
james baxter
30 PointsI figured it out. I was missing a bracket haha. Thanks!
Ryan Ackermann
14,665 PointsGlad I could help :)