如何缩放/定位节点 Swift SpriteKit?自定义视图? [英] How to scale/position nodes Swift SpriteKit? Custom View?

查看:50
本文介绍了如何缩放/定位节点 Swift SpriteKit?自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,但无法找到缩放/定位所有内容的正确方法.我有一个通用应用程序,当我从一个设备切换到另一个设备时,节点每次都不在正确的位置.我使用 AspectFill 是因为它以适合每个设备的正确方式调整节点的大小.问题是定位.我不知道我得出的结论是否正确,我需要弄清楚如何使视图与屏幕大小相同,以便它针对每个设备进行更改,然后正确定位和缩放所有内容,但这就是我一直在想办法.我一直在网上尝试许多不同的东西,并研究诸如如何制作自定义视图之类的东西.我试图制作一个有约束的矩形来填充屏幕,然后将该矩形设置为等于 UIView 或 SKView.我尝试了很多不同的东西,并在网上查看了很多东西,但它们要么令人困惑,我不知道我是否在尝试正确,或者它们不适合我的情况.这给我带来了很多困惑,我希望有人可以帮助解决这个问题.我认为问题是我需要制作一个与屏幕尺寸相关的自定义视图.我不知道这是否可能或我应该追求什么.如果有人能为我解决这个问题,那就太好了.

解决方案

为了让我的游戏具有通用性,我让所有节点的大小和位置都依赖于 SKScene 大小

不要使用硬编码数字,而是尝试使用 SKScene 大小的比例.例如:而不是写这个:

node.position = CGPointMake(100, 100)

这样写:

node.position = CGPointMake(world.frame.size.width/32 * 16, world.frame.size.height/18 * 9)

<块引用>

world - SKNode 大小与您的 SKScene

完全相同

尺寸相同

let blackSquare = SKSpriteNode(texture: nil, color: UIColor.blackColor(), size: CGSizeMake(world.frame.size.width/32, world.frame.size.width/32))

还有 GameViewController 应该看起来像这样:

override func viewDidLoad() {super.viewDidLoad()//配置视图让 skView = 查看为!视窗skView.multipleTouchEnabled = false//创建和配置场景让场景 = GameScene(大小:skView.bounds.size)场景.scaleMode = .AspectFill场景大小 = skView.bounds.sizeskView.presentScene(场景);}

这有帮助吗?

I'm working on a game but can't figure out the right way to scale/ position everything. I have a universal app and when I switch from device to device the nodes aren't in the right place everytime. I'm using AspectFill because it sizes my nodes the the right way for each device. The problem is the positioning. I don't know if I'm right in making the conclusion that I need to figure out how to make the view the same size as the screen so that it changes for each device and then positions and scales everything properly, but this is what I've been trying to figure out. I've been trying many different things online and looking at things like how to make a custom view. I've tried to make a rectangle that ha constraints to fill the screen and then setting that rectangle to equal the UIView or SKView. I've tried many different things and looked at many things online but they are either confusing and I don't know if I'm trying them right or they don't pertain to my situation. This is causing a lot of confusion for me and I was hoping someone could help with this issue. I think the issue is that I need to make a custom view that relates to the size of the screen. I don't know if this is possible or what I should be going after. It would be great if someone could clear this up for me.

解决方案

To keep my games universal I make all my nodes sizes and positions dependant on SKScene size

Instead of using hardcoded numbers try using proportions from your SKScene size. For example: Instead of writing this:

node.position = CGPointMake(100, 100)

Write somthing like this:

node.position = CGPointMake(world.frame.size.width / 32 * 16, world.frame.size.height / 18 * 9)

world - SKNode of exactly same size as your SKScene

Same for sizes

let blackSquare = SKSpriteNode(texture: nil, color: UIColor.blackColor(), size: CGSizeMake(world.frame.size.width / 32, world.frame.size.width / 32))

Also GameViewController should look something like this:

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure the view

    let skView = view as! SKView
    skView.multipleTouchEnabled = false

    // Create and configure the scene

    let scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFill
    scene.size = skView.bounds.size

    skView.presentScene(scene);
}

Was this helpfull?

这篇关于如何缩放/定位节点 Swift SpriteKit?自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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