无论我做什么,我似乎无法让我的代码记下一次碰撞。没有任何Nslogs输出任何东西所以我知道代码甚至从未注意到碰撞。我尝试了很多尺寸的图像,甚至尝试多次更改节点类型类别,但仍然没有。我在这里做错了什么?
#import "JKGPlayer.h"
#import "JKGMyScene.h"
static const uint32_t firstNodeTypeCategory = 0x1 << 1;
static const uint32_t secondNodeTypeCategory = 0x1 << 2;
// 1
@interface JKGMyScene () <SKPhysicsContactDelegate>
@property (nonatomic) JKGPlayer * player;
@property (nonatomic) SKSpriteNode * plat2;
@property (nonatomic) SKSpriteNode * a1;
@property (nonatomic) SKSpriteNode * a2;
@end
@implementation JKGMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
// 2
NSLog(@"Size: %@", NSStringFromCGSize(size));
// 3
self.backgroundColor = [SKColor colorWithRed:1.0 green:.50 blue:1.0 alpha:1.0];
// 4
_plat2 = [SKSpriteNode spriteNodeWithImageNamed:@"twa1.png"];
_plat2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_plat2.size];
_plat2.position = CGPointMake(500, 500);
_plat2.zPosition = 15;
_plat2.physicsBody.dynamic = NO;
_plat2.physicsBody.usesPreciseCollisionDetection = YES;
_plat2.physicsBody.friction = 0;
_plat2.name = @"plat2";
_plat2.physicsBody.categoryBitMask = secondNodeTypeCategory;
_plat2.physicsBody.contactTestBitMask = firstNodeTypeCategory;
_plat2.physicsBody.collisionBitMask = secondNodeTypeCategory;
[self addChild:_plat2];
_a1 = [SKSpriteNode spriteNodeWithImageNamed:@"sun.png"];
_a1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_a1.size];
_a1.position = CGPointMake(500, 450);
_a1.zPosition = 15;
_a1.physicsBody.dynamic = NO;
_a1.physicsBody.usesPreciseCollisionDetection = YES;
_a1.physicsBody.friction = 0;
_a1.name = @"plat2";
_a1.physicsBody.categoryBitMask = firstNodeTypeCategory;
_a1.physicsBody.contactTestBitMask = secondNodeTypeCategory;
_a1.physicsBody.collisionBitMask = firstNodeTypeCategory;
[self addChild:_a1];
_a2 = [SKSpriteNode spriteNodeWithImageNamed:@"sun.png"];
_a2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_a2.size];
_a2.position = CGPointMake(500, 500);
_a2.zPosition = 15;
_a2.physicsBody.dynamic = NO;
_a2.physicsBody.usesPreciseCollisionDetection = YES;
_a2.physicsBody.friction = 0;
_a2.name = @"plat2";
_a2.physicsBody.categoryBitMask = secondNodeTypeCategory;
_a2.physicsBody.contactTestBitMask = firstNodeTypeCategory;
_a2.physicsBody.collisionBitMask = secondNodeTypeCategory;
[self addChild:_a2];
self.player = [[JKGPlayer alloc] initWithImageNamed:@"walkright1"];
self.player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.player.size];
self.player.position = CGPointMake(500, 550);
self.player.zPosition = 15;
self.player.physicsBody.dynamic = NO;
self.player.physicsBody.usesPreciseCollisionDetection = YES;
self.player.physicsBody.friction = 0;
self.player.name = @"player";
self.player.physicsBody.categoryBitMask = firstNodeTypeCategory;
self.player.physicsBody.contactTestBitMask = secondNodeTypeCategory;
self.player.physicsBody.collisionBitMask = firstNodeTypeCategory;
[self addChild:self.player];
self.userInteractionEnabled = YES;
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
}
return self;
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
}
- (void)update:(NSTimeInterval)currentTime {
self.player.desiredPosition = CGPointMake(self.player.position.x, self.player.position.y-1);
self.player.position = self.player.desiredPosition;
self.player.onGround = NO;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
int x = self.player.position.x-5;
int y = self.player.position.y-5;
CGPoint A = CGPointMake(x, y);
self.player.position = A;
// 1 - Choose one of the touches to work with
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
}
- (void)projectile:(SKSpriteNode *)projectile didCollideWithMonster:(SKSpriteNode *)monster {
NSLog(@"Hit");
}
- (void)didBeginContact:(SKPhysicsContact *)contact{
NSLog(@"AAAAAA");
// 1
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
// 2
if ((firstBody.categoryBitMask & firstNodeTypeCategory) != 0 &&
(secondBody.categoryBitMask & secondNodeTypeCategory) != 0)
{
[self projectile:(SKSpriteNode *) firstBody.node didCollideWithMonster:(SKSpriteNode *) secondBody.node];
}
}