2014-11-25 91 views
0

定位SKSpriteNode這是我與SpriteKit的第一個項目,我下面THIS教程 但是,當我試着給的位置,像他那樣進入該教程在20:10這段代碼的圖片:問題與SKScene

playScene.swift

import SpriteKit 

class playScene : SKScene { 

let runningBar = SKSpriteNode(imageNamed: "bar") 

override func didMoveToView(view: SKView) { 
    println("We are at the new scene!") 
    self.backgroundColor = UIColor(hex: 0x80D9FF, alpha: 1) 
    self.runningBar.anchorPoint = CGPointMake(0.5, 0.5) 
    self.runningBar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.runningBar.size.height/2)) 
    self.addChild(self.runningBar) 

} 
override func update(currentTime: NSTimeInterval) { 

    } 
} 

我想在屏幕下方給的位置,但我得到了這樣的輸出:

enter image description here

但輸出應該是:

enter image description here

GameScene.swift(如果需要)

import SpriteKit 

class GameScene: SKScene { 

let playButton = SKSpriteNode(imageNamed: "play") 

override func didMoveToView(view: SKView) { 

    self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) 
    self.addChild(self.playButton) 
    self.backgroundColor = UIColor(hex: 0x80D9FF, alpha: 1) 

} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

    for touch: AnyObject in touches{ 
     let location = touch.locationInNode(self) 
     if self.nodeAtPoint(location) == self.playButton{ 
      var scene = playScene(size: size.self) 
      let skView = self.view as SKView! 
      skView.ignoresSiblingOrder = true 
      scene.scaleMode = .ResizeFill 
      scene.size = skView.bounds.size 
      skView.presentScene(scene) 
     } 

    } 

} 

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
    } 
} 

誰能告訴我怎樣才能做到這一點?

謝謝提前。

+0

發佈與創建酒吧相關的代碼 – LearnCocos2D 2014-11-25 11:53:32

回答

2

你需要正確的看教程。你已經錯了設置以下行:

self.runningBar.anchorPoint = CGPointMake(0.5, 0.5) 
self.runningBar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.runningBar.size.height/2)) 

他們應該是:

self.runningBar.anchorPoint = CGPointMake(0, 0.5) 
self.runningBar.position = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame) + (self.runningBar.size.height/2)) 

爲了更好地理解座標系統的,有看documentation

+0

'anchorPoint = CGPointZero'和'position = CGPointZero'如何? – 0x141E 2014-11-25 17:29:47

+0

非常感謝你!它的工作現在。 :) – 2014-11-26 06:01:34

+0

Dharmesh,你也可以按照@ 0x141E – ZeMoon 2014-11-26 06:03:03