gpt4 book ai didi

ios - 未检测到 SpriteKit 碰撞

转载 作者:行者123 更新时间:2023-11-29 02:59:52 26 4
gpt4 key购买 nike

我的场景中有 2 个 SKSpriteNode,我想检测它们之间的碰撞。但是,这不起作用。这是相关代码

节点 1:

 PSCarNode *newCar = [PSCarNode new];
newCar.size = CGSizeMake(CAR_WIDTH, CAR_HEIGHT);
CGFloat carXval = - CAR_WIDTH; // start off-screen
newCar.position = CGPointMake(carXval, Yval);
newCar.zPosition = 5;
newCar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(CAR_WIDTH, CAR_HEIGHT)];
newCar.physicsBody.dynamic = YES;
newCar.physicsBody.categoryBitMask = carCategory;
newCar.physicsBody.contactTestBitMask = frogCategory;
newCar.physicsBody.collisionBitMask = 0;
newCar.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:newCar];

节点 2:

    player = [[PSPLAYERNode alloc] init];
[player setPosition: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
[player setSize:CGSizeMake(PLAYER_SIZE, PLAYER_SIZE)];
player.zPosition = 1;
player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(3/4)*PLAYER_SIZE];
player.physicsBody.dynamic = YES;
player.physicsBody.collisionBitMask = 1;
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.contactTestBitMask = carCategory;
[self addChild:player];

初始化:

    - (instancetype)initWithSize:(CGSize)size
{
self = [super initWithSize:size];
if (self) {
[self createScene];
self.physicsWorld.gravity = CGVectorMake(0., 0.); // no gravity
self.physicsWorld.contactDelegate = self;
animationInProgress = NO;
}
return self;
}

和委托(delegate):

    - (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"player hit");
}

为什么没有检测到碰撞。我可以清楚地看到汽车节点在玩家节点上移动,但日志中没有显示任何内容。

最佳答案

因为这两个对象属于两个不同的类别,并且它们的接触位掩码组合起来也为 0。

只是根本不要设置位掩码,直到您真正需要它们来定义两个(一组)物体根本不应该接触或应该产生接触事件但不解决碰撞(即允许它们通过每个物体)的行为其他)。

您将在 SKPhysicsBody class reference 中找到有关位掩码如何工作的信息。 .

关于ios - 未检测到 SpriteKit 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com