【问题标题】:scoring system in a sprite kit game精灵套件游戏中的计分系统
【发布时间】:2014-03-29 21:51:18
【问题描述】:

我正在使用 sprite kit 制作一个游戏,其中一个球落到一个人身上,我需要添加一个标签,每次球击中那个人时都会计数。我已经对联系人部分进行了排序,但我对如何制作标签并使其计数感到有点困惑。 GG

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKSpriteNode *firstNode, *secondNode;

    firstNode = (SKSpriteNode *)contact.bodyA.node;
    secondNode = (SKSpriteNode *)contact.bodyB.node;

    if ((contact.bodyA.categoryBitMask == manCategory)
    && (contact.bodyB.categoryBitMask == pooCategory))
    {
        CGPoint contactPoint = contact.contactPoint;

        float contact_y = contactPoint.y;
        float target_y = secondNode.position.y;
        float margin = secondNode.frame.size.height/2 - 25;

        if ((contact_y > (target_y - margin)) &&
        (contact_y < (target_y + margin)))
        {

        }
    }
}

【问题讨论】:

  • 您能否详细介绍一下您是如何处理联系工作的?
  • 基本上就是这样......

标签: ios iphone ios7 sprite-kit


【解决方案1】:

虽然您绝对仍然可以将 UILabel 和 UIButtons 用于 spriteKit,但他们似乎希望您使用 SKLabelNode 代替。

如何完全设置 SKLabelNode 的示例可能如下所示:

@property SKLabelNode* deathLabel;

然后

deathLabel = [SKLabelNode labelNodeWithFontNamed:@"CoolveticaRg-Regular"];
deathLabel.fontSize = 16;
deathLabel.text = [NSString stringWithFormat:@"%d Deaths",deaths];
deathLabel.position = CGPointMake(self.size.width/2,labelHeight);
deathLabel.fontColor = [SKColor whiteColor];
deathLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
[hudLayer addChild:deathLabel];

然后当您需要更新它时,只需重复该行:

deathLabel.text = [NSString stringWithFormat:@"%d Deaths",deaths];

我注意到一个奇怪的地方是 SKLabelNodes 默认使用上次使用的任何字体、fontSize、fontColor、horizo​​ntalAlignmentMode 和 verticalAlignmentMode,这介于方便和令人沮丧之间,具体取决于您希望标签的多样性。

【讨论】:

    【解决方案2】:

    试试这个

    创建一个 UILabel 属性...

    @property (nonatomic, strong) UILabel *coreLabel;
    

    构建它...

    scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(...)];
    scoreLabel.text = @"0";
    [self.view addSubview:scoreLabel];
    

    ...

    更新 UIlabel...

    if ((contact_y > (target_y - margin)) && (contact_y < (target_y + margin)))
        {
            int temp = [scoreLabel intValue];
            temp += 1;
            scoreLabel.text = [NSString stringWithFormat:@"%i", temp];
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多