Invalid Context

按照《IPhone Game Development》开始学习Game Development。 当在一个Game Loop中进行Update和Render时,都需要得到CGContextRef画图形。如果直接调用Update和Render, 在其中调UIGraphicsGetCurrentContext()就会报Invalid Context。 按照文档中的说法,系统会维护一个CGContextRef的栈,而UIGraphicsGetCurrentContext()会取栈顶的CGContextRef 正确的做法是只在drawRect里调用UIGraphicsGetCurrentContext() 因为在drawRect之前,系统会往栈里面压入一个valid的CGContextRef,除非自己去维护一个CGContextRef,否则不应该在其他地方取CGContextRef 对于Game Loop这种设计,应该由drawRect调用Render进行View的重绘,Update只负责修改一些变量,在Render中进行判断和绘制。