简体   繁体   English

我无法将球SKSpriteNode显示在辅助View Controller的UIImage(Xcode / Swift 2)上

[英]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.

相关问题 在Swift中我的视图控制器上找不到后退按钮 - Can't get a back button on my view controller in Swift 通过名称 Swift 2, Xcode 7 获取 SKSpriteNode - Get SKSpriteNode by name Swift 2, Xcode 7 带有 UIImage 的 Swift SKSpriteNode - Swift SKSpriteNode with UIImage Swift / XCode-为什么在按下tableViewCell时不能将数据传递给下一个视图控制器? - Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed? 如何将文本字段的文本输入到视图控制器中? - How can I get the textfield's text into my view controller? 我正在尝试让图标数组显示在我的天气应用中,但似乎无法获取UIImage来显示它们 - I am trying to get an icon array to display in my weather app, but can not seem to get UIImage to display them 当用户在Swift中点击我的UIImage时,如何全屏显示图像? - How can I display image on fullscreen when user taps on my UIImage in Swift? Xcode:我无法访问某个场景的视图 controller - Xcode: I can't access a view controller for a scene Xcode(Swift):我无法在视图控制器之间传输数据 - Xcode(Swift): I can't transfer data between View Controllers 无法让 S3 上传在 Xcode 10.2/Swift 中工作 - Can't get S3 Uploads to work in Xcode 10.2/Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM