- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试切换场景,但我的应用程序崩溃并出现此错误:
crash
crash
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
这是我切换场景的代码:
func switchscenes() {
if display >= 2 {
Player.removeFromParent()
PlayerRight.removeFromParent()
PlayerLeft.removeFromParent()
fireHair.removeFromParent()
fireHairRight.removeFromParent()
fireHairLeft.removeFromParent()
imageRightView.removeFromSuperview()
imageLeftView.removeFromSuperview()
println("crash")
var gameplayScene:SKScene = GamePlay(size: self.size)
println("crash")
self.view?.presentScene(gameplayScene)
println("crash")
display = 0
}
}
然后在 View 中加载:
timer5 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("switchscenes"), userInfo: nil, repeats: true)
打印崩溃两次但不是第三次显示
self.view?.presentScene(gameplayScene)
是错误的原因。我怎样才能解决这个问题?
编辑:
GamePlay.swift代码:
import SpriteKit
import Foundation
import UIKit
import iAd
//Collisions
struct PhysicsCategory {
static let Enemy : UInt32 = 1
static let Player : UInt32 = 2
static let PlayerRight : UInt32 = 3
static let PlayerLeft : UInt32 = 4
static let SuperBlock : UInt32 = 5
}
var transition:SKTransition = SKTransition.fadeWithDuration(0.5)
var superblockRandomizer = NSTimeInterval()
var timer8 : NSTimer!
var playerVelocity :CGFloat!
class GamePlay: SKScene, SKPhysicsContactDelegate {
var SuperBlock = SKShapeNode()
var redRandom : CGFloat = 1
var greenRandom : CGFloat = 1
var blueRandom : CGFloat = 1
//Highscore Variable
var Highscore = Int()
var fontsize : CGFloat = 20
//Score
var Score : Int = 0
var ScoreLabel = UILabel()
//Snow
var snow = SKEmitterNode(fileNamed: "SnowShower")
//Rain
var rain = SKEmitterNode(fileNamed: "RainShower")
//Fire Hair
var fireHair = SKEmitterNode(fileNamed: "FireHair")
//Fire Hair Right
var fireHairRight = SKEmitterNode(fileNamed: "FireHair")
//Fire Hair Left
var fireHairLeft = SKEmitterNode(fileNamed: "FireHair")
//Main Character
var Player = SKSpriteNode(imageNamed: "mainPlayer.png")
//Right-far character
var PlayerRight = SKSpriteNode(imageNamed: "mainPlayer.png")
//Left-far character
var PlayerLeft = SKSpriteNode(imageNamed: "mainPlayer.png")
//Holding vs Tapping Movement of Player
var isTouching = false
var touchXPosition:CGFloat = 0
override func didMoveToView(view: SKView) {
/* Setup your scene here */
playerVelocity = (size.width / 25)
let controller = self.view?.window?.rootViewController as? GameViewController
// Preload a new iAd
interAd = ADInterstitialAd()
interAd.delegate = controller
adLoaded = false
superblockRandomizer = NSTimeInterval(arc4random_uniform(5) + 5)
let spawnBlock = SKAction.sequence([SKAction.runBlock(spawnEnemies), SKAction.waitForDuration(0.3)])
runAction(SKAction.repeatActionForever(spawnBlock))
let runScore = SKAction.sequence([SKAction.runBlock(scoreCounter), SKAction.waitForDuration(0.1)])
runAction(SKAction.repeatActionForever(runScore))
let spawnSuperBlock = SKAction.sequence([SKAction.waitForDuration(superblockRandomizer), SKAction.runBlock(spawnSuperBlocks)])
runAction(SKAction.repeatActionForever(spawnSuperBlock))
snow.particleBirthRate = 15
fireHair.particleBirthRate = 200
fireHairRight.particleBirthRate = 200
fireHairLeft.particleBirthRate = 200
rain.particleBirthRate = 100
//Highscore
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
if (HighscoreDefault.valueForKey("Highscore") != nil) {
Highscore = HighscoreDefault.valueForKey("Highscore") as! NSInteger
}
else {
Highscore = 0
}
//Collisions/Physics
physicsWorld.contactDelegate = self
//Background Color
scene?.backgroundColor = UIColor.blackColor()
//Player coordinates
Player.position.x = size.width * 0.5
Player.position.y = size.height / 3
//Setting Player Sizes
Player.size.width = size.width * 0.11
Player.size.height = size.width * 0.11
PlayerRight.size.width = size.width * 0.11
PlayerRight.size.height = size.width * 0.11
PlayerLeft.size.width = size.width * 0.11
PlayerLeft.size.height = size.width * 0.11
//Setting hair position range
fireHair.particlePositionRange = CGVector(dx: size.width * 0.11, dy: 0)
fireHairRight.particlePositionRange = CGVector(dx: size.width * 0.11, dy: 0)
fireHairLeft.particlePositionRange = CGVector(dx: size.width * 0.11, dy: 0)
//Setting snow position range
snow.particlePositionRange = CGVector(dx: size.width, dy: 0)
//Initial position of player
Player.position = CGPoint(x: Player.position.x, y: Player.position.y)
//Initial position of far-right player
PlayerRight.position = CGPoint(x: Player.position.x + size.width, y: Player.position.y)
//Initial position of far-left player
PlayerLeft.position = CGPoint(x: Player.position.x - size.width, y: Player.position.y)
//Adding Physics/Collisions to Player
Player.physicsBody = SKPhysicsBody (rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy | PhysicsCategory.SuperBlock
Player.physicsBody?.dynamic = false
//Adding Physics/Collisions to PlayerRight
PlayerRight.physicsBody = SKPhysicsBody (rectangleOfSize: PlayerRight.size)
PlayerRight.physicsBody?.affectedByGravity = false
PlayerRight.physicsBody?.categoryBitMask = PhysicsCategory.PlayerRight
PlayerRight.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy | PhysicsCategory.SuperBlock
PlayerRight.physicsBody?.dynamic = false
//Adding Physics/Collisions to PlayerLeft
PlayerLeft.physicsBody = SKPhysicsBody (rectangleOfSize: PlayerRight.size)
PlayerLeft.physicsBody?.affectedByGravity = false
PlayerLeft.physicsBody?.categoryBitMask = PhysicsCategory.PlayerLeft
PlayerLeft.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy | PhysicsCategory.SuperBlock
PlayerLeft.physicsBody?.dynamic = false
//Making Players visible
self.addChild(Player)
self.addChild(PlayerRight)
self.addChild(PlayerLeft)
self.addChild(fireHair)
self.addChild(fireHairRight)
self.addChild(fireHairLeft)
if size.height <= 569 {
fontsize = 20
}
else if size.height >= 569 {
fontsize = 25
}
//Making Score Visible
ScoreLabel.text = "\(Score)"
ScoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: fontsize*5, height: fontsize))
ScoreLabel.font = UIFont(name: ScoreLabel.font.fontName, size: fontsize)
ScoreLabel.textColor = UIColor.whiteColor()
self.view?.addSubview(ScoreLabel)
}
func scoreCounter() {
//Setting score
Score += 1
ScoreLabel.text = "\(Score)"
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
//Checking for Player to enemy collisions
if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.Player)){
CollisionWithEnemy(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.Player) && (secondBody.categoryBitMask == PhysicsCategory.Enemy) {
CollisionWithEnemy2(firstBody.node as! SKSpriteNode, Enemy: secondBody.node as! SKShapeNode)
}
//Checking for PlayerRight to enemy collisions
if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.PlayerRight)){
CollisionWithEnemy(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.PlayerRight) && (secondBody.categoryBitMask == PhysicsCategory.Enemy) {
CollisionWithEnemy2(firstBody.node as! SKSpriteNode, Enemy: secondBody.node as! SKShapeNode)
}
//Checking for PlayerLeft to enemy collisions
if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.PlayerLeft)){
CollisionWithEnemy(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.PlayerLeft) && (secondBody.categoryBitMask == PhysicsCategory.Enemy) {
CollisionWithEnemy2(firstBody.node as! SKSpriteNode, Enemy: secondBody.node as! SKShapeNode)
}
if SuperBlock.position.y > size.height / 2 {
//Checking for enemy to enemy collisions
if ((firstBody.categoryBitMask == PhysicsCategory.SuperBlock) && (secondBody.categoryBitMask == PhysicsCategory.Enemy)){
SuperBlockWithEnemy(firstBody.node as! SKShapeNode, Enemy: secondBody.node as! SKShapeNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.SuperBlock) {
SuperBlockWithEnemy2(firstBody.node as! SKShapeNode, SuperBlock: secondBody.node as! SKShapeNode)
}
}
//Checking for Player to superblock collisions
if ((firstBody.categoryBitMask == PhysicsCategory.SuperBlock) && (secondBody.categoryBitMask == PhysicsCategory.Player)){
CollisionWithSuper(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.Player) && (secondBody.categoryBitMask == PhysicsCategory.SuperBlock) {
CollisionWithSuper2(firstBody.node as! SKSpriteNode, SuperBlock: secondBody.node as! SKShapeNode)
}
//Checking for PlayerRight to superblock collisions
if ((firstBody.categoryBitMask == PhysicsCategory.SuperBlock) && (secondBody.categoryBitMask == PhysicsCategory.PlayerRight)){
CollisionWithSuper(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.PlayerRight) && (secondBody.categoryBitMask == PhysicsCategory.SuperBlock) {
CollisionWithSuper2(firstBody.node as! SKSpriteNode, SuperBlock: secondBody.node as! SKShapeNode)
}
//Checking for PlayerLeft to superblock collisions
if ((firstBody.categoryBitMask == PhysicsCategory.SuperBlock) && (secondBody.categoryBitMask == PhysicsCategory.PlayerLeft)){
CollisionWithSuper(firstBody.node as! SKShapeNode, Player: secondBody.node as! SKSpriteNode)
}
else if (firstBody.categoryBitMask == PhysicsCategory.PlayerLeft) && (secondBody.categoryBitMask == PhysicsCategory.SuperBlock) {
CollisionWithSuper2(firstBody.node as! SKSpriteNode, SuperBlock: secondBody.node as! SKShapeNode)
}
}
func CollisionWithEnemy(Enemy: SKShapeNode, Player: SKSpriteNode) {
//Highscore
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Score, forKey: "Score")
ScoreDefault.synchronize()
if (Score > Highscore) {
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
HighscoreDefault.setValue(Score, forKey: "Highscore")
}
var gameOver:SKScene = GameOverScene(size: self.size)
ScoreLabel.removeFromSuperview()
Player.removeFromParent()
self.view?.presentScene(gameOver, transition: transition)
}
func CollisionWithEnemy2(Player: SKSpriteNode, Enemy: SKShapeNode) {
//Highscore
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Score, forKey: "Score")
ScoreDefault.synchronize()
if (Score > Highscore) {
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
HighscoreDefault.setValue(Score, forKey: "Highscore")
}
var gameOver:SKScene = GameOverScene(size: self.size)
ScoreLabel.removeFromSuperview()
Player.removeFromParent()
self.view?.presentScene(gameOver, transition: transition)
}
func SuperBlockWithEnemy(SuperBlock: SKShapeNode, Enemy: SKShapeNode) {
Enemy.removeFromParent()
}
func SuperBlockWithEnemy2(Enemy: SKShapeNode, SuperBlock: SKShapeNode) {
Enemy.removeFromParent()
}
func CollisionWithSuper(SuperBlock: SKShapeNode, Player: SKSpriteNode) {
//Highscore
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Score, forKey: "Score")
ScoreDefault.synchronize()
if (Score > Highscore) {
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
HighscoreDefault.setValue(Score, forKey: "Highscore")
}
var gameOver:SKScene = GameOverScene(size: self.size)
ScoreLabel.removeFromSuperview()
Player.removeFromParent()
self.view?.presentScene(gameOver, transition: transition)
}
func CollisionWithSuper2(Player: SKSpriteNode, SuperBlock: SKShapeNode) {
//Highscore
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Score, forKey: "Score")
ScoreDefault.synchronize()
if (Score > Highscore) {
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
HighscoreDefault.setValue(Score, forKey: "Highscore")
}
var gameOver:SKScene = GameOverScene(size: self.size)
ScoreLabel.removeFromSuperview()
Player.removeFromParent()
self.view?.presentScene(gameOver, transition: transition)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
isTouching = true
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
touchXPosition = location.x
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
isTouching = false
}
func spawnSuperBlocks() {
superblockRandomizer = NSTimeInterval(arc4random_uniform(5) + 5)
//Randomizing width of blocks
var blockWidth = Int(arc4random_uniform(UInt32(size.width / 3)) + UInt32(size.width / 3))
//Min and Max position of blocks
var minPosition : UInt32 = UInt32(blockWidth / 2)
var maxPosition : UInt32 = UInt32(size.width - CGFloat(blockWidth / 2))
//Randomizing Block Position
var blockXPosition = arc4random_uniform(maxPosition - minPosition) + minPosition
//Making Blocks
SuperBlock = SKShapeNode(rectOfSize: CGSize(width: blockWidth, height: Int(size.height / 12)))
SuperBlock.position = CGPointMake (CGFloat(blockXPosition), CGFloat(size.height+size.height / 5))
//Coloring Blocks
redRandom = CGFloat(arc4random_uniform(200) + 56)
greenRandom = CGFloat(arc4random_uniform(200) + 56)
blueRandom = CGFloat(arc4random_uniform(200) + 56)
SuperBlock.fillColor = SKColor(red: redRandom / 255, green: greenRandom / 255, blue: blueRandom / 255, alpha: 1)
SuperBlock.lineWidth = 0
//Moving Blocks
let action = SKAction.moveToY(-50, duration: 1)
//Removing blocks once off screen
let actionDone = SKAction.removeFromParent()
//Running the above actions
SuperBlock.runAction(SKAction.sequence([action, actionDone]))
//Physics/Collisions
SuperBlock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize (width: blockWidth, height: 50))
SuperBlock.physicsBody?.categoryBitMask = PhysicsCategory.SuperBlock
SuperBlock.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
SuperBlock.physicsBody?.affectedByGravity = false
SuperBlock.physicsBody?.dynamic = true
//Adding enemy to scene
self.addChild(SuperBlock)
}
func spawnEnemies() {
//Randomizing width of blocks
var blockWidth = Int(arc4random_uniform(UInt32(size.width / 3)) + UInt32(size.width / 4))
//Min and Max position of blocks
var minPosition : UInt32 = UInt32(blockWidth / 2)
var maxPosition : UInt32 = UInt32(size.width - CGFloat(blockWidth / 2))
//Randomizing Block Position
var blockXPosition = arc4random_uniform(maxPosition - minPosition) + minPosition
var sideblockPositionMin = UInt32(blockWidth / 2)
var sideblockPositionMax = UInt32(size.width - CGFloat(blockWidth / 2))
var randomizer2 = arc4random_uniform(10)
if randomizer2 == 5 {
blockXPosition = sideblockPositionMin
}
else if randomizer2 == 7 {
blockXPosition = sideblockPositionMax
}
else {
}
//Making Blocks
var Enemy = SKShapeNode(rectOfSize: CGSize(width: blockWidth, height: Int(size.height / 100)))
Enemy.position = CGPointMake (CGFloat(blockXPosition), CGFloat(size.height+size.height / 12))
//Coloring Blocks
Enemy.fillColor = SKColor.whiteColor()
//Moving Blocks
let action = SKAction.moveToY(-50, duration: 1.5)
//Removing blocks once off screen
let actionDone = SKAction.removeFromParent()
//Running the above actions
Enemy.runAction(SKAction.sequence([action, actionDone]))
//Physics/Collisions
Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize (width: blockWidth, height: 1))
Enemy.physicsBody?.categoryBitMask = PhysicsCategory.Enemy
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.dynamic = true
//Adding enemy to scene
self.addChild(Enemy)
}
func spawn() {
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
Player.position = CGPoint(x: Player.position.x, y: Player.position.y)
PlayerRight.position = CGPoint(x: Player.position.x + size.width, y: Player.position.y)
PlayerLeft.position = CGPoint(x: Player.position.x - size.width, y: Player.position.y)
fireHair.position = CGPoint(x: Player.position.x, y: Player.position.y - Player.size.height / 1.5)
fireHairRight.position = CGPoint(x: Player.position.x + size.width, y: Player.position.y - Player.size.height / 1.5)
fireHairLeft.position = CGPoint(x: Player.position.x - size.width, y: Player.position.y - Player.size.height / 1.5)
if isTouching {
if touchXPosition > self.size.width / 2 {
// move character to the right.
Player.position.x += playerVelocity
}
if touchXPosition < self.size.width / 2 {
// move character to the left.
Player.position.x -= playerVelocity
}
}
if Player.position.x < 0 {
Player.position.x = Player.position.x + size.width
PlayerRight.position.x = Player.position.x + size.width
PlayerLeft.position.x = Player.position.x - size.width
}
if Player.position.x > size.width {
Player.position.x = Player.position.x - size.width
PlayerRight.position.x = Player.position.x + size.width
PlayerLeft.position.x = Player.position.x - size.width
}
}
}
最佳答案
几天后终于弄明白了。我想在开发初期做一个凉爽的雨/雪效果,但我最终决定反对。结果我仍然有几行与雨雪相关的代码,但它们正在加载我已删除的 nil SKEmitterNode(文件名为:)。
关于ios - self.view?.presentScene : unexpectedly found nil while unwrapping an Optional value (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972518/
让我们写一个简单的类在我的脑海中解释: class SomeClass { var happyToUsed = 10 } 并创建一个对象 let someObject = SomeClass(
采用 self 的方法与采用 &self 甚至 &mut self 的方法有什么区别? 例如 impl SomeStruct { fn example1(self) { } fn ex
请观察以下代码(Win10上的python 3.6,PyCharm),函数thread0(self)作为线程成功启动,但是 thread1(self)似乎与thread0(self)不同已设置。 se
backbone.js 开始于: //Establish the root object, `window` (`self`) in the browser, or `global` on the s
做的事: self = self.init; return self; 在 Objective-C 中具有相同的效果: self.init() 快速? 例如,在这种情况下: else if([form
我查看了关于堆栈溢出的一些关于使用[weak self]和[unowned self]的问题的评论。我需要确保我理解正确。 我正在使用最新的 Xcode - Xcode 13.4,最新的 macOS
我面临在以下模型类代码中的 self.init 调用或分配给 self 之前使用 self 的错误tableview单元格项目,它发生在我尝试获取表格单元格项目的文档ID之后。 应该做什么?请推荐。
晚上好。 我对在 Swift 中转义(异步)闭包有疑问,我想知道哪种方法是解决它的最佳方法。 有一个示例函数。 func exampleFunction() { functionWithEsca
我需要在内心深处保持坚强的自我。 我知道声明[weak self]就够了外封闭仅一次。 但是guard let self = self else { return }呢? ,是否也足以为外部闭包声明一
代码 use std::{ fs::self, io::self, }; fn rmdir(path: impl AsRef) -> io::Result { fs::remo
我检查了共享相同主题的问题,但没有一个解决我遇到的这种奇怪行为: 说我有一个简单的老学校struct : struct Person { var name: String var age:
我应该解释为什么我的问题不是重复的:TypeError: can only concatenate list (not “str”) to list ...所以它不是重复的,因为该帖子处理代码中出现的
我有一个 trait,它接受一个类型参数,我想说实现这个 trait 的对象也会符合这个类型参数(使用泛型,为了 Java 的兼容性) 以下代码: trait HandleOwner[SELF
这个问题在这里已经有了答案: Why would a JavaScript variable start with a dollar sign? [duplicate] (16 个答案) 关闭 8
我总是找到一些类似的代码newPromise.promiseDispatch.apply(newPromise, message),我不明白为什么不使用newPromise.promiseDispat
我看到类似的模式 def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... 非
mysql查询示例: SELECT a1.* FROM agreement a1 LEFT JOIN agreement a2 on a1.agreementType = a2.agreementTy
self.delegate = self; 这样做有什么问题吗?正确的做法是什么? 谢谢,尼尔。 代码: (UITextField*)initWith:(id)sender:(float)X:(flo
为什么要声明self在类中需要的结构中不需要?我不知道是否还有其他例子说明了这种情况,但在转义闭包的情况下,确实如此。如果闭包是非可选的(因此是非转义的),则不需要声明 self在两者中的任何一个。
这个问题已经有答案了: What does the ampersand (&) before `self` mean in Rust? (1 个回答) 已关闭去年。 我不清楚 self 之间有什么区别
我是一名优秀的程序员,十分优秀!