我试图在我的游戏中测试碰撞,这是一个Brick Breaker类型的游戏,在子弹和特定类型的砖之间。当子弹击中砖块时,然后移除砖块。我可以测试任何砖块和子弹之间的碰撞,并将它们移除。但我不能让碰撞在特定类型的砖之间工作,然后移除那块砖。
这是我的Brick.h课程
#import <SpriteKit/SpriteKit.h>
typedef enum : NSUInteger {
// Normal Bricks
Blue = 1,
LightBlue = 2,
DarkBlue = 3,
Green = 4,
LightGreen = 5,
DarkGreen = 6,
Grey = 7,
DarkGrey = 8,
Pink = 9,
LightPink = 10,
Purple = 11,
LightPurple = 12,
DarkPurple = 13,
Red = 14,
DarkRed = 15,
Orange = 16,
Gold = 17,
Yellow = 18,
Teal = 19,
Brown = 20,
} BrickType;
static const uint32_t BRICK_CATEGORY = 0x1 << 2;
@interface XSBrick : SKSpriteNode
@property (nonatomic) BrickType type;
@property (nonatomic) BOOL indestructible;
@property (nonatomic) BOOL spawnsExtraBall;
@property (nonatomic) BOOL givesExtraLife;
@property (nonatomic) BOOL enlargePaddle;
@property (nonatomic) BOOL spawnsTwoOrThreeExtraBalls;
@property (nonatomic) BOOL makesBallSlow;
@property (nonatomic) BOOL makesBallFast;
@property (nonatomic) BOOL bullets;
-(instancetype)initWithType:(BrickType)type;
-(void)hit;
@end
Brick.m类
#import "XSBrick.h"
@implementation XSBrick
{
SKAction *_brickSmashSound;
}
#pragma mark - Load Bricks
-(instancetype)initWithType:(BrickType)type
{
switch (type) {
// Normal Bricks
case Blue:
self = [super initWithImageNamed:@"BrickBlue"];
break;
case LightBlue:
self = [super initWithImageNamed:@"BrickLightBlue"];
break;
case DarkBlue:
self = [super initWithImageNamed:@"BrickDarkBlue"];
break;
case Green:
self = [super initWithImageNamed:@"BrickGreen"];
break;
case DarkGreen:
self = [super initWithImageNamed:@"BrickDarkGreen"];
break;
case LightGreen:
self = [super initWithImageNamed:@"BrickLightGreen"];
break;
case Grey:
self = [super initWithImageNamed:@"BrickGrey"];
break;
case DarkGrey:
self = [super initWithImageNamed:@"BrickDarkGrey"];
break;
case Pink:
self = [super initWithImageNamed:@"BrickPink"];
break;
case LightPink:
self = [super initWithImageNamed:@"BrickLightPink"];
break;
case Purple:
self = [super initWithImageNamed:@"BrickPurple"];
break;
case LightPurple:
self = [super initWithImageNamed:@"BrickLightPurple"];
break;
case DarkPurple:
self = [super initWithImageNamed:@"BrickDarkPurple"];
break;
case Red:
self = [super initWithImageNamed:@"BrickRed"];
break;
case DarkRed:
self = [super initWithImageNamed:@"BrickDarkRed"];
break;
case Orange:
self = [super initWithImageNamed:@"BrickOrange"];
break;
case Gold:
self = [super initWithImageNamed:@"BrickGold"];
break;
case Yellow:
self = [super initWithImageNamed:@"BrickYellow"];
break;
case Teal:
self = [super initWithImageNamed:@"BrickTeal"];
break;
case Brown:
self = [super initWithImageNamed:@"BrickBrown"];
break;
default:
self = nil;
break;
}
return self;
}
在MyScene.m中,didBeginContact。这可以工作但删除子弹击中的任何类型的砖
// Collision between bullets and bricks
if (firstBody.categoryBitMask == BRICK_CATEGORY && secondBody.categoryBitMask == BULLET_CATEGORY) {
[firstBody.node removeFromParent];
[secondBody.node removeFromParent];
}
我试过了,但是Xcode给了我一个关于SKNode和NSUInteger之间比较的警告,所以它没有用。
// Collision between bullets and bricks
if (firstBody.categoryBitMask == BRICK_CATEGORY && secondBody.categoryBitMask == BULLET_CATEGORY) {
if (firstBody.node == LightBlue) {
[firstBody.node removeFromParent];
}
[secondBody.node removeFromParent];
}
我也尝试过创建我的Brick的一个实例然后针对它进行测试,但这也不起作用。
// Collision between bullets and bricks
if (firstBody.categoryBitMask == BRICK_CATEGORY && secondBody.categoryBitMask == BULLET_CATEGORY) {
XSBrick *LightBlueBrick = [[XSBrick alloc]initWithType:LightBlue];
if (firstBody.node == LightBlueBrick) {
[firstBody.node removeFromParent];
}
[secondBody.node removeFromParent];
}
我已经有一段时间了,现在试图让它发挥作用。另外,我对iOS的开发还很陌生,所以任何帮助都会非常感激。
修改
我尝试了你的答案,rickster,但它犯了一个错误,“2014-07-02 09:13:52.527 Brick Breaker [44129:60b] - [SKSpriteNode type]:无法识别的选择器发送到实例0x12145a2a0”。但我确实使用你提供的一些代码让它工作。
// Collision between bullets and bricks
if (firstBody.categoryBitMask == BRICK_CATEGORY && secondBody.categoryBitMask == BULLET_CATEGORY) {
XSBrick *brick = nil;
SKNode *bullets = nil;
if ([firstBody.node isKindOfClass:[XSBrick class]]) {
brick = firstBody.node;
bullets = secondBody.node;
if (brick.type == LightBlue) {
[firstBody.node removeFromParent];
}
else if (brick.type == Blue) {
[firstBody.node removeFromParent];
}
}
[secondBody.node removeFromParent];
}
这现在有效。它会检测到什么类型的砖被击中,然后移除该砖。如果它不在我的if语句中,它就不会被删除,这就是我想要的方式。非常感谢你的帮助!!
答案 0 :(得分:1)
if (firstBody.node == LightBlue)
无法正常工作,因为您正在将节点(对象指针)与枚举值(整数)进行比较。您需要比较相同类型的值以进行相等比较,以获得任何有用的含义。
if (firstBody.node == LightBlueBrick)
无法正常工作,因为您正在针对您在那里创建的XSBrick
实例进行测试。您已经在didBeginContact
,因此已经存在的一个节点参与了碰撞 - 如果您创建了一个新节点,那么该节点将保证不会是那个节点。已经存在。您只需要检查碰撞中每个节点的属性,以找出它是哪个节点或哪种节点。
您已经有一个if
语句,用于测试firstBody.categoryBitMask
是否与您用于砖块的值相匹配。如果您确定将仅值用于砖块,则可以放心地假设firstBody.node
指向XSBrick
的实例。 (但安全的做法是使用isKindOfClass
来确定。)
要让编译器调用XSBrick
方法,您需要将firstBody.node
强制转换为该类类型。然后,您可以查看您在type
上定义的XSBrick
媒体资源。
最后,SpriteKit并不能保证联系人处理程序中主体的顺序,所以在砖块和子弹之间碰撞的第一个主体是砖块时,它永远不会是真的。
这些是解决此问题所需的成分。将它们放在一起,这是联系处理程序的示例实现:
- (void)didBeginContact:(SKPhysicsContact *)contact {
// If we expect more than one kind of collision, check that first
if (contact.bodyA.categoryBitMask | contact.bodyA.categoryBitMask == BRICK_CATEGORY | BULLET_CATEGORY)
// We know it's a brick/bullet collision, sort out which body is which
XSBrick *brick = nil;
SKNode *bullet = nil;
if ([contact.bodyA.node isKindOfClass:[XSBrick class]]) {
brick = contact.bodyA.node;
bullet = contact.bodyB.node;
} else {
brick = contact.bodyB.node;
bullet = contact.bodyA.node;
}
// Now, check the type of brick
if (brick.type == LightBlue) {
// Do whatever with light blue bricks
} else {
// Do whatever with other kinds of bricks
}
}
}