我已经构建了一个依赖于SKPhysics的游戏来检测玩家和随机生成的墙之间的碰撞。出于某种原因,SKPhysics didBeginContact()无效。我已经在其中添加了一个print语句,但它根本没有运行。我已将物理添加到wall类和播放器类,但结果是相同的。如果你发现任何突然出现的事,请告诉我。
代码:
GameScene.swift
//
// GameScene.swift
// marioRunner
//
// Created by nick on 11/18/15.
// Copyright (c) 2015 Supreme Leader. All rights reserved.
//
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var movingGround: MLMovingGround!
var hero: MLHero!
var wallGenerator: MLWallGenerator!
var isStarted = false
override func didMoveToView(view: SKView) {
backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255.5, blue: 244.0/255.0, alpha: 1.0)
movingGround = MLMovingGround(size: CGSizeMake(view.frame.width, 20))
movingGround.position = CGPointMake(0, view.frame.size.height/2)
addChild(movingGround)
self.hero = MLHero()
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
self.addChild(hero)
wallGenerator = MLWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
addChild(wallGenerator)
physicsWorld.contactDelegate = self
}
func start() {
isStarted = true
movingGround.start()
wallGenerator.startGeneratingWallsEvery(1)
hero.flip()
}
func gameOver() {
hero.stop()
}
func didBeginContact(contact: SKPhysicsContact) {
//gameOver()
hero.body.color = UIColor.orangeColor()
print("HIT")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isStarted == false{
start()
} else {
hero.flip()
}
}
override func update(currentTime: CFTimeInterval) {
}
}
Wall Class
//
// MLWall.swift
// marioRunner
//
// Created by nick on 12/9/15.
// Copyright © 2015 Supreme Leader. All rights reserved.
//
import Foundation
import SpriteKit
class MLWall: SKSpriteNode {
let WALL_WIDTH: CGFloat = 30.0
let WALL_HEIGHT: CGFloat = 50.0
let WALL_COLOR = UIColor.blackColor()
init() {
let size = CGSizeMake(WALL_WIDTH, WALL_HEIGHT)
super.init(texture: nil, color: WALL_COLOR, size: size)
loadPhysicsBodyWithSize(size)
startMoving()
}
func loadPhysicsBodyWithSize(size: CGSize) {
physicsBody = SKPhysicsBody(rectangleOfSize: size)
physicsBody?.categoryBitMask = wallCategory
physicsBody?.affectedByGravity = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func startMoving() {
let moveLeft = SKAction.moveByX(-300, y:0, duration:1)
runAction(SKAction.repeatActionForever(moveLeft))
}
}
英雄课
//
// MLHero.swift
// marioRunner
//
// Created by nick on 12/7/15.
// Copyright © 2015 Supreme Leader. All rights reserved.
//
import Foundation
import SpriteKit
class MLHero: SKSpriteNode {
var body: SKSpriteNode!
var arm: SKSpriteNode!
var leftFoot: SKSpriteNode!
var rightFoot: SKSpriteNode!
var isUpsideDown = false
init() {
super.init(texture: nil, color: UIColor.clearColor(), size: CGSizeMake(32, 44))
loadPhysicsBodyWithSize(size)
body = SKSpriteNode(color:UIColor.redColor(), size: CGSizeMake(self.frame.size.width, 40))
body.position = CGPointMake(0, 2)
addChild(body)
let skinColor = UIColor(red:207.0/255.0, green:193.0/255.0, blue: 168.0/255.0, alpha: 1.0)
let face = SKSpriteNode(color: skinColor, size: CGSizeMake(self.frame.size.width, 2))
face.position = CGPointMake(0, 6)
body.addChild(face)
let eyeColor = UIColor.whiteColor()
let leftEye = SKSpriteNode(color: eyeColor, size: CGSizeMake(6,6))
let rightEye = leftEye.copy() as! SKSpriteNode
let pupil = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(3,3))
pupil.position = CGPointMake(2, 0)
leftEye.addChild(pupil)
rightEye.addChild(pupil.copy() as! SKSpriteNode)
leftEye.position = CGPointMake(-4, 0)
face.addChild(leftEye)
rightEye.position = CGPointMake(14, 0)
face.addChild(rightEye)
}
/*func performOneRunCycle() {
let up = SKAction.moveByX(0, y: 2, duration: 0.05)
let down = SKAction.moveByX(0, y: -2, duration: 0.05)
}*/
func loadPhysicsBodyWithSize(size: CGSize) {
physicsBody = SKPhysicsBody(rectangleOfSize: size)
physicsBody?.categoryBitMask = heroCategory
physicsBody?.contactTestBitMask = wallCategory
physicsBody?.affectedByGravity = false
}
func flip() {
isUpsideDown = !isUpsideDown
var scale: CGFloat
if isUpsideDown {
scale = -1.0
} else {
scale = 1.0
}
let translate = SKAction.moveByX(0, y: scale*(size.height + kMLGroundHeight), duration: 0.1)
let flip = SKAction.scaleYTo(scale, duration: 0.1)
runAction(translate)
runAction(flip)
}
func stop() {
//body.removeAllActions()
print("stopped")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Constants.swift
//
// constants.swift
// marioRunner
//
// Created by nick on 12/10/15.
// Copyright © 2015 Supreme Leader. All rights reserved.
//
import Foundation
import UIKit
let kMLGroundHeight: CGFloat = 20.0
let kdefaultXToMovePerSecond: CGFloat = 320.0
let heroCategory: UInt32 = 0x1 << 0
let wallCategory: UInt32 = 0x1 << 1
谢谢, 尼克