SkSpriteNode在通用游戏中的位置 [英] SkSpriteNode position in universal game

查看:90
本文介绍了SkSpriteNode在通用游戏中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift在纵向模式下为所有iOS设备创建通用游戏.在GameViewController中,我正在创建这样的场景:

I'm creating universal game for all iOS devices in portrait mode using Swift. In GameViewController I'm creating scene like this:

let scene = GameScene(size:CGSize(width: 1536, height: 2048))
scene.scaleMode = .AspectFill

背景图像的分辨率为1536x2048,因此,在iPad上使用scaleMode以上时,它将以完整尺寸显示,在iPhone 6上,其显示为1152x2048,且侧面已修剪.在所有设备上都能正常工作,并且只需要一张背景图像.问题是,如果我要求size.width或self.frame.size.width,即使实际的可见区域为例如,它总是返回1536. 1152.

Background image has resolution 1536x2048, and so with above scaleMode on iPad it's displayed in its full size, on iPhone 6 1152x2048 is displayed with sides trimmed. Works perfectly fine on all devices, and only one background image is needed. Problem is that if I call for size.width or self.frame.size.width it always returns 1536, even if the actual visible area is e.g. 1152.

如何设置SkSpriteNode相对于可见区域的位置,以使其在每台设备的角落都为50x50?

How can I set SkSpriteNode's position relative to visible area, so that it'll be for example 50x50 from the corner on every device?

推荐答案

如何设置SkSpriteNode相对于可见区域的位置,所以 例如在每个设备的拐角处都是50x50?

How can I set SkSpriteNode's position relative to visible area, so that it'll be for example 50x50 from the corner on every device?

可见区域"就是视图.

因此,您可以相对于视图而不是场景来定位自己的位置.实际上,我在游戏中做了很多事情,这是通用的,可以在OS X和iOS上运行.

So you can make your positions relative to the view, and not the scene. I actually do this a lot in my game, which is universal and runs on both OS X and iOS.

在我的情况下,我通常是针对用户界面执行此操作,因此我可能具有缩放的场景,但是我想设置一些相对于缩放的场景而不是相对于可见区域(即视图)的位置.

In my case I typically do this for the user-interface, so I might have a scaled scene but I want to set some positions not relative to the scaled scene but relative to the visible area (i.e. the view).

为此,您可以编写一个将视图坐标转换为相应场景坐标的函数.

To do this, you can write a function that converts view coordinates to the corresponding scene coordinates.

这是我使用的功能.请注意,我从视线的高度中减去了所需的y位置,以便可以将(0,0)像sprite-kit一样对待左下角,而不是像UIKit那样对待左上角.

Here is my function that I use. Note that I subtract my desired y-position from height of view so that I can treat (0,0) as the bottom-left like sprite-kit does instead of the top-left like UIKit does.

func convert(point: CGPoint)->CGPoint {
    return self.view!.convert(CGPoint(x: point.x, y:self.view!.frame.height-point.y), to: self)
}

以下是使用此功能的示例:

Here is an example of using this function:

self.node.position = convert(CGPoint(x: 50, y: 50)) 

这将始终强制节点相对于视图(屏幕的可见部分)处于(50,50)的位置,而不管场景的缩放比例和大小如何.

This will always force the position of the node to be at (50,50) relative to the view (the visible portion of the screen) regardless of how your scene is scaled and sized.

这篇关于SkSpriteNode在通用游戏中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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