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

Richard Rodriguez
Richard Rodriguez
2,687 Points

SpriteKit Game: Help with variation of game! How can I assign a different value of points for every dog type node?

Basically I have a typedef NS_ENUM with 3 RRSpaceDogTypes. SpaceDogTypeA SpaceDogTypeB SpaceDogTypeC

I want to assign a different value of points for each specific contact with a SpaceDogType ( i.e. if the particle hits SpaceDogTypeA then I want to assign 10points, if it hits B then 30 points....)

My code currently assigns points per hit, and I have been trying various ways to isolate the SpaceDogTypes by modifying the code below, but i can't figure it out !!!

Richard Rodriguez
Richard Rodriguez
2,687 Points
  • (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 == RRCollisionCategorySpaceDog && secondBody.categoryBitMask == RRCollisionCategoryGround) { NSLog(@"Bam");

RRSpaceDog *spaceDog = (RRSpaceDog *)firstBody.node;

[spaceDog removeFromParent];
[self loseLife];

} else if (firstBody.categoryBitMask == RRCollisionCategorySpaceDog && secondBody.categoryBitMask == RRCollisionCategoryParticle) { NSLog(@"boom");

RRSpaceDog *spaceDog = (RRSpaceDog *)firstBody.node;
NSString *explosionPath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
SKEmitterNode *explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:explosionPath];
explosion.position = CGPointMake(self.frame.size.width -20, self.frame.size.height-125);

[explosion runAction:[SKAction waitForDuration:.5] completion:^{
    [explosion removeFromParent];

}];


[self addPoints:RRPointsPerHit];

[self addChild:explosion];
[spaceDog removeFromParent];

3 Answers

Stone Preston
Stone Preston
42,016 Points

does your space dog object have a type property? if so you could check that

else if (firstBody.categoryBitMask == RRCollisionCategorySpaceDog  && secondBody.categoryBitMask == RRCollisionCategoryParticle) {
    NSLog(@"boom");

    RRSpaceDog *spaceDog = (RRSpaceDog *)firstBody.node;

    //check the space dog type
   if (spaceDog.type == SpaceDogTypeA) {

      [self addPoints:100];
    } else if (spaceDog.type == SpaceDogTypeB) {

       [self addPoints:200;

    } else {

        [self addPoints:300];

    }

    NSString *explosionPath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
    SKEmitterNode *explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:explosionPath];
    explosion.position = CGPointMake(self.frame.size.width -20, self.frame.size.height-125);

    [explosion runAction:[SKAction waitForDuration:.5] completion:^{
        [explosion removeFromParent];

    }];
Richard Rodriguez
Richard Rodriguez
2,687 Points

typedef NS_ENUM(NSUInteger, RRSpaceDogType) { RRSpaceDogTypeA = 0, RRSpaceDogTypeB = 1, RRSpaceDogTypeC = 2, };

@interface RRSpaceDog : SKSpriteNode

+(instancetype) smushBallofType:(RRSpaceDogType)type;

@end

Stone Preston
Stone Preston
42,016 Points

ok so yeah that should work then

Richard Rodriguez
Richard Rodriguez
2,687 Points

Yes I do have a SpaceDogType, but when i put your code in the program it will not recognize the spaceDog.type

Richard Rodriguez
Richard Rodriguez
2,687 Points

It says : Property 'type' not found on object of type 'RRSpaceDog'

Stone Preston
Stone Preston
42,016 Points

ok so add a property named type, and in your spaceDog constructor set its property type using the type you pass in as a parameter.

Richard Rodriguez
Richard Rodriguez
2,687 Points

+(instancetype) SpaceDogOfType:(RRSpaceDogType)type{ RRSpaceDog *spaceDog;

Sorry this is probably really simple but I am kind of lost.. I have the above code in my RRSpaceDog.h

So when you say create a new property, do you mean create it in the RRGamePlayScene..

Thanks so much for your help too !!

Stone Preston
Stone Preston
42,016 Points

no add the type property to your spaceDog class header file. it should look something like this:

@property (nonatomic) RRSpaceDogType type;
Richard Rodriguez
Richard Rodriguez
2,687 Points

Wow amazing ! That works I am like so happy right now.

I really appreciate your help!