gpt4 book ai didi

iphone - 这是为 iPhone 游戏制作游戏循环的好方法吗?

转载 作者:行者123 更新时间:2023-12-03 18:48:11 26 4
gpt4 key购买 nike

我是 iPhone 开发新手,但正在尝试构建 2D 游戏。我正在关注一本书,但它创建的游戏循环基本上是这样说的:

function gameLoop
update()
render()
sleep(1/30th second)
gameLoop

原因是这将以 30 fps 的速度运行。然而,这似乎有点心理上的问题,因为如果我的帧花了 1/30 秒,那么它将以 15fps 的速度运行(因为它花费的 sleep 时间和更新时间一样多)。

所以,我做了一些挖掘,发现了 CADisplayLink 类,它将对我的 gameLoop 函数的调用同步到刷新率(或其一小部分)。我找不到它的许多示例,因此我在这里发布代码进行审查:-)它似乎按预期工作,并且它包括将经过的(帧)时间传递到 Update 方法中,以便我的逻辑可以是帧率-独立(但是,如果我的框架运行时间超过了允许的时间,我实际上无法在文档中找到 CADisplayLink 会做什么 - 我希望它能尽力 catch ,并且不会崩溃!)。

//
// GameAppDelegate.m
//
// Created by Danny Tuppeny on 10/03/2010.
// Copyright Danny Tuppeny 2010. All rights reserved.
//

#import "GameAppDelegate.h"
#import "GameViewController.h"
#import "GameStates/gsSplash.h"

@implementation GameAppDelegate

@synthesize window;
@synthesize viewController;

- (void) applicationDidFinishLaunching:(UIApplication *)application
{
// Create an instance of the first GameState (Splash Screen)
[self doStateChange:[gsSplash class]];

// Set up the game loop
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void) gameLoop
{
// Calculate how long has passed since the previous frame
CFTimeInterval currentFrameTime = [displayLink timestamp];
CFTimeInterval elapsed = 0;

// For the first frame, we want to pass 0 (since we haven't elapsed any time), so only
// calculate this in the case where we're not the first frame
if (lastFrameTime != 0)
{
elapsed = currentFrameTime - lastFrameTime;
}

// Keep track of this frames time (so we can calculate this next time)
lastFrameTime = currentFrameTime;

NSLog([NSString stringWithFormat:@"%f", elapsed]);

// Call update, passing the elapsed time in
[((GameState*)viewController.view) Update:elapsed];
}

- (void) doStateChange:(Class)state
{
// Remove the previous GameState
if (viewController.view != nil)
{
[viewController.view removeFromSuperview];
[viewController.view release];
}

// Create the new GameState
viewController.view = [[state alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self];

// Now set as visible
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}

- (void) dealloc
{
[viewController release];
[window release];
[super dealloc];
}

@end

如有任何反馈,我们将不胜感激:-)

PS。如果你能告诉我为什么所有书籍都使用“viewController.view”,但其他所有内容似乎都使用“[对象名称]”格式,那就加分了。为什么不[viewController View ]?

最佳答案

您在问题中将 Cocos2D 列为标签,但您实际上并未使用任何 Cocos2D 代码。您是否考虑过为您的游戏实现 Cocos2D?这将为您省去一些不必要的麻烦。

至于你的语法问题 [myObject view] 用于调用 myObject 上的方法,而 myObject.view 用于设置/获取作为属性公开的实例变量。我不记得您是否也可以使用 [myObject view] 检索实例变量,但如果这有效,那么我想两者之间的唯一区别是语法,您可以使用这两种方法来检索实例变量。

希望其中的一些内容对您有用。

关于iphone - 这是为 iPhone 游戏制作游戏循环的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2427917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com