我的didBeginContact方法应该在球击中时删除我的砖精灵。然而,它没有做好自己的工作。当我运行它时,我得到了NSLog" paddle"当我击中桨和我击中砖时。我已经多次查看了代码,有人可以告诉我这里有什么加入吗?
@interface GameScene ()
@property (nonatomic) SKSpriteNode *paddle;
@end
static const uint32_t ballCategory = 1; //00000000000000000000000000000001
static const uint32_t brickCategory = 2; //00000000000000000000000000000010
static const uint32_t paddleCategory = 4; //00000000000000000000000000000100
static const uint32_t edgeCategory = 8; //00000000000000000000000000001000
static const uint32_t bottomEdgeCategory = 16; //00000000000000000000000000010000
@implementation GameScene
-(void)didBeginContact:(SKPhysicsContact *)contact {
//create placeholder for the "non ball" object
SKPhysicsBody *notTheBall;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
notTheBall = contact.bodyB;
} else {
notTheBall = contact.bodyB;
}
if (notTheBall.categoryBitMask == brickCategory) {
NSLog(@"brick");
[notTheBall.node removeFromParent];
} else {
NSLog(@"paddle");
}
}
- (void) addBall:(CGSize)size {
//create sprite with image
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
//resize sprite
ball.size = CGSizeMake(33, 33);
//position it
ball.position = CGPointMake(size.width/2, size.height/2);
//give physics to ball
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
ball.physicsBody.friction = 0;
ball.physicsBody.linearDamping = 0;
ball.physicsBody.restitution = 1.0f;
ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.contactTestBitMask = brickCategory | paddleCategory;
//add sprite to scene
[self addChild:ball];
//create vector
CGVector myVector = CGVectorMake(20, 20);
//apply vector to ballphysics body
[ball.physicsBody applyImpulse:myVector];
}
- (void) addBricks:(CGSize)size {
for (int i = 0; i < 5; i++) {
//create brick sprite from image
SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:@"brick"];
//resize bricks
brick.size = CGSizeMake(60, 30);
//psoition bricks
int xPos = size.width/5 * (i+.5);
int yPos = size.height - 25;
brick.position = CGPointMake(xPos, yPos);
brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:brick.frame.size];
brick.physicsBody.dynamic = NO;
brick.physicsBody.categoryBitMask = brickCategory;
//add bricks to scene
[self addChild:brick];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint newPosition = CGPointMake(location.x, 100);
//stop the paddle from going too far
if (newPosition.x < self.paddle.size.width/ 2) {
newPosition.x = self.paddle.size.width/ 2;
}
if (newPosition.x > self.size.width - (self.paddle.size.width/ 2)) {
newPosition.x = self.size.width - (self.paddle.size.width/ 2);
}
self.paddle.position = newPosition;
}
}
-(void) addPlayer:(CGSize)size {
//create player paddle
self.paddle = [SKSpriteNode spriteNodeWithImageNamed:@"paddle"];
//resize paddle
self.paddle.size = CGSizeMake(125, 31.25);
//position it
self.paddle.position = CGPointMake(size.width/2, 100);
//add a physics body
self.paddle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.paddle.frame.size];
//make it static
self.paddle.physicsBody.dynamic = NO;
self.paddle.physicsBody.categoryBitMask = paddleCategory;
//add sprite paddle
[self addChild:self.paddle];
}
-(instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]){
self.backgroundColor = [SKColor colorWithRed:(29.0f/255) green:(29.0f/255) blue:(29.0f/255) alpha:1.0];
//change gravity
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsWorld.contactDelegate = self;
//add physics body to scene
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = edgeCategory;
[self addPlayer:size];
[self addBricks:size];
[self addBall:size];
}
return self;
}
@end
答案 0 :(得分:1)
我弄清楚发生了什么。我不小心把非球对象在第一个代码片段中声明为bodyB,如果bodyA.categoryBitMask高于bodyB.categoryBitMask,我真的需要bodyA为非球对象。
以下代码是问题所在。
-(void)didBeginContact:(SKPhysicsContact *)contact {
//create placeholder for the "non ball" object
SKPhysicsBody *notTheBall;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
notTheBall = contact.bodyB;
} else {
notTheBall = contact.bodyB;
}
if (notTheBall.categoryBitMask == brickCategory) {
NSLog(@"brick");
[notTheBall.node removeFromParent];
} else {
NSLog(@"paddle");
}
}
&#13;
此代码修复了问题:
-(void)didBeginContact:(SKPhysicsContact *)contact {
//create placeholder for the "non ball" object
SKPhysicsBody *notTheBall;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
notTheBall = contact.bodyB;
} else {
notTheBall = contact.bodyA;
}
if (notTheBall.categoryBitMask == brickCategory) {
NSLog(@"brick");
[notTheBall.node removeFromParent];
} else {
NSLog(@"paddle");
}
}
&#13;