我的didBeginContact方法应该在有球撞击时删除我的积木精灵。但是,它没有尽力而为。当我运行它时,当我敲击桨板和击中砖块时,我会得到NSLog“桨板”。我已多次查看代码,有人可以告诉我这里有什么内容吗?
@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
最佳答案
我知道发生了什么事。我不小心把第一个代码段中的非球对象声明为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");
}
}
此代码已解决问题:
-(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");
}
}
关于ios - didBeginContact方法不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27005831/