gpt4 book ai didi

ios - 如何在 Storyboard中创建一个按钮以启动在 xcode swift 中使用 spritekit 创建的游戏

转载 作者:行者123 更新时间:2023-11-28 07:01:34 24 4
gpt4 key购买 nike

我使用 spritekit 和 swift 在 xcode 中创建了一个非常简单的游戏。现在我想要一个在我第一次启动应用程序时显示的主菜单。然后我想要一个按钮,当点击它时,它会开始游戏。有没有办法做到这一点?我应该使用 Storyboard吗?非常感谢! :)

最佳答案

通过使用 SpriteKit,您可以这样做:

在你的 GameViewController.swift 中用这段代码替换你的代码:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
//load your GameScene from viewController
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)

}

override func prefersStatusBarHidden() -> Bool {
return true
}
}

此代码将在您开始游戏时加载 GameScene

将此代码添加到您的 GameScene.swift 类中:

import SpriteKit

class GameScene: SKScene {
//create playbutton instance
let playButton = SKSpriteNode(imageNamed: "play_unpresed")

override func didMoveToView(view: SKView) {

backgroundColor = UIColor.greenColor()
addPlayButton() //add playbutton
}

func addPlayButton(){

playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
playButton.xScale = 0.2
playButton.yScale = 0.2
self.addChild(playButton)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>){
let location = touch.locationInNode(self)
//this will detect touch on play button
if self.nodeAtPoint(location) == self.playButton {
//it will transits to the next scene
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let letsPlay = playScene(size: self.size)
self.view?.presentScene(letsPlay, transition: reveal)

}
}

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

这将通过一个按钮加载一个场景,当您按下一个按钮时,它将带您到您的 playScene 为此,您必须通过依次单击 Command + N 和 iOS Source 创建一个新文件 -> Cocoa Touch 类 -> 下一步 -> 添加类名 playScene -> SKScene 的子类 -> 并创建它。

在你的playScene.swift中添加import SpriteKit

检查 THIS示例项目以获取更多信息。

HERE是 spriteKit 的简单教程。

关于ios - 如何在 Storyboard中创建一个按钮以启动在 xcode swift 中使用 spritekit 创建的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31890128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com