我上周刚开始编程并且一直试图为IOS制作一个简单的游戏应用程序。关于游戏在玩游戏时要做什么,一切都运转正常。然而,在我输了并被带到我的结束场景之后,当我点击重启按钮时,该应用程序似乎将我带回游戏场景,而是打开一个暗褐色的屏幕并在此之前停留几秒钟然后返回到最后的场景。然后,如果我在回到结束场景后再次单击重启按钮,它会重复。
棕色屏幕持续多长时间取决于具有最快时间间隔的sharkTimer。因此,如果最快的是每半秒生成一条鲨鱼,那么棕色屏幕会持续半秒钟,然后转移到最终场景。
游戏非常简单,因为角色是一条上下游动的鱼,游戏的目标是吃一些叫做食物的东西,并避免其他被称为鲨鱼游向你的东西。
这是我项目的链接:
https://www.dropbox.com/sh/z8o3iunz9rng6na/AACDGGOTR-QhqbQc402y4sgRa?dl=0
这是我在GameScene.swift文件中到目前为止所得到的:
import SpriteKit
import UIKit
struct physicsCategory
{
static let shark: UInt32 = 1
static let food: UInt32 = 2
static let fish: UInt32 = 3
}
class GameScene: SKScene, SKPhysicsContactDelegate
{
var score = Int()
var scoreLabel = UILabel()
var fish = SKSpriteNode(imageNamed: "fish1 copy.png")
var shark = SKSpriteNode(imageNamed: "shark1 copy.png")
var food = SKSpriteNode(imageNamed: "fish game point copy.png")
override func didMoveToView(view: SKView)
{
physicsWorld.contactDelegate = self
self.scene?.backgroundColor = UIColor(red: 117/255.0, green: 208/255.0, blue: 224/255.0, alpha: 1.0)
fish.setScale(0.15)
fish.position = CGPointMake(self.frame.size.width * 0.1, self.frame.size.height * 0.5)
fish.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(35, 40))
fish.physicsBody?.affectedByGravity = false
fish.physicsBody?.categoryBitMask = physicsCategory.fish
fish.physicsBody?.contactTestBitMask = physicsCategory.shark
fish.physicsBody?.contactTestBitMask = physicsCategory.food
fish.physicsBody?.dynamic = false
self.addChild(fish)
var sharkTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("SpawnShark"), userInfo: nil, repeats: true)
var sharkTimer2 = NSTimer.scheduledTimerWithTimeInterval(2.33, target: self, selector: Selector("SpawnShark"), userInfo: nil, repeats: true)
var sharkTimer3 = NSTimer.scheduledTimerWithTimeInterval(2.79, target: self, selector: Selector("SpawnShark"), userInfo: nil, repeats: true)
var foodTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("SpawnFood"), userInfo: nil, repeats: true)
scoreLabel.text = "\(score)"
scoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
scoreLabel.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.3)
scoreLabel.textColor = UIColor.whiteColor()
self.view?.addSubview(scoreLabel)
}
func SpawnShark()
{
var shark = SKSpriteNode(imageNamed: "shark1 copy.png")
shark.setScale(0.33)
var MinValue = self.size.height - self.size.height
var MaxValue = self.size.height
var SpawnPoint = UInt32(MaxValue - MinValue)
shark.position = CGPoint(x: self.size.width , y: CGFloat(arc4random_uniform(SpawnPoint)))
let action = SKAction.moveToX(-70, duration: 3.0)
shark.runAction(SKAction.repeatActionForever(action))
let actionDone = SKAction.removeFromParent()
shark.runAction(SKAction.sequence([action, actionDone]))
shark.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(100, 20))
shark.physicsBody?.affectedByGravity = false
shark.physicsBody?.categoryBitMask = physicsCategory.shark
shark.physicsBody?.contactTestBitMask = physicsCategory.fish
shark.physicsBody?.dynamic = true
self.addChild(shark)
}
func SpawnFood()
{
var food = SKSpriteNode(imageNamed: "fish game point copy.png")
food.setScale(0.1)
var MinValue = self.size.height - self.size.height + 10
var MaxValue = self.size.height - 10
var SpawnPoint = UInt32(MaxValue - MinValue)
food.position = CGPoint(x: self.size.width , y: CGFloat(arc4random_uniform(SpawnPoint)))
let action = SKAction.moveToX(-70, duration: 5.0)
food.runAction(SKAction.repeatActionForever(action))
let actionDone = SKAction.removeFromParent()
food.runAction(SKAction.sequence([action, actionDone]))
food.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(10, 10))
food.physicsBody?.affectedByGravity = false
food.physicsBody?.categoryBitMask = physicsCategory.food
food.physicsBody?.contactTestBitMask = physicsCategory.fish
food.physicsBody?.dynamic = true
self.addChild(food)
}
func didBeginContact(contact: SKPhysicsContact)
{
var firstBody: SKPhysicsBody = contact.bodyA
var secondBody: SKPhysicsBody = contact.bodyB
if (firstBody.categoryBitMask == physicsCategory.fish) && (secondBody.categoryBitMask == physicsCategory.food) ||
(firstBody.categoryBitMask == physicsCategory.food) && (secondBody.categoryBitMask == physicsCategory.fish)
{
CollisionWithFood(firstBody.node as! SKSpriteNode, food: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == physicsCategory.shark) && (secondBody.categoryBitMask == physicsCategory.fish) ||
(firstBody.categoryBitMask == physicsCategory.fish) && (secondBody.categoryBitMask == physicsCategory.shark)
{
CollisionWithFish(firstBody.node as! SKSpriteNode, fish: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithFood(fish: SKSpriteNode, food: SKSpriteNode)
{
food.removeFromParent()
score++
scoreLabel.text = "\(score)"
}
func CollisionWithFish(shark: SKSpriteNode, fish: SKSpriteNode)
{
fish.removeFromParent()
scoreLabel.removeFromSuperview()
self.view?.presentScene(EndScene())
self.view?.presentScene(EndScene(), transition: SKTransition.crossFadeWithDuration(1.0))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
for touch: AnyObject in touches
{
let location = touch.locationInNode(self)
fish.position.y = location.y
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches
{
let location = touch.locationInNode(self)
fish.position.y = location.y
}
}
override func update(currentTime: CFTimeInterval)
{
/* Called before each frame is rendered */
}
}
我的第二个名为EndScene.swift的文件是程序进入的地方,如果鲨鱼得到你并且游戏结束。它是一个白色的屏幕,在屏幕的中上方有一个小的重启按钮,当你按下它时,应该带你回到游戏场景,但是会把你带到一个深棕色的。
这是我的EndScene.swift文件:
import Foundation
import SpriteKit
class EndScene: SKScene
{
var restartButton: UIButton!
override func didMoveToView(view: SKView)
{
scene?.backgroundColor = UIColor.whiteColor()
restartButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
restartButton.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 7)
restartButton.setTitle("Restart", forState: UIControlState.Normal)
restartButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
restartButton.addTarget(self, action: Selector("Restart"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(restartButton)
}
func Restart()
{
self.view?.presentScene(GameScene(), transition: SKTransition.crossFadeWithDuration(0.3))
restartButton.removeFromSuperview()
}
}
答案 0 :(得分:0)
在展示场景时,您应该将self.view
添加到GameScene
初始化
func Restart()
{
self.view?.presentScene(GameScene(size: view.frame.size), transition: SKTransition.crossFadeWithDuration(0.3))
restartButton.removeFromSuperview()
}