我有我的第一个游戏应用程序正在开发中。在这个游戏中,用户控制的唯一角色将从一个街区跳到另一个街区。这就像马里奥(在马里奥兄弟中)从一个移动电梯跳到另一个。如果他失败了,他就会死。那么,你怎么能从成功跳跃的短暂跌落中分辨出自由落体呢?我认为我可以做的一件事是测量角色的垂直速度。所以我有以下几行代码。它与didSimulatePhysics一起使用
SKNode *player = [self childNodeWithName:@"//player"]; // It's the node characterizing the game character
CGVector v = player.physicsBody.velocity;
if (v.dy < -2000) {
[self endTheScene:kEndReasonLose]; // The character has died from free fall => game is over
}
当游戏角色跳跃时,游戏应用程序可以记录-2022.466797的垂直速度。所以这个措施是行不通的。我还可以做些什么?设置一个不可见的条,看看游戏角色是否用intersectsNode触摸过它?那也可能失败。我以前从未开发过游戏。所以我不知道他们是怎么做到的,这让我意识到任天堂游戏开发者是多么令人印象深刻。30年过去了,我还是做不到。
感谢您的意见。
更新
我认为以下内容可以判断角色是否死于自由落体。
- (void)didSimulatePhysics {
if (self.isMoving) {
// isMoving is an instance variable (BOOL): YES if the game has started
CGVector v = player.physicsBody.velocity;
BOOL hasFallen = self.lastFallenDate ? [self.lastFallenDate timeIntervalSinceNow] < -2.0 : YES; // lastFallenDate is an instance variable recording the time when the character fell last time
if (hasFallen) {
if (v.dy < -1500) {
[self endTheScene:kEndReasonLose];
}
}
}
}
然而,我认为就 SKAction 而言,Apple 有一个错误需要修复。不管我怎么做,虽然角色没有摔倒,但游戏开始后大约 5 秒就会出现音频。
- (void)endTheScene:(EndReason)endReason {
if (endReason == kEndReasonLose) {
SKAction *lossAudio = [SKAction playSoundFileNamed:@"failureAudio.caf" waitForCompletion:NO];
[self runAction:lossAudio];
}
}