当我制作一个UIButton,并且该功能呈现了" GameScene"时,它只是进入一个看起来像我的播放器之一的绿色场景,但真的很近。
这是GameScene代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
var Score = Int()
var player = SKSpriteNode(imageNamed: "player")
var ScoreLabel = UILabel()
override func didMoveToView(view: SKView) {
self.view?.backgroundColor = UIColor.blueColor()
physicsWorld.contactDelegate = self
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody?.affectedByGravity = false
player.physicsBody?.categoryBitMask = PhysicsCatagory.player
player.physicsBody?.contactTestBitMask = PhysicsCatagory.Chlorine
player.physicsBody?.dynamic = false
player.position = CGPointMake( self.size.width/2, self.size.height/10)
self.addChild(player)
var stimer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnShot"), userInfo: nil, repeats: true)
var Ctimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("spawnChlorine"), userInfo: nil, repeats: true )
ScoreLabel.text = "\(Score)"
ScoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
ScoreLabel.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 7)
//ScoreLabel.backgroundColor = UIColor.whiteColor()
ScoreLabel.textColor = UIColor.blackColor()
//ScoreLabel.font = UIFont.smallSystemFontSize(30)
self.view?.addSubview(ScoreLabel)
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCatagory.Chlorine) && (secondBody.categoryBitMask == PhysicsCatagory.Shot)) ||
((firstBody.categoryBitMask == PhysicsCatagory.Shot) && (secondBody.categoryBitMask == PhysicsCatagory.Chlorine)){
collisionWithShot((firstBody.node as? SKSpriteNode)!, Shot: (secondBody.node as? SKSpriteNode)!)
}else if ((firstBody.categoryBitMask == PhysicsCatagory.Chlorine) && (secondBody.categoryBitMask == PhysicsCatagory.player)) ||
((firstBody.categoryBitMask == PhysicsCatagory.player) && (secondBody.categoryBitMask == PhysicsCatagory.Chlorine)){
collisionWithPlayer(firstBody.node as! SKSpriteNode, player: secondBody.node as! SKSpriteNode)
}
}
func collisionWithShot (Chlorine: SKSpriteNode, Shot: SKSpriteNode){
Chlorine.removeFromParent()
Shot.removeFromParent()
Score++
ScoreLabel.text = ("Score: \(Score)")
}
func collisionWithPlayer (Chlorine: SKSpriteNode, player: SKSpriteNode){
player.removeFromParent()
Chlorine.removeFromParent()
ScoreLabel.removeFromSuperview()
self.view?.presentScene(EndScene())
}
func spawnShot(){
var Shot = SKSpriteNode(imageNamed: "Shot")
zPosition = -5
Shot.size = CGSize(width: 30, height: 30)
Shot.position = CGPointMake(player.position.x, player.position.y)
let action = SKAction.moveToY(self.size.height + 30, duration: 0.8)
let actionDone = SKAction.removeFromParent()
Shot.runAction(SKAction.sequence([action, actionDone]))
Shot.physicsBody = SKPhysicsBody(rectangleOfSize: Shot.size)
Shot.physicsBody?.categoryBitMask = PhysicsCatagory.Shot
Shot.physicsBody?.contactTestBitMask = PhysicsCatagory.Chlorine
Shot.physicsBody?.affectedByGravity = false
Shot.physicsBody?.dynamic = false
self.addChild(Shot)
}
var stimer:NSTimer!
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
player.position.x = location.x
}
}
func spawnChlorine(){
var Chlorine = SKSpriteNode(imageNamed: "Chlorine.png")
var minValue = self.size.width / 8
var maxValue = self.size.width - 20
var spawnPoint = UInt32((maxValue - minValue))
Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (spawnPoint )) ,y: self.size.height)
let action = SKAction.moveToY(-20, duration: 8.0)
let actionDone = SKAction.removeFromParent()
Chlorine.runAction(SKAction.sequence([action, actionDone]))
Chlorine.runAction(SKAction.repeatActionForever(action))
Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize: Chlorine.size)
Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine
Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot
Chlorine.physicsBody?.affectedByGravity = false
Chlorine.physicsBody?.dynamic = true
self.addChild(Chlorine)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
player.position.x = location.x
//player.position.y = location.y
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
以下是玩家死亡时呈现的场景:
//
// EndScene.swift
// Algae Escape
//
// Created by Alex Walker on 8/24/15.
// Copyright (c) 2015 Alex Walker. All rights reserved.
//
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()
}
}