sprite-kit 中的简单组合乘数
Simple combo multiplier in sprite-kit
我正在制作一个反应游戏,你可以在其中消灭敌人并获得积分。现在,如果你快速摧毁它们,我想要获得连击点数,如果有特定的时间间隔,连击乘数应该再次归零。
我想像这样乘以多个点:2 * 2 = 4 * 2 = 8 * 2 = 16 * 2 ...
(如果你消灭了一个敌人,你会得到 2 分)。
我在这里补充几点:
if (CGRectIntersectsRect(enemy.frame, player.frame)) {
points = points + 1;
[enemy removeFromParent];
}
我总是可以将当前点数乘以 2,但如果有特定时间没有获得点数,我想重置组合乘数。
希望有人能帮助我。
(请在 objective c 中输入代码)
你需要跟踪最后一个敌人的休闲时间戳和因素。当处理下一个 kill 时,你检查时间戳,如果它低于阈值,你提高因子。当前kill的时间替换时间戳。
如果您还没有更好的地方(服务或其他),您可以创建一个 FightRecorder
class 作为单身人士。
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
这似乎并不比记录最后一个敌人被消灭的时间复杂,然后在 update:
方法中决定 combo 是否已经过去,因为没有更多的敌人被消灭在您允许的任何超时时间内命中。
我对Sprite kit不熟悉,但是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;
}
}
我正在制作一个反应游戏,你可以在其中消灭敌人并获得积分。现在,如果你快速摧毁它们,我想要获得连击点数,如果有特定的时间间隔,连击乘数应该再次归零。
我想像这样乘以多个点:2 * 2 = 4 * 2 = 8 * 2 = 16 * 2 ... (如果你消灭了一个敌人,你会得到 2 分)。
我在这里补充几点:
if (CGRectIntersectsRect(enemy.frame, player.frame)) {
points = points + 1;
[enemy removeFromParent];
}
我总是可以将当前点数乘以 2,但如果有特定时间没有获得点数,我想重置组合乘数。
希望有人能帮助我。 (请在 objective c 中输入代码)
你需要跟踪最后一个敌人的休闲时间戳和因素。当处理下一个 kill 时,你检查时间戳,如果它低于阈值,你提高因子。当前kill的时间替换时间戳。
如果您还没有更好的地方(服务或其他),您可以创建一个 FightRecorder
class 作为单身人士。
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
这似乎并不比记录最后一个敌人被消灭的时间复杂,然后在 update:
方法中决定 combo 是否已经过去,因为没有更多的敌人被消灭在您允许的任何超时时间内命中。
我对Sprite kit不熟悉,但是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;
}
}