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 trialKelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsskphysics
l tried creating the game differently. l have the enemies coming in from the right hand side and the hero on the right left corner with the machine. the idea is the enemies get hit by the projectile and hero gain points or the enemies get the machine and the hero loses point. my codes are as follows
gameplay scene
-(void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
if ( firstBody.categoryBitMask == RLCollisionCategoryEnemy && secondBody.categoryBitMask == RLCollisionCategoryProjectile ) {
RLDogNode *dog = (RLDogNode *)firstBody.node;
RLProjectileNode *projectile = (RLProjectileNode *)secondBody.node;
[dog removeFromParent];
[projectile removeFromParent];
} else if (firstBody.categoryBitMask == RLCollisionCategoryEnemy && secondBody.categoryBitMask == RLCollisionCategoryMachine) {
RLDogNode *dog = (RLDogNode*)firstBody.node;
RLMachineNode *machine = (RLMachineNode *)secondBody.node;
[dog removeFromParent];
[machine removeFromParent];
}
projectile physics
- (void) setupPhysicsBody {
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.categoryBitMask = RLCollisionCategoryProjectile;
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = RLCollisionCategoryEnemy;
}
machine physics
- (void) setupPhysicsBody {
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.categoryBitMask = RLCollisionCategoryMachine;
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = RLCollisionCategoryEnemy;
}
dog physics
- (void) setupPhysicsBody {
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.velocity = CGVectorMake(-50, 0);
self.physicsBody.categoryBitMask = RLCollisionCategoryEnemy;
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = RLCollisionCategoryProjectile | RLCollisionCategoryMachine;
}
and this code in the util.h file`typedef NS_OPTIONS(uint32_t, RLCollisionCategory) {
RLCollisionCategoryEnemy = 1 << 0,
RLCollisionCategoryProjectile = 1 << 1,
RLCollisionCategoryDebris = 1 << 2,
RLCollisionCategoryMachine = 1 << 3
};
so far l have hack my way for the projectile to get the enemies for them to disappear but they get throughout pass the machine without disappearing. is there anything am doing here? if yeah? please let me know Amit Bijlani
2 Answers
Michael Blazek
162 PointsIf I understand correctly, you should not be calling
[machine removeFromParent];
So i would remove that line. You mentioned an error, but what is the error your getting?
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsi havce been able to crack i get logs when they neck of the dog gets in contact with the machine. only problem now is they dog is removed from the parent since am using this code
else if (firstBody.categoryBitMask == RLCollisionCategoryEnemy && secondBody.categoryBitMask == RLCollisionCategoryMachine) {
RLDogNode *dog = (RLDogNode*)firstBody.node;
RLMachineNode *machine = (RLMachineNode *)secondBody.node;
[dog removeFromParent];
[machine removeFromParent];
}
i tried removing the last line so that the machine doesn't move. but was ended up with an error. how can i resolve the please?
@[Amit Bijlani](https://teamtreehouse.com/amit)
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsKelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsThe complier points at the machine pointer and then says unused variable
Michael Blazek
162 PointsMichael Blazek
162 PointsThat is just a warning that you declared a variable and did not use it in the scope of the block of code it was declared in. If you removed this line as well you would not get the warning:
RLMachineNode *machine = (RLMachineNode *)secondBody.node;
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsKelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsthanks that did work. one more question Micheal. how can i set it just at that point of contact the enemy disappears at now their body get pass the machine have way before they disappear
Michael Blazek
162 PointsMichael Blazek
162 PointsKelvin, it could have to do with the physics boxes you have set up on the objects. Seems like you changed the layout of the game, a ss of the new layout may help. If you can annotate what is going wrong that may help as well.