【问题标题】:Simple combo multiplier in sprite-kitsprite-kit 中的简单组合乘数
【发布时间】:2016-05-01 04:14:15
【问题描述】:

我正在制作一个反应游戏,您可以在其中消灭敌人并获得积分。现在我想获得连击点数,如果你快速摧毁它们,并且如果有特定的时间间隔,连击乘数应该再次变为零。

我想把这样的点加倍:2 * 2 = 4 * 2 = 8 * 2 = 16 * 2... (如果你摧毁一个敌人,你会得到 2 分)。

我在这里加点:

if (CGRectIntersectsRect(enemy.frame, player.frame)) {
        points = points + 1;
        [enemy removeFromParent];
    }

我总是可以将当前点数乘以 2,但如果有特定时间量而没有获得点数,我想重置组合乘数。

我希望有人可以帮助我。 (目标 c 中的代码)

【问题讨论】:

  • 您可能会采用这样的方式:首先,定义您认为“快速”的内容。例如,跟踪最后一个敌人被摧毁的时间,如果下一个被摧毁的敌人之间的延迟小于 x 毫秒,则增加奖励乘数。第二部分将检查自上次摧毁敌人以来已经过去了多少时间,并在此基础上减少乘数。

标签: ios objective-c sprite-kit 2d-games


【解决方案1】:

这似乎并不比记录最后一个敌人被摧毁的时间更复杂,然后在 update: 方法中确定 combo 是否已经过去,因为在您允许的任何超时时间内没有更多的敌人被击中.

我不熟悉 Sprite 套件,但 update 似乎超过了当前时间;优秀。您需要记录以下内容:

  • timeout(时间):当前超时。这会随着游戏的进行而减少,变得更难。
  • lastEnemyKillTime(时间):最后一个敌人被击杀的时间。
  • comboPoints(整数):用户每次点击获得多少分。这将随着组合的扩展而增加。
  • points(整数):当前得分。

所以,是这样的:

@interface MyClass ()
{
    NSTimeInterval _timeout;
    NSTimeInterval _lastEnemyKillTime;
    BOOL _comboFactor;
    NSUInteger _points;

}
@end

我猜 Sprite Kit 使用了init: 方法;用它来初始化变量:

- (id)init
{
    self = [super init];
    if (self != nil) {
        _timeout = 1.0;
        _lastEnemyKillTime = 0.0;
        _points = 0;
        _comboPoints = 1;
    }
}

update: 方法类似于:

- (void)update:(NSTimeInterval)currentTime
{
    BOOL withinTimeout = currentTime - _lastEnemyKillTime <= _timeout;
    if (CGRectIntersectsRect(enemy.frame, player.frame)) {
        _inCombo = withinTimeout;
        if (_inCombo)
            _comboPoints *= 2;
        _points += _comboPoint;
        _lastEnemyKillTime = currentTime;
        [enemy removeFromParent];
    } else if (_comboPoints > 1 && !withinTimeout) {
        _lastEnemyKillTime = 0.0;
        _comboPoints = 1;
    }
}

【讨论】:

  • 谢谢!你能给我这个目标 c 中的代码吗?
  • @ccdev 那是 Objective-C :)
  • @ccdev 我已经更新了我的答案;这不是你所要求的。
  • @trojanfoe 我有 3 个问题: 1. 什么意思:BOOL insideTimeout = currentTime - _lastEnemyKillTime
  • @ccdev 1) 如果当前帧在您的超时期限内,它将生成withinTimeout = YES。 &lt;= 运算符返回一个布尔结果。以后会用它来做决定。 2) 在init: 方法中,但您也将随着时间的推移对其进行更改,以使update: 方法中的游戏更加困难,尽管我不清楚具体是如何完成的。 3) 它是在if 部分完成的,而不是在else 部分。
【解决方案2】:

您需要跟踪最后一个敌人的临时时间戳和因素。处理下一个杀戮时,检查时间戳,如果它低于阈值,则提高因子。当前杀死的时间代替了时间戳。

如果您还没有更好的地方(服务或某事),您可以创建一个 FightRecorder 类作为单例。

NSDate *newKillTime = new NSDate;

FightRecorder recorder = [FightRecorder instance];
if([newKillTime timeIntervalSinceDate:recorder.lastKillTime] < SCORE_BOUNDS_IN_SEC) {
    recorder.factor++; // could also be a method
    points = points + [recorder calculateScore];  // do your score math here
}
else {
    [recorder reset];  // set the inner state of the fight recorder to no-bonus
}

recorder.lastKillTime = newKillTime; // record the date for the next kill

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2014-01-13
    相关资源
    最近更新 更多