【问题标题】:How can I implement a SKPhysicsBody object in another SKPhysicsBody object?如何在另一个 SKPhysicsBody 对象中实现 SKPhysicsBody 对象?
【发布时间】:2014-04-22 08:30:22
【问题描述】:

场景是:一个玩家和一个盒子,玩家在盒子里。当我在屏幕上向右滑动时,播放器会向右运行。当他到达盒子的右边缘时,他会将盒子推到右边。

所以我设计了两个SKNode

    CGRect box = CGRectMake(110, 270, 100, 100);
    SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
    shapeNode.path = [UIBezierPath bezierPathWithRect:box].CGPath;
    shapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:shapeNode.path];
    shapeNode.physicsBody.dynamic = NO;
    shapeNode.physicsBody.categoryBitMask = kbox;
    shapeNode.physicsBody.collisionBitMask = 1;
    shapeNode.physicsBody.contactTestBitMask = kplayer;
    [self addChild:shapeNode];

    SKSpriteNode *player = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor]   size:CGSizeMake(10, 30)];
    player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
    player.physicsBody.categoryBitMask = kplayer;
    player.physicsBody.collisionBitMask = 1;
    player.physicsBody.contactTestBitMask = kbox;
    player.position = CGPointMake(self.size.width/2, self.size.height/2);
    [self addChild:player];

上述初始代码不起作用,shapeNode 无法与播放器发生碰撞。如何在另一个 SKPhysicsBody 对象中实现 SKPhysicsBody 对象?我怎样才能实现我的想法?非常感谢!

【问题讨论】:

  • 尝试使用shapeNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:box];
  • @akashg 我试过了,但是没用。

标签: ios7 sprite-kit game-physics skphysicsbody


【解决方案1】:

您可以在盒子的(内)侧添加一个子节点,并检查玩家与内节点之间的联系。建立联系后,只要已注册联系,就手动移动父(框)。您可以使用box.position = CGPointMake(box.position.x, box.position.y+1); 之类的方式移动该框。这将在update: 方法中完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多