2013-01-03 66 views
0

我需要通过特定的方式在我的应用程序中组织渲染循环(这是有原因的)。在游戏中渲染循环

比方说,我有

Sprite *sprite = [[Sprite alloc] initWithContentsOfFile:@"sprite.png"]; // some sprite 

while (true) { 
    [Graphics update]; 
    sprite.x = (sprite.x + 1) % 1024 // moving sprite by one pixel each frame 
    [Input update]; 
    [self update]; 
} 

Graphics.update应该渲染一帧和延迟执行(不渲染),直到下一帧

@interface Graphics() { 
    static BOOL _frozen; 
    static int _count; 
    static int _frameCount; 
    static float _frameRate; 
    static double _lastFrameTimestamp; 
} 

@end 

@implementation Graphics 

+ (void)initialize { 
    _frozen = NO 
    _count = 0 
    _frameCount = 0 
    _frameRate = 30.0 
    _lastFrameTimestamp = CACurrentMediaTime() 
} 

+ (void)freeze { 
    _frozen = YES; 
} 

+ (void)update { 
    if _frozen 
     return 
    end 

    Video.nextFrame // some OpenGL ES magic to render next frame 

    _count++ 

    now = CACurrentMediaTime() 
    waitTill = _lastFrameTimestamp + _count * (1.0/_frameRate) 

    if now <= waitTill 
     sleep(waitTill - now) 
    else 
     _count = 0 
     _lastFrameTimestamp = CACurrentMediaTime() 
    end 

    _frameCount++ 
} 

@end 

不知怎的,它的工作原理和精灵移动。但是当我回家时,应用程序WillResignActive不会被调用,当我回到应用程序时,会出现黑屏,并在一段时间后应用程序崩溃。

这里是我尝试口的东西:https://bitbucket.org/lukas/openrgss/src/7d9228cc281207fe00a99f63b507198ea2596ead/src/graphics.cGraphics_update功能)

+1

我建议阅读Objective-C编程的介绍或复习。您将所有方法声明为类方法。以同样的方式,所有的ivars都是静态的。你的类接口不支持NSObject(或任何其他类)的子类。你可以在不调用超级实现的情况下重写初始化名单继续... – LearnCocos2D

回答

1

如果使用的是麻雀,这是你应如何处理它:

SPSprite *sprite = [[SPSprite alloc] initWithContentsOfFile:@"sprite.png"]; // some sprite 

[self addEventListener:@selector(enterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME]; 

-(void)enterFrame:(SPEnterFrameEvent*)e { 
    [Graphics update]; 
    sprite.x += 1; // moving sprite by one pixel each frame 
    [Input update]; 
    [self update]; 
} 

麻雀管理使用SP_ENTER_FRAME_EVENT你的游戏循环。每当它再次呈现时,它都会被调用。大约每秒30次(尽管可以配置)。

2

您可以尝试使用核心动画DisplayLink的,而不是一个while循环。这就是它通常在图形框架中完成的方式。 currentRunLoop每1/60秒调用一次你的更新方法。

如果使用NSRunLoop,则应该在更新中删除睡眠呼叫。

CADisplayLink *displayLink; 

// Set your update method 
displayLink = [CADisplayLink displayLinkWithTarget:[Graphics class] 
              selector:@selector(update)]; 
// Set fps to device refresh rate (60) 
[displayLink setFrameInterval:1.0f]; 

// Add your display link to current run loop 
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 

// Stop updating 
[displayLink invalidate]; 

最后一行停止执行,所以在你完成循环之前不要调用它。