ios - iPhone 5s SpriteKit 绘图异常

标签 ios sprite-kit skscene skview

我了解到 iPhone 5s 的像素分辨率为 640 x 1136,点分辨率为 320 x 568(为了向后兼容非 Retina 设备)。

当我使用 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.

知道这里发生了什么吗? enter image description here

最佳答案

无论如何,这是我取得好成绩的方式:

只需打开一个新项目并试试这个:

在您的 GameViewController 中,不要使用 viewDidLoad,而是使用 viewWillLayoutSubviews

编辑: 这是一个 good explanation Rob Mayoff 关于 viewDidLoad 之类的方法和 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];
    }
}

现在在场景类的 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 发表的评论)。

大致就是这些,希望对您有所帮助。

关于ios - iPhone 5s SpriteKit 绘图异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713792/

相关文章:

带有可点击内容的 ios 图像

ios - 如何检查 iPhone SDK 的第 3 方静态库是否为 Thumb 编译?

swift3 - SpriteKit中的一击必杀功能

ios - 我是否正确地定义了 SKScene?

swift - tvOS Swift SKScene dealloc 喜怒无常

iphone - ios 5 sdk,添加自定义控件

ios - 在 iOS 上调试 EXC_BAD_ACCESS

opengl-es - 自从将 SKShader 添加到 Sprite 后,如何计算当前时间?

ios - SpriteKit 平台游戏中的性能问题

cocoa - TouchesBeganWithEvent 未在 SKScene 中调用