gpt4 book ai didi

swift - SKshapenode 没有响应 Physicsbody

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:25 26 4
gpt4 key购买 nike

我创建了一个 SKShapeNode 并为其分配了一个 physicsBody。但是,当有接触时它不会被触发。

创建SKShapeNode代码:

-(SKShapeNode*)gravityline{
//SKSpriteNode *lolo=[[SKSpriteNode alloc]init];
SKShapeNode *lolo = [[SKShapeNode alloc] init];
CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, fff.x, fff.y);
CGPathAddLineToPoint(path, 0,rayoriginpoint.x,rayoriginpoint.y );
CGPathCloseSubpath(path);
lolo.path = path;
lolo.name=@"gravityline";
lolo.strokeColor=[SKColor greenColor];
lolo.glowWidth=.1;
CGPathRelease(path);
lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];
//lolo.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromPath:path];
//lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];
lolo.physicsBody.categoryBitMask=raylightCategory;
lolo.physicsBody.collisionBitMask=batCategory;
lolo.physicsBody.contactTestBitMask=batCategory;
lolo.physicsBody.usesPreciseCollisionDetection=YES;
lolo.physicsBody.linearDamping=0;
lolo.physicsBody.restitution=1.0;
lolo.physicsBody.dynamic=NO;

return lolo;
}

这是触发代码:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if (firstBody.categoryBitMask == raylightCategory && secondBody.categoryBitMask==batCategory)
{
NSLog(@"Contact with bat have been made");
[secondBody.node removeFromParent];
}
}

如果有人知道我做错了什么,为什么 SKShapeNode 没有激活 physicsBody,请告诉我。

最佳答案

这肯定行不通:

lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];

如果有的话,这将返回一个已经分配给不同节点的主体。但我猜它只是返回 nil。

这条注释行也不起作用:

//lolo.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromPath:path];

边缘形状将创建静态(如:不可移动的)物体。因此,该节点不会在物理中移动,如果我没记错的话,您也不会从与静态物体的接触中获得接触响应,只有动态物体。

这个应该可以工作:

//lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];

但是你在这里将 body 设置为静态 body :

lolo.physicsBody.dynamic=NO;

因此,适用相同的规则,就好像您正在创建带边循环的主体一样。

关于swift - SKshapenode 没有响应 Physicsbody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882339/

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