我正在开发一个游戏,我有一个滚动的地面节点,现在有人如何根据它运行的设备改变地面节点的大小?我有一种方法来确定游戏运行在哪个设备上,并且改变我的游戏中所有其他节点的大小对于地面节点来说非常好。我试着这样做但没有成功。
提前谢谢。
确定游戏运行的设备的方法:
func determineDevice() {
if view!.bounds.width == 480.0 { //IP4
isIphone4 = true
isIphone5 = false
isIphone6 = false
isIphone6Plus = false
}
if view!.bounds.width == 568.0 { //IP5
isIphone4 = false
isIphone5 = true
isIphone6 = false
isIphone6Plus = false
}
if view!.bounds.width == 667.0 { //IP6
isIphone4 = false
isIphone5 = false
isIphone6 = true
isIphone6Plus = false
}
if view!.bounds.width == 736.0 { //IP6+
isIphone4 = false
isIphone5 = false
isIphone6 = false
isIphone6Plus = true
}
}
以下是我根据屏幕尺寸设置游戏中所有其他节点大小的示例:
class GameScene: SKScene, SKPhysicsContactDelegate {
var screenWidth:CGFloat = 0
var screenHeight:CGFloat = 0
override func didMoveToView(view: SKView) {
screenWidth = self.view!.bounds.width
screenHeight = self.view!.bounds.height
node.size = CGSizeMake(screenWidth / 5.5, screenHeight / 6)
}
}
最后但并非最不重要的是,地面代码:
func initializingScrollingGround() {
for var index = 0; index < 2; index += 1 {
ground = SKSpriteNode(imageNamed:"Ground_Layer")
ground.size = CGSize(width: frame.width, height: frame.height / 6)
ground.position = CGPoint(x: index * Int(ground.size.width), y: Int(screenHeight - 37))
ground.zPosition = 10
ground.anchorPoint = CGPointZero
ground.name = "Ground"
let offsetX = ground.size.width * ground.anchorPoint.x
let offsetY = ground.size.height * ground.anchorPoint.y
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0 - offsetX, 48 - offsetY)
CGPathAddLineToPoint(path, nil, 0 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 665 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 668 - offsetX, 48 - offsetY)
CGPathCloseSubpath(path)
ground.physicsBody = SKPhysicsBody(polygonFromPath: path)
ground.physicsBody?.categoryBitMask = PhysicsCatagory.ground
ground.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
ground.physicsBody?.contactTestBitMask = PhysicsCatagory.dragon
ground.physicsBody?.affectedByGravity = false
ground.physicsBody?.dynamic = false
ground.physicsBody?.restitution = 0
self.worldNode.addChild(ground)
}
}
在更新方法中:
func moveGround() {
self.worldNode.enumerateChildNodesWithName("Ground", usingBlock: { (node, stop) -> Void in
if let Ground = node as? SKSpriteNode {
Ground.position = CGPoint(x: Ground.position.x - self.groundVelocity, y: Ground.position.y)
// Checks if ground node is completely scrolled off the screen, if yes, then puts it at the end of the other node.
if Ground.position.x <= -Ground.size.width {
Ground.position = CGPointMake(Ground.position.x + Ground.size.width * 2, Ground.position.y)
}
}
})
}
现在有人能怎样解决我的问题?
答案 0 :(得分:0)
尝试的一种方法是使用SKCameraNode
,然后所有节点将一起更改。此示例使用SKCameraNode
上的扩展名,仅区分iPad和iPhone;
// SKCameraNode+Extensions.swift
extension SKCameraNode {
func updateScaleFor(userInterfaceIdiom: UIUserInterfaceIdiom, isLandscape: Bool) {
switch userInterfaceIdiom {
case .phone:
self.setScale(isLandscape ? 0.75 : 1.0)
case .pad:
self.setScale(isLandscape ? 1.0 : 1.25)
default:
break
}
}
}
然后当你的场景加载时;
camera.updateScaleFor(userInterfaceIdiom: UIDevice.current.userInterfaceIdiom, isLandscape: true)