Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么精灵套件会在这里造成碰撞?_Ios_Sprite Kit - Fatal编程技术网

Ios 为什么精灵套件会在这里造成碰撞?

Ios 为什么精灵套件会在这里造成碰撞?,ios,sprite-kit,Ios,Sprite Kit,我有一个文件,Categories.h,它为我的SpriteKit游戏定义了类别位掩码。里面是这样的: #ifndef BallRoll_Categories_h #define BallRoll_Categories_h static u_int8_t ballCategory = 2; static u_int8_t collideCategory = 6; static u_int8_t contactCategory = 4; #endif 我还有一个名为BRPowerup的类,它

我有一个文件,
Categories.h
,它为我的SpriteKit游戏定义了类别位掩码。里面是这样的:

#ifndef BallRoll_Categories_h
#define BallRoll_Categories_h

static u_int8_t ballCategory = 2;
static u_int8_t collideCategory = 6;
static u_int8_t contactCategory = 4;

#endif
我还有一个名为
BRPowerup
的类,它的实现中有以下方法:

-(id)initWithType:(int)type
{
    if(self = [super init])
    {
        self.powerupType = type;
        self.size = CGSizeMake(32, 32);
        self.texture = [SKTexture textureWithImageNamed:sprites[self.powerupType]];
        self.name = @"powerup";
        self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width / 2];
        self.physicsBody.dynamic = NO;
        self.physicsBody.affectedByGravity = NO;
        self.physicsBody.categoryBitMask = contactCategory;
        self.physicsBody.collisionBitMask = 0;
        self.physicsBody.contactTestBitMask = ballCategory;
        NSLog(@"init");
    }
    return self;
}
BRMyScene.h
内部,此代码在
initWithSize:
方法中运行

self.ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];
        self.ball.position = CGPointMake(100, 100);
        self.ball.size = CGSizeMake(32, 32);
        self.ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.ball.size.width / 2];
        self.ball.physicsBody.density = .7;
        self.ball.physicsBody.restitution = .25;
        self.ball.physicsBody.categoryBitMask = ballCategory;
self.ball.physicsBody.contactTestBitMask = contactCategory;
        self.ball.physicsBody.collisionBitMask = collideCategory;
        self.ball.name = @"ball";
        [self addChild:self.ball];
接触被完美检测到,但球仍然与
碰撞类别之外的物体碰撞,并从通电状态反弹。它还与碰撞类别之外的其他事物发生碰撞

如果需要更多代码,请留下评论


谢谢你的帮助!提前感谢您的回答。

您已声明您的类别掩码如下-

static u_int8_t ballCategory = 2;    // 00000010
static u_int8_t collideCategory = 6; // 00000110
static u_int8_t contactCategory = 4; // 00000100
似乎碰撞类别等于球类别|接触类别

self.ball.physicsBody.collisionBitMask=collideCategory将球设置为与ballCategorycontactCategory的其他物理实体碰撞

通过给它自己的唯一位,如000011000,它等于8,使它成为自己的类别。尝试使用8而不是6

最后一点需要注意的是,根据您的声明,我认为位掩码应该是32位(uint32_t)掩码,而不是8位(u_int8_t)。试试这个-

static const uint32_t ballCategory = 0x1 << 1;    // 00000010 (last 8 bits of 32bit mask)
static const uint32_t contactCategory = 0x1 << 2; // 00000100
static const uint32_t collideCategory = 0x1 << 3; // 00001000

static const uint32\u t ballcontegory=0x1它可以工作!非常感谢。不过,我有一个问题。我不是这样的位掩码天才,但我理解二进制和其他数学基础。我相信对32位数字的评论是8位的,因为32位数字小到可以成为8位数字,经过一些搜索,我发现了类别之间发生冲突的原因。我的问题是:32位数字也会发生这种冲突吗?还有,为什么32位数字每次都要加倍,而不是增加1?提前谢谢@JoeeElectric只有32种可能的类别,每一位1个。苹果公司的文档中写道:“每当潜在的交互发生时,每个物体的类别掩码都会与另一个物体的接触和碰撞掩码进行测试。Sprite Kit通过将两个掩码逻辑地相加来执行这些测试。如果结果是非零数,则会发生交互。”二进制中的数字“6”是00000110,所以当你用“2”或“4”时,你会得到一个非零的数字,并且会发生交互作用。