2015-03-25 73 views
0

所以我只是想做一個適合不同屏幕的背景。這是我到目前爲止:如何爲不同屏幕調整背景大小?

class GameScene: SKScene, { 

let scaleFactor: CGFloat = 0.0 

override init(size: CGSize) { 
    super.init(size: size) 
    scaleFactor = self.size.width/320.0 
} 

func createBackgroundNode() -> SKNode { 

    let backgroundNode = SKNode() 
    let background = SKSpriteNode(imageNamed: "Background") 
    background.setScale(scaleFactor) 
    background.position = CGPoint(x: self.frame.width/2 , y: self.frame.height/2) 
    background.zPosition = 0 
    addChild(background) 

    return backgroundNode 
} 

回答

1

我在編碼的目標c,但我認爲我的答案將爲你工作。你只需要將代碼調整到swift。我添加下面的方法只是實施後:

- (void)ScreenSize 
{ 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 

     CGSize result = [[UIScreen mainScreen] bounds].size; 
     if (result.height == 480) { 
      //Do something 
     } else if (result.height == 568) { 
      //Do something 
     } else if (result.height == 667) { 
      //Do something 
     } else if (result.height == 736) { 
      //Do something 
     } 
    } else { 
     //Do something for iPad here 
    } 
} 

我用虛空在這裏,但你可以使用的東西,什麼適合你的需要,像SKSpriteNode等

希望它能幫助,

好運。