GVKun编程网logo

Xcode 6 / SpriteKit / Swift中的场景大小(swift应用场景)

10

在本文中,我们将详细介绍Xcode6/SpriteKit/Swift中的场景大小的各个方面,并为您提供关于swift应用场景的相关解答,同时,我们也将为您带来关于AppleWatchSimulator

在本文中,我们将详细介绍Xcode 6 / SpriteKit / Swift中的场景大小的各个方面,并为您提供关于swift应用场景的相关解答,同时,我们也将为您带来关于Apple Watch Simulator上的watchOS中运行SpriteKit游戏(xCode 8,Swift 3,iOS 10) – libswiftSwiftOnoneSupport错误、ios – SpriteKit,XCode 8,Swift 3 MGGetBoolAnswer在模拟器中不可用、ios – 使用swift和spriteKit如何围绕节点外的点旋转SKSpriteNode?、ios – 在SpriteKit中创建按钮:Swift的有用知识。

本文目录一览:

Xcode 6 / SpriteKit / Swift中的场景大小(swift应用场景)

Xcode 6 / SpriteKit / Swift中的场景大小(swift应用场景)

我一直被困在Xcode中应该做的非常简单的事情上,我试图在场景中添加一个精灵,并将其放置在左下角:

var groundTexture : SKTexture = SKTexture(imageNamed: "dirt-block")    var ground : SKSpriteNode = SKSpriteNode(texture: groundTexture)ground.position = CGPointMake(ground.size.width/2, ground.size.height/2)self.addChild(ground)

它什么都没显示,所以我看了看场景尺寸是什么,self.frame.size.width和self.frame.size.height返回的宽度为1024,高度为768,与方向无关。

我希望以横向模式解决此问题,如果这对答案有什么影响?

显然,我缺少一个与场景设置有关的非常基本的想法,如果有人可以提供任何帮助,将不胜感激。

谢谢。

答案1

小编典典

您必须编辑GameViewController:

切一切从viewDidLoad()super.viewDidLoad()

覆写 viewWillLayoutSubviews

override func viewWillLayoutSubviews() {    super.viewWillLayoutSubviews()    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {        // Configure the view.        var 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.size = skView.bounds.size        scene.scaleMode = .AspectFill        skView.presentScene(scene)    }}

您还应该设置场景大小,如您所见

scene.size = skView.bounds.size

希望这可以帮助

还有一个很棒的教程,对它进行了更好的解释:http : //www.raywenderlich.com/49721/how-to-
create-a-breakout-game-using-
spritekit (第1章或第2章)

Apple Watch Simulator上的watchOS中运行SpriteKit游戏(xCode 8,Swift 3,iOS 10) – libswiftSwiftOnoneSupport错误

Apple Watch Simulator上的watchOS中运行SpriteKit游戏(xCode 8,Swift 3,iOS 10) – libswiftSwiftOnoneSupport错误

我下载了xCode 8.0 beta并打开了最近用 swift 2编写的项目,然后我使用xCode将其转换为swift 3.

然后我通过设置“游戏”为我的项目添加了一个watchOS目标

文件>新>目标:

screenshot

我检查了WatchExtension中的GameScene.swift,确定所有代码都在那里并设置了一个场景:

import SpriteKit

    class GameScene: SKScene {

        private var spinnyNode : SKShapeNode?

        override func sceneDidLoad() {

            if let label = self.childNode(withName: "//helloLabel") as? SKLabelNode {
                label.alpha = 0.0
                label.run(SKAction.fadeIn(withDuration: 2.0))
            }

            let w = (self.size.width + self.size.height) * 0.05
            let spinnyNode = SKShapeNode(rectOf: CGSize(width: w,height: w),cornerRadius: w * 0.3)

            spinnyNode.position = CGPoint(x: 0.0,y: 0.0)
            spinnyNode.strokeColor = UIColor.red()
            spinnyNode.linewidth = 8.0

            spinnyNode.run(SKAction.sequence([SKAction.wait(forDuration: 0.5),SKAction.fadeOut(withDuration: 0.5),SKAction.removeFromParent()]))

            spinnyNode.run(SKAction.repeatForever(SKAction.rotate(byAngle: 6.28,duration: 1)))

            self.run(SKAction.repeatForever(SKAction.sequence([SKAction.wait(forDuration: 2.0),SKAction.run({
                                                                let n = spinnyNode.copy() as! SKShapeNode
                                                                self.addChild(n)
                                                               })])))
        }


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

不幸的是,我似乎无法在Apple Watch Simulator上安装它.

我已经尝试了我能想到的一切,包括:

>清洁建筑等
>卸载/重新安装,
>查看了info.plist for common errors,
>使用Add Additional Simulators创建一个带有配对Apple Watch的新模拟器,
>已添加Skip Install =否,建议here,
>从iPhone模拟器中的配对iOS Apple Watch App安装(只是不安装),
>甚至添加了用户定义的项目设置,如raywenderlich watchOS教程中所建议的……

我甚至无法安装或出现在Apple Watch上.我不做什么?

UPDATE

我已经为iOS应用程序调整了部署目标到10.0,我终于能够从iPhone模拟器中的Apple Watch应用程序安装它,除了从Apple Watch Simulator启动Apple Watch App之外,我收到以下错误:

dyld: Library not loaded: @rpath/libswiftSwiftOnonesupport.dylib
Referenced from: /Users/MYNAME/Library/Developer/CoreSimulator/Devices/XXXXXX-XXXX-XXXX-XXXX/data/Containers/Bundle/Application/XXXXXX-XXXX-XXXX-XXXX/MYAPPNAME.app/PlugIns/MYAPPWATCH Extension.appex/MYAPPWATCH Extension
Reason: image not found
(lldb)

这个错误是什么意思?不应该加载任何图像,因为它是默认的SpriteKit测试…

解决方法

可能不是真正的解决方案,但是在我尝试各种各样的事情后发现的一个解决方法被发现 here,on Stackoverflow,因为错误发生在我上面的问题的底部.

因此,如果您将应用程序转换为Swift 3.0,将watchOS“游戏”目标添加到您的项目中,将iOS部署目标更改为10.0并在WatchOS 3.0模拟器和iPhone 6s iOS 10模拟器上运行,请更新以下设置:

将NO更改为YES:

Project > Targets > App Name > Embed Asset Packs In Product Bundle = YES

并且“你好,世界!”应该出现在Apple Watch上,带有旋转的脉冲spriteNode(屏幕截图中未显示,因为它没有足够快地捕获它).

Apple Watch

请注意,您可能必须通过启动Apple Watch App,点击您的应用程序,然后按“Apple Watch上的显示”,从iPhone模拟器安装Apple Watch App中的应用程序.

ios – SpriteKit,XCode 8,Swift 3 MGGetBoolAnswer在模拟器中不可用

ios – SpriteKit,XCode 8,Swift 3 MGGetBoolAnswer在模拟器中不可用

每次我在 Xcode模拟器中运行我的应用程序时,我的控制台都会填写这些消息.有谁知道这意味着什么,或者我怎么能摆脱它?

这是完整的信息:

2016-09-14 19:28:51.042 SpaceGameDeluxe[3182:49139] SKUtil.m:
MGGetBoolAnswer is not available in the simulator.

解决方法

我也收到了这些消息.我在苹果开发论坛上发现了一个线程,苹果工作人员说:

by fbroom on Sep 15,2016 1:22 PM

This is a harmless message. It will be removed in the next release. Sorry for the confusion.

https://forums.developer.apple.com/thread/62983

ios – 使用swift和spriteKit如何围绕节点外的点旋转SKSpriteNode?

ios – 使用swift和spriteKit如何围绕节点外的点旋转SKSpriteNode?

如何围绕任意点在SpriteKit中旋转节点?

我已经搜索了一个答案“创建一个SKNode并将其位置设置到旋转中心.将应该围绕该中心旋转的节点添加到中心节点.将子节点的位置设置为所需的偏移量(即半径,比如说x 100).更改中心节点的旋转属性,使子节点围绕中心点旋转.同样适用于cocos2d btw.“

但我真的不明白如何在代码中写这个:(.我在swift非常初学者,这看起来很难.任何人都可以给我一个代码帮助?

解决方法

经过几个小时的实验,我就这样做了.
var fish: SKSpriteNode = SKSpriteNode(imageNamed: "fish")

    fish.anchorPoint = CGPoint(x:CGFloat(-0.5),y:CGFloat(-0.2)) 
    fish.zPosition = CGFloat(y+0.5)  
    fish.position = CGPoint(x:CGFloat(x),y:CGFloat(-60-96*(y-3)))

    var jump = SKAction.rotatetoAngle(CGFloat(3.14),duration: NSTimeInterval(1))
    fish.runAction(jump)

ios – 在SpriteKit中创建按钮:Swift

ios – 在SpriteKit中创建按钮:Swift

我想在SpriteKit或SKScene中创建一个按钮,将视图发送到另一个视图控制器.

我尝试使用“performSegue with identifier”,但显然SKScene不支持此功能.
如何使用SpriteKit创建一个将视图发送到另一个视图的按钮?

这是我尝试用来执行此操作的代码.

带有“HomeButton.prepareForSegueWithIdentifier()”的行只是一个例子.它实际上不会让我添加“prepareForSegue”部分,它不支持它< ---我的意思是,当我去添加它时,它是无法识别的.

class GameOverScene: SKScene {

     var HomeButton: SKNode! = nil


    init(size: CGSize,won: Bool) {
        super.init(size: size)

        backgroundColor = SKColor.whiteColor()

        HomeButton = SKSpriteNode(color: SKColor.blueColor(),size: CGSize(width: 100,height: 100))
        HomeButton.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
        HomeButton.userInteractionEnabled = true
        self.addChild(HomeButton)

        let message = won ? "You Won!" : "You Lose!"





        let label = SKLabelNode(fontNamed: "Title 1")
        label.text = message
        label.fontSize = 40
        label.fontColor = SKColor.blackColor()
        label.position = CGPoint(x: size.width/2,y: size.height/2)
        addChild(label)

        runAction(SKAction.sequence([SKAction.waitForDuration(3.0),SKAction.runBlock() {
            let reveal = SKTransition.flipHorizontalWithDuration(0.5)
            let scene = GameScene(size: size)
            self.view?.presentScene(scene,transition: reveal)
        }
        ]))


    }

    override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            if HomeButton.containsPoint(location) {

                HomeButton.prepareForSegueWithIdentifier()

            }
        }
    }

注意:我尝试过使用按钮,但它们不适用于SKScene.

如果有任何混淆,我会回应.

解决方法

如果你需要在SpriteKit中创建一个按钮,我认为这个按钮必须有所有或部分可用的动作来做任何你想做的事情(就像UIButton所做的那样)

在这里,您可以找到一个构建SpriteKit按钮的简单类,称为FTButtonNode:

class FTButtonNode: SKSpriteNode {

    enum FTButtonActionType: Int {
        case TouchUpInside = 1,TouchDown,TouchUp
    }

    var isEnabled: Bool = true {
        didSet {
            if (disabledTexture != nil) {
                texture = isEnabled ? defaultTexture : disabledTexture
            }
        }
    }
    var isSelected: Bool = false {
        didSet {
            texture = isSelected ? selectedTexture : defaultTexture
        }
    }

    var defaultTexture: SKTexture
    var selectedTexture: SKTexture
    var label: SKLabelNode

    required init(coder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    init(normalTexture defaultTexture: SKTexture!,selectedTexture:SKTexture!,disabledTexture: SKTexture?) {

        self.defaultTexture = defaultTexture
        self.selectedTexture = selectedTexture
        self.disabledTexture = disabledTexture
        self.label = SKLabelNode(fontNamed: "Helvetica");

        super.init(texture: defaultTexture,color: UIColor.whiteColor(),size: defaultTexture.size())
        userInteractionEnabled = true

        //Creating and adding a blank label,centered on the button
        self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center;
        self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center;
        addChild(self.label)

        // Adding this node as an empty layer. Without it the touch functions are not being called
        // The reason for this is unkNown when this was implemented...?
        let bugFixLayerNode = SKSpriteNode(texture: nil,color: UIColor.clearColor(),size: defaultTexture.size())
        bugFixLayerNode.position = self.position
        addChild(bugFixLayerNode)

    }

    /**
     * Taking a target object and adding an action that is triggered by a button event.
     */
    func setButtonAction(target: AnyObject,triggerEvent event:FTButtonActionType,action:Selector) {

        switch (event) {
        case .TouchUpInside:
            targetTouchUpInside = target
            actionTouchUpInside = action
        case .TouchDown:
            targetTouchDown = target
            actionTouchDown = action
        case .TouchUp:
            targetTouchUp = target
            actionTouchUp = action
        }

    }

    /*
    New function for setting text. Calling function multiple times does
    not create a ton of new labels,just updates existing label.
    You can set the title,font type and font size with this function
    */

    func setButtonLabel(title: Nsstring,font: String,fontSize: CGFloat) {
        self.label.text = title as String
        self.label.fontSize = fontSize
        self.label.fontName = font
    }

    var disabledTexture: SKTexture?
    var actionTouchUpInside: Selector?
    var actionTouchUp: Selector?
    var actionTouchDown: Selector?
    weak var targetTouchUpInside: AnyObject?
    weak var targetTouchUp: AnyObject?
    weak var targetTouchDown: AnyObject?

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if (!isEnabled) {
            return
        }
        isSelected = true
        if (targetTouchDown != nil && targetTouchDown!.respondsToSelector(actionTouchDown!)) {
            UIApplication.sharedApplication().sendAction(actionTouchDown!,to: targetTouchDown,from: self,forEvent: nil)
        }
    }

    override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {

        if (!isEnabled) {
            return
        }

        let touch: AnyObject! = touches.first
        let touchLocation = touch.locationInNode(parent!)

        if (CGRectContainsPoint(frame,touchLocation)) {
            isSelected = true
        } else {
            isSelected = false
        }

    }

    override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if (!isEnabled) {
            return
        }

        isSelected = false

        if (targetTouchUpInside != nil && targetTouchUpInside!.respondsToSelector(actionTouchUpInside!)) {
            let touch: AnyObject! = touches.first
            let touchLocation = touch.locationInNode(parent!)

            if (CGRectContainsPoint(frame,touchLocation) ) {
                UIApplication.sharedApplication().sendAction(actionTouchUpInside!,to: targetTouchUpInside,forEvent: nil)
            }

        }

        if (targetTouchUp != nil && targetTouchUp!.respondsToSelector(actionTouchUp!)) {
            UIApplication.sharedApplication().sendAction(actionTouchUp!,to: targetTouchUp,forEvent: nil)
        }
    }

}

该来源于this Gist年提供

用法:

let backTexture: SKTexture! = SKTexture(image:"backBtn.png")
let backTextureSelected: SKTexture! = SKTexture(image:"backSelBtn.png")  
let backBtn = FTButtonNode(normalTexture: backTexture,selectedTexture: backTextureSelected,disabledTexture: backTexture,size:backTexture.size())
backBtn.setButtonAction(self,triggerEvent: .TouchUpInside,action: #selector(GameScene.backBtnTap))
backBtn.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
backBtn.zPosition = 1
backBtn.name = "backBtn"
self.addChild(backBtn)

func backBtnTap() {
    print("backBtnTap tapped")
    // Here for example you can do:
    let transition = SKTransition.fadeWithDuration(0.5)
    let nextScene = MenuScene(size: self.scene!.size)
    nextScene.scaleMode = .Resizefill
    self.scene?.view?.presentScene(nextScene,transition: transition)
}

关于Xcode 6 / SpriteKit / Swift中的场景大小swift应用场景的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Apple Watch Simulator上的watchOS中运行SpriteKit游戏(xCode 8,Swift 3,iOS 10) – libswiftSwiftOnoneSupport错误、ios – SpriteKit,XCode 8,Swift 3 MGGetBoolAnswer在模拟器中不可用、ios – 使用swift和spriteKit如何围绕节点外的点旋转SKSpriteNode?、ios – 在SpriteKit中创建按钮:Swift等相关知识的信息别忘了在本站进行查找喔。

本文标签: