我的第一个视图控制器上有一个按钮,指向第二个视图控制器。当第二个视图控制器出现时,应该会出现一个球图像,并且它的代码应该运行。我似乎在弄清楚为什么图像没有出现时遇到了一点麻烦。任何帮助将不胜感激!
import UIKit
import SpriteKit
class GameViewController: UIViewController {
@IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if let scene = gamePlay(fileNamed:"gamePlay") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
import SceneKit
import SpriteKit
class gamePlayController: UIViewController {
}
class gamePlay: SKScene, SKPhysicsContactDelegate {
let skyColor = SKColor(red: 0, green: 191, blue: 255, alpha: 1)
var blueBall:SKSpriteNode!
override func didMoveToView(view: SKView) {
self.backgroundColor = skyColor
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
self.physicsWorld.contactDelegate = self
blueBall = SKSpriteNode( imageNamed: "ball")
blueBall.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
blueBall.physicsBody = SKPhysicsBody(circleOfRadius: blueBall.size.width / 1.5 )
blueBall.physicsBody!.dynamic = true
blueBall.physicsBody!.allowsRotation = false
self.addChild(blueBall)
}
override func touchesBegan(touches: Set<UITouch> , withEvent event: UIEvent?) {
self.blueBall.physicsBody?.velocity = CGVectorMake(35, 0)
self.blueBall.physicsBody?.applyImpulse(CGVectorMake(4, 10))
}
}
看起来位置不对。试试这个代码
blueBall.position = CGPoint(x: CGRectGetMidX(self.bounds), y: CGRectGetMidY(self.bounds))
我解决了这个问题。我需要将我的 backgroundImage 编写为 SKSpriteNode,并相应地设置 zPosition。