iPad 和 iPhone 的 SpriteKit 坐标不同 [英] SpriteKit Coordinates differ between iPad and iPhone

查看:17
本文介绍了iPad 和 iPhone 的 SpriteKit 坐标不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 SpriteKit 场景,它的行为与我预期的不同.它是一个通用应用程序,我用来在场景中绘制单个红色方块的代码如下:

I have a very simple SpriteKit scene that is not behaving like I would expect. It is a universal app and the code that I am using to draw a single red square in the scene is as follows:

    let redSquare = SKSpriteNode(color: UIColor .redColor(), size: CGSizeMake(100.0, 100.0))
    redSquare.anchorPoint = CGPointMake(0.0, 0.0)
    redSquare.position = CGPointMake(1.0, 1.0)
    addChild(redSquare)

此代码在您选择 SpriteKit 游戏时 xCode 为您创建的默认应用的 GameScene didMoveToView 方法中.

This code is in the GameScene didMoveToView method of the default app that xCode creates for you when you select SpriteKit game.

在 iPad 上运行时,它的行为与预期一样,将红色正方形 1 点从屏幕的底部和左侧边缘绘制出来.

When run on the iPad it behaves as expected drawing the red square 1 point off the bottom and left edge of the screen.

当我在 iPHone 6 Plus 模拟器上运行它时,它显示如下:

When I run it on the iPHone 6 Plus simulator however it shows up like this:

宇宙飞船在两台设备上都显示在正确的接触点,但触摸位置的 println() 在 iPad 上显示 (1,1),在 iPhone 上显示 (1,100).这表明 (x,0) - (x,99) 的坐标不在 iPhone 屏幕的底部,这对我来说毫无意义.

The spaceship shows up at the correct touchpoint on both devices, however a println() of the touch location shows (1,1) on the iPad and (1,100) on the iPhone. This indicates that the coordinates from (x,0) - (x,99) are off the bottom of the iPhone screen which makes no sense to me at all.

为什么两个设备的显示方式不同?在模拟器和实际设备上的行为是相同的.

Why do the two devices not display it the same way? The behavior is the same in both the simulators and on the actual devices.

谢谢!滑板车

推荐答案

这是因为iPhone的屏幕尺寸不正确造成的-看一下场景的sks文件,应该可以看到场景的尺寸设置好了默认为 (1024, 768).要更正此问题,您需要设置场景大小和缩放模式:

This is caused because the size of the screen on iPhone is incorrect - have a look in the scene's sks file, you should see the size of the scene is set to (1024, 768) by default. To correct this you need to set the scene size and scale mode:

scene.size = skView.bounds.size
scene.scaleMode = .AspectFill

您应该在 didMoveToView 或实例化场景时执行此操作.

You should do this is didMoveToView or when you instantiate the scene.

这篇关于iPad 和 iPhone 的 SpriteKit 坐标不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆