[英]I can't get my ball SKSpriteNode to display on a secondary View Controller's UIImage(Xcode / Swift 2)
I have a button on my first view controller that leads to the second view controller. 我的第一个视图控制器上有一个按钮,该按钮可引向第二个视图控制器。 When the second view controller appears, a ball image is supposed to appear and it's code is supposed to run.
当第二个视图控制器出现时,应该显示一个球形图像并且应该运行其代码。 I seem to be having a little bit of trouble figuring out why the image isn't appearing.
我似乎在弄清楚为什么未显示图像时有些麻烦。 Any help would be greatly appreciated!
任何帮助将不胜感激!
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))
}
}
Looks like position is wrong. 看起来位置是错误的。 Try this code
试试这个代码
blueBall.position = CGPoint(x: CGRectGetMidX(self.bounds), y: CGRectGetMidY(self.bounds))
I fixed the issue. 我解决了这个问题。 I needed to write my backgroundImage as an SKSpriteNode and set the zPosition accordingly.
我需要将backgroundImage编写为SKSpriteNode并相应地设置zPosition。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.