iPhone 5 秒 SpriteKit 绘图异常
iPhone 5s SpriteKit drawing oddities
我了解到 iPhone 5s 的像素分辨率为 640 x 1136,点分辨率为 320 x 568(为了向后兼容非 Retina 设备)。
problem/confusion/oddity 似乎是在我使用 SpriteKit 时出现的。
示例:
我正在从左下角 (0, 0) 到右上角 (width, height) 画一条线。结果是这条线几乎画到一半。事实上,当我打印出屏幕尺寸时,它应该是 320 x 568。所以我决定从 (0, 0) 绘制到 (width * 2, height * 2)。当然,这打印出 640 x 1136。
奇怪的是:
尽管我是从应该是对角线的角到角绘制的,但实际上 而不是 ,是从角到角绘制的。
备注:
- I'm getting the width & height values from self.value.frame.size.
- The diagonal line seems to draw just fine using any of the iPad simulators.
知道这是怎么回事吗?
无论如何,我是这样取得好成绩的:
只需打开一个新项目并试试这个:
在你的 GameViewController 中使用 viewWillLayoutSubviews.
而不是 viewDidLoad
编辑:
这里有一个good explanation by Rob Mayoff about methods like viewDidLoad and viewWillLayoutSubviews.
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
if(!skView.scene){
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
所以现在在场景 class 中的 didMoveToView 方法中,只需画一条线:
SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 0.0, 0.0);
CGPathAddLineToPoint(pathToDraw, NULL, self.frame.size.width,self.frame.size.height);
yourline.path = pathToDraw;
[yourline setStrokeColor:[UIColor redColor]];
[self addChild:yourline];
CGPathRelease(pathToDraw);
阅读 this 关于 init 与 didMoveToView 的对比(阅读 LearnCocos2D 发表的评论)。
大致就是这些,希望对您有所帮助。
我了解到 iPhone 5s 的像素分辨率为 640 x 1136,点分辨率为 320 x 568(为了向后兼容非 Retina 设备)。
problem/confusion/oddity 似乎是在我使用 SpriteKit 时出现的。 示例:
我正在从左下角 (0, 0) 到右上角 (width, height) 画一条线。结果是这条线几乎画到一半。事实上,当我打印出屏幕尺寸时,它应该是 320 x 568。所以我决定从 (0, 0) 绘制到 (width * 2, height * 2)。当然,这打印出 640 x 1136。
奇怪的是: 尽管我是从应该是对角线的角到角绘制的,但实际上 而不是 ,是从角到角绘制的。
备注:
- I'm getting the width & height values from self.value.frame.size.
- The diagonal line seems to draw just fine using any of the iPad simulators.
知道这是怎么回事吗?
无论如何,我是这样取得好成绩的:
只需打开一个新项目并试试这个:
在你的 GameViewController 中使用 viewWillLayoutSubviews.
而不是 viewDidLoad编辑: 这里有一个good explanation by Rob Mayoff about methods like viewDidLoad and viewWillLayoutSubviews.
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
if(!skView.scene){
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
所以现在在场景 class 中的 didMoveToView 方法中,只需画一条线:
SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 0.0, 0.0);
CGPathAddLineToPoint(pathToDraw, NULL, self.frame.size.width,self.frame.size.height);
yourline.path = pathToDraw;
[yourline setStrokeColor:[UIColor redColor]];
[self addChild:yourline];
CGPathRelease(pathToDraw);
阅读 this 关于 init 与 didMoveToView 的对比(阅读 LearnCocos2D 发表的评论)。
大致就是这些,希望对您有所帮助。