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

iOS

Spritekit: Score multiplier for streak

I've been doing a lot of reading and looking around online but I can't find a good way of implementing a score multiplier for a streak of hits.

For example if the player hits 10 enemies in a row without missing the score jumps to 2x for each consecutive hit until the streak is broken.

It seems like you should be able to check for consecutive hits and then flag them somehow. I think this kind of upgrade would make a game like space cat much more interesting. I just can't figure out a good way to implement it.

Has anyone done this? Any one have any ideas?

2 Answers

Dimitris Sotiris Tsolis
Dimitris Sotiris Tsolis
28,141 Points

Here is my suggestion. First, go to the THUtil.h file and add a new category

typedef NS_OPTIONS(uint32_t, THCollisionCategory) {
    THCollisionCategoryEnemy        = 1 << 0,
    THCollisionCategoryProjectile   = 1 << 1,
    THCollisionCategoryDebris       = 1 << 2,
    THCollisionCategoryGround       = 1 << 3,
    THCollisionCategoryScreenBounds = 1 << 4   // <-- ADD THIS ONE
};

Then, in when you setup the physics body for the projectile (in THProjectileNode.m file) go and add the new category as a "contactTestBitMask". The results will be

self.physicsBody.contactTestBitMask = THCollisionCategoryEnemy | THCollisionCategoryScreenBounds;

After that, go to the THGamePlayScene.m file, in the initWithSize: method and after these lines of code...

self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;

...add these lines.

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = THCollisionCategoryScreenBounds;
self.physicsBody.contactTestBitMask = THCollisionCategoryProjectile;

Last changes: go to the didBeginContact:(SKPhysicsContact *)contact method and add an else if like this

else if (firstBody.categoryBitMask == THCollisionCategoryProjectile && secondBody.categoryBitMask == THCollisionCategoryScreenBounds) {
    NSLog(@"hit screen bounds");
}

By doing all these changes, you can "know" about a projectile getting out of the screen.

Now, the logic is the following

  1. Create a variable which will be the counter and init it to 0
  2. When a projectile hit an enemy, increase the counter and then add the points considering the multiplier's value
  3. When a projectile hit the screen's bounds (or a dog, the ground), reset the counter to 0

Thanks so much for your answer. I've got pretty much everything good to go, except I can't figure out part two of your final points. Could you give an example of how to accomplish that?

I'm sorry to ask for so much help. I'm still learning. I did give it a go myself a couple times before coming back with my head hung low haha.

Dimitris Sotiris Tsolis
Dimitris Sotiris Tsolis
28,141 Points

No problem, don't worry! Now, about my point. Let's say you've got a counter named "_streakCount". At the didBeginContact: method you could do something like this

//..... some code

if (firstBody.categoryBitMask == DTCollisionCategoryEnemy &&
    secondBody.categoryBitMask == DTCollisionCategoryProjectile) {

    DTSpaceDogNode *spaceDog = (DTSpaceDogNode *) firstBody.node;
    DTProjectileNode *projectile = (DTProjectileNode *)secondBody.node;


    // increase the counter
    _streakCount++;
    // add points considering the streak
    if (_streakCount >= 10) {
        [self addPoints:DTPointsPerHit * 2];
    }
    else {
        [self addPoints:DTPointsPerHit];
    }


    [self runAction:self.explodeSFX];
    [spaceDog removeFromParent];
    [projectile removeFromParent];
    [self createDebrisAtPosition:contact.contactPoint];
}

//..... some code

Thank you so much, the multiplier is working perfectly now.

I figured out step three on my own (so proud of myself haha).

I really appreciate the help!