sprite kit can't receive contact message nodes - ios

so i made the code below to receive contact:
- (void) didBeginContact:(SKPhysicsContact *)contact{
SKPhysicsBody *firstBody,*secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
//player<enemy<abilities
firstBody =contact.bodyA; // player
secondBody = contact.bodyB; // enemy or abilities
} else {
firstBody = contact.bodyB;//player
secondBody = contact.bodyA;//enemy or projectile
}
if (firstBody.categoryBitMask == collisionCatergoryPlayer &&
secondBody.categoryBitMask == collisionCatergoryEnemy) {
NSLog(#"test");
playerBall *ball = (playerBall *)firstBody.node;
[ball removeFromParent];
enemyBall *enemyBall1 = (enemyBall *)secondBody.node;
[enemyBall1 removeFromParent];
} else if (firstBody.categoryBitMask == collisionCatergoryPlayer &&
secondBody.categoryBitMask == collisionCatergoryAbilities){
NSLog(#"upgrade");
abilities *abilities1 = (abilities *)secondBody.node;
//if theres abilities2 and 3,make a catergory bit mask for it and remove it from parent
[abilities1 removeFromParent];
}
}
but i can't receive the nslog messages from the code above,please help!!!!
also the remove nodes are not doing anything,my bit mask is player

Related

Accessing individual objects that all have the same categoryBitMask

I have several game world objects the player needs to interact with individually upon his physicsBody.categoryBitMask contacting them. Instead of using separate categoryBitMasks for each individual object (object count surpasses categoryBitMask's limit, which is 32) I just use 1 categoryBitMask and gave all the objects individual names. Here's how it looks in code:
-(void)createCollisionAreas
{
if (_tileMap)
{
TMXObjectGroup *group = [_tileMap groupNamed:#"ContactZone"]; //Layer's name.
//Province gateway.
NSDictionary *singularObject = [group objectNamed:#"msgDifferentProvince"];
if (singularObject)
{
CGFloat x = [singularObject[#"x"] floatValue];
CGFloat y = [singularObject[#"y"] floatValue];
CGFloat w = [singularObject[#"width"] floatValue];
CGFloat h = [singularObject[#"height"] floatValue];
SKSpriteNode *object = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(w, h)];
object.name = #"provinceGateway";
object.position = CGPointMake(x + w/2, y + h/2);
object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(w, h)];
object.physicsBody.categoryBitMask = terrainCategory;
object.physicsBody.contactTestBitMask = playerCategory;
object.physicsBody.collisionBitMask = 0;
object.physicsBody.dynamic = NO;
object.physicsBody.friction = 0;
object.hidden = YES;
[_backgroundLayer addChild:object];
}
/*More code written below. Too lazy to copy & paste it all*/
}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody; //Create 2 placeholder reference's for the contacting objects.
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) //If bodyA has smallest of 2 bits...
{
firstBody = contact.bodyA; //...it is then the firstBody reference [Smallest of two (category) bits.].
secondBody = contact.bodyB; //...and bodyB is then secondBody reference [Largest of two bits.].
}
else //This is the reverse of the above code (just in case so we always know what's what).
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
/**BOUNDARY contacts*/
if ((firstBody.categoryBitMask == noGoCategory) && (secondBody.categoryBitMask == playerCategory))
{
//Boundary contacted by player.
if ([_backgroundLayer childNodeWithName:#"bounds"])
{
NSLog(#"Player contacted map bounds.");
}
if ([_backgroundLayer childNodeWithName:#"nogo"])
{
NSLog(#"Player can't go further.");
}
if ([_backgroundLayer childNodeWithName:#"provinceGateway"])
{
NSLog(#"Another province is ahead. Can't go any further.");
}
if ([_backgroundLayer childNodeWithName:#"slope"])
{
NSLog(#"Player contacted a slope.");
}
}
The problem is, in didBeginContact method, when the player contacts any object all the code gets executed. Even the code from objects the player hasn't contacted yet. This means the IF statements, such as if ([_backgroundLayer childNodeWithName:#"slope"]), are not complete. Can someone tell me how to properly write out the IF statements for individual object contact? The following IF statement, inside didBeginContact, doesn't work either:
if ([_player intersectsNode:[_backgroundLayer childNodeWithName:#"slope"]])
Your if statements are all just checking if the child exists, which it seems they all do. What I think you want to check is if the collision node has the name you are looking for, so change:
if ([_backgroundLayer childNodeWithName:#"bounds"])
to:
if ([firstBody.node.name isEqualToString:#"bounds"])

How to blink SpritekitNode?

i want to blink SpritekitNode when two player and monster collapse . im using code in didBeginContact:
-(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.node.name isEqual: OBSTACLE_KEY] && ( [secondBody.node.name isEqual: PLAYER_KEY] ))
{
if ([kPref integerForKey:BUY_LIFES] > 0)
{
SKAction *blink = [SKAction sequence:#[[SKAction fadeOutWithDuration:0.25],
[SKAction fadeInWithDuration:0.25]]];
SKAction *blinkForever = [SKAction repeatAction:blink count:3];
[secondBody.node runAction: blinkForever];
int life = [kPref integerForKey:BUY_LIFES] - 1;
[kPref setInteger:life forKey:BUY_LIFES];
}
else
{
[kPref setInteger:sec forKey:HIGH_SCORE];
self.paused =YES;
}
}
}
it working but it continuously call didBeginContact when monster touches player node till it connected to it, i want to set in way that if monster node touch player than it blinks 3 times and that period of time none monster can touch player node.
Please help me out.!

Cannot add SKSpriteNode dynamically?

I'm adding SKSpriteNode dynamically on didBeginContact delegate method :
Here is my code
-(void)adBall
{
SKSpriteNode *obj = [[SKSpriteNode alloc]initWithImageNamed:#"ball.png"];
obj.physicsBody = [SKPhysicsBody bodyWithTexture:obj.texture size:obj.texture.size];
obj.position = CGPointMake(100, 100);
obj.name = OBSTACLE_KEY;
obj.physicsBody.categoryBitMask = BallCategory1;
obj.physicsBody.contactTestBitMask = GreenLineCategory | RedLineCategory ;;
obj.physicsBody.collisionBitMask = GreenLineCategory | RedLineCategory ;;
obj.physicsBody.affectedByGravity = YES;
obj.physicsBody.dynamic = YES;
obj.physicsBody.friction = 0.0f;
obj.physicsBody.linearDamping = 0.0f;
obj.physicsBody.restitution = 1.0f;
obj.physicsBody.allowsRotation = NO;
[obj.physicsBody applyForce:CGVectorMake(200, 300)];
}
-(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.node.name isEqual: OBSTACLE_KEY] && ( [secondBody.node.name isEqual: BLACKLINE_KEY]))
{
[self adBall];
}
}
But cant appear in screen it just show ball at bottom and disappear.
and if add [self adBall]; in didMoveToView method than working fine.
So Plz tell me where I'm wrong here ?
You never run
[self addChild:obj];
inside your adBall method. Therefore the newly created sprite won't be added to the scene graph and naturally won't be drawn (it gets discarded at the end of the method).
Ok Done it.
as LearnCocos2D said that i didn't add [self addChild:obj]; i add that line but than it not working in didMoveToView than what i did is called method after some time.
Like :
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self adBall];
});

SkSpriteNode collision not being detected

Im using Sprite Kit to detect collision between two objects. Here is how I define their bitmasks.
static const uint32_t puffinCategory = 0x1 << 0;
static const uint32_t planeCategory = 0x1 << 1;
Here is my code as to how setup the puffins and planes physics body.
For puffin
SKSpriteNode *PuffinNode = [[SKSpriteNode alloc]initWithImageNamed:#"puffin"];
PuffinNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:PuffinNode.size];
PuffinNode.physicsBody.usesPreciseCollisionDetection = YES;
PuffinNode.physicsBody.categoryBitMask = puffinCategory;
PuffinNode.physicsBody.dynamic = NO;
PuffinNode.physicsBody.collisionBitMask = puffinCategory;
PuffinNode.physicsBody.contactTestBitMask = planeCategory;
[PuffinNode setZPosition:1.5];
For Plane
SKSpriteNode *planeSpriteNode = [[SKSpriteNode alloc]initWithImageNamed:planeStringFileName];
planeSpriteNode.position = CGPointMake(0, self.view.frame.size.height*-1);
planeSpriteNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:planeSpriteNode.size];
planeSpriteNode.physicsBody.usesPreciseCollisionDetection = YES;
planeSpriteNode.physicsBody.categoryBitMask = planeCategory;
planeSpriteNode.physicsBody.dynamic = NO;
planeSpriteNode.physicsBody.collisionBitMask = puffinCategory;
planeSpriteNode.physicsBody.contactTestBitMask = puffinCategory;
Here is my implementation of the delegate method didBeginContact:
-(void)didBeginContact:(SKPhysicsContact *)contact{
NSLog(#"collission method run");
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 & puffinCategory) != 0 && (secondBody.categoryBitMask & planeCategory) != 0 ){
NSLog(#"collission occured");
}
}
Im not seeing the method logging if it is called, nor do I see a log when the two sprites collide.
You set
PuffinNode.physicsBody.dynamic = NO;
and
planeSpriteNode.physicsBody.dynamic = NO;
two static bodies cannot collide, at least one should be dynamic
Some problems :
1) Did you set the SKScene's physicsWorld.contactDelegate ?
2) Both of your nodes have no dynamic. If you want them to interact in the physics world you should make them under physics laws. One of them at least must be dynamic.
3) Your collisionBitMask are not well set.

Sprite Kit collisions for multiple collisions

I have looked, and have found answers for single collisions, but I am looking for a way to detect more than one type of collision. I am making a game where there are 3 collisions I would like. The user plane colliding with enemy bullets, the user's bullet colliding with the enemy plane (which i have working already), and the enemy bullet and user bullet colliding. I have all the categoryBitMask and contactTestBitMask set up and correct. Here is my delegate method.
- (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 user plane hits enemy bullet
if ((firstBody.categoryBitMask == playerShipCategory) &&
(secondBody.categoryBitMask == enemyBulletCategory)) {
[self takeDamageByAmount:POINT_INCREMENTER];
[_enemyBullet removeFromParent];
SKAction *bounce = [SKAction sequence:#[
[SKAction fadeAlphaTo:.5 duration:.2],
[SKAction fadeAlphaTo:1.0 duration:.2],
[SKAction fadeAlphaTo:.5 duration:.2],
[SKAction fadeAlphaTo:1.0 duration:.2]
]];
[_playerPlane runAction:bounce];
}
// if the user bullet hits the enemy bullet
else if ((firstBody.categoryBitMask == bulletCategory) &&
(secondBody.categoryBitMask == enemyBulletCategory)) {
[_enemyBullet removeFromParent];
[_bullet removeFromParent];
}
// if bullet hits enemy ship - THIS ONE WORKS, but none of the others work for some reason
else if ((firstBody.categoryBitMask == bulletCategory) &&
(secondBody.categoryBitMask == enemyShipCategory)) {
[self gainPointsByAmoint:POINT_INCREMENTER];
[self projectile:(SKSpriteNode *)firstBody.node didCollideWithMonster:(SKSpriteNode *)secondBody.node];
}
}
This should work it's already tested and working
//Define the collider Category
typedef NS_ENUM(uint32_t, CollisionType) {
enemyShipCategory = 0x1 << 0,
enemyBulletCategory = 0x1 << 1,
playerShipCategory = 0x1 << 2,
bulletCategory = 0x1 << 3,
};
// Set the category that this physics body belongs to
// and specify which categories of bodies cause intersection
// notifications with this physics body
ship.physicsBody.categoryBitMask = playerShipCategory;
ship.physicsBody.contactTestBitMask = enemyBulletCategory;
shipBullet.physicsBody.categoryBitMask = bulletCategory;
shipBullet.physicsBody.contactTestBitMask = enemyShipCategory | enemyBulletCategory;
enemy.physicsBody.categoryBitMask = enemyShipCategory;
enemy.physicsBody.contactTestBitMask = bulletCategory;
enemyBullet.PhysicsBody.categoryBitMask = enemyBulletCategory;
enemyBullet.physicsBody.contactTestBitMask = bulletCategory;
// And handle Collisions
- (void)didBeginContact:(SKPhysicsContact *)contact {
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (playerShipCategory | enemyBulletCategory)) {
SKNode *Ship, *bullet;
if (contact.bodyA.categoryBitMask == playerShipCategory) {
Ship = contact.bodyA.node;
bullet = contact.bodyB.node;
} else {
Ship = contact.bodyB.node;
bullet = contact.bodyA.node;
}
[self takeDamageByAmount:POINT_INCREMENTER];
[bullet removeFromParent];
SKAction *bounce = [SKAction sequence:#[
[SKAction fadeAlphaTo:.5 duration:.2],
[SKAction fadeAlphaTo:1.0 duration:.2],
[SKAction fadeAlphaTo:.5 duration:.2],
[SKAction fadeAlphaTo:1.0 duration:.2]
]];
[Ship runAction:bounce];
}
else if (collision == (bulletCategory | enemyBulletCategory)) {
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent];
}
else if (collision == (bulletCategory | enemyShipCategory)) {
SKNode *shipBullet, *enemyShip;
if (contact.bodyA.categoryBitMask == shipBullet) {
shipBullet = contact.bodyA.node;
enemyShip = contact.bodyB.node;
} else {
shipBullet = contact.bodyB.node;
enemyShip = contact.bodyA.node;
}
[self gainPointsByAmoint:POINT_INCREMENTER];
[self projectile:shipBullet didCollideWithMonster:enemyShip];
}
}
Good Luck!!

Resources