您好我想知道我的节点如何与移动的场景发生碰撞。我有一个主节点,可以沿x轴移动,并可以与其他节点发生冲突。我想让非主要节点与移动场景的两侧发生碰撞。我该怎么做? (摄像机仅跟随主节点)。
我有一个障碍课。
在我的MLObstacle.m
中-(id)init
{
if(self = [super init])
{
int aux = arc4random()%3;
switch (aux) {
case 0:
self = [MLObstaculo spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake((aux+1)*10,(aux+1)*13 )];
break;
case 1:
self = [MLObstaculo spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake((aux+1)*10,(aux+1)*15 )];
break;
case 2:
self = [MLObstaculo spriteNodeWithColor:[UIColor redColor] size:CGSizeMake((aux+1)*10,(aux+1)*14 )];
break;
}
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.categoryBitMask = obstacleCategory;
self.physicsBody.contactTestBitMask = visibleSidesCategory;
self.physicsBody.collisionBitMask = visibleSidesCategory;
[self setDx:0.0];
[self setDy:2.4];
}
return self;
}
所以我向我的WorldClass添加了障碍
在我的WorlGen.m
中-(void)GenerateWorld
{
SKSpriteNode *Ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width,100)];
Ground.position = CGPointMake(0, -self.scene.frame.size.height/2 + Ground.size.height/2);
Ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Ground.size];
Ground.physicsBody.dynamic = NO;
[self addChild:Ground];
for(int i = 0; i < 3; i++)
{
MLObstaculo * auxobs= [[MLObstaculo alloc]initObstaculo];
auxobs.position = CGPointMake((i+1)*50, Ground.position.y + Ground.size.height/2 +auxobs.size.height/2 );
[self.Obstaculos addObject:auxobs];
[self addChild:auxobs];
auxobs = NULL;
}
}
然后我将世界添加到我的场景
Scene.m
-(id)initWithSize:(CGSize)size
{
if(self = [super initWithSize:size]
{
WorldGen * world = [[WorldGen alloc] init];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.contactDelegate = self;
self.physicsBody.categoryBitMask = VisibleSidesCategory
self.physicsBody.collisionBitMask = ObstacleCategory;
self.anchorPoint = CGPointMake(0.5,0.5);
[self addChild:world];
[world GenerateWorld];
}
}
...更新
-(void)didSimulatePhysics
{
[self centerOnNode:hero];
}
-(void)centerOnNode:(SKNode *)node
{
CGPoint pointinScene = [self convertPoint:node.position fromNode:node.parent];
world.position = CGPointMake(world.position.x -pointinScene.x , world.position.y);
}
我使用这种格式(static const uint32_t ObstacleCategory = 0x1 <<1;
)
答案 0 :(得分:1)
首先通过定义collisionBitMask
进行编辑-(id)init
{
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = ObstacleCategory;
self.physicsBody.contactTestBitMask = VisibleSidesCategory;
self.physicsBods.collisionBitMask = VisibleSidesCategory;
}
其次在Xcode中执行此步骤 - &gt; 文件 - &gt; 新 - &gt; 文件 - &gt; C和C ++ - &gt; 标题文件
给出Physics.h的名称并在其中添加这些行:
typedef enum : uint8_t {
ObstacleCategory = 1,
VisibleSidesCategory = 2,
} ColliderType;
然后将Physics.h 导入所有类(您不需要为Physics.h分配或初始化,只需使用ObstacleCategory和VisibleSidesCategory)
答案 1 :(得分:0)
从这个和另一个问题的问题,我终于确定API中存在错误。具体地,如果使用基于WithRect:
或FromRect:
的初始化器(但不是基于RectOfSize:
或center:
的初始化器,则在初始化期间将节点的anchorPoint视为(0,0)一),所以你得到意想不到的行为。
因此,即使将场景的anchorPoint和其他对象设置为(0.5,0.5),节点也会将其左下角相对于父节点的中点放置,而不是将其置于子节点的中间位置。相对于父节点的中点的中点。
具体而言,这可以通过以下三种方式中的任何一种来解决:
shapeNodeWithRectOfSize:
代替shapeNodeWithRect:
和bodyWithEdgeLoopFromPath:
代替bodyWithEdgeLoopFromRect:
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(-node.size.width/2, -node.size.height/2, node.size.width, node.size.height)];