1

我有一个非常简单的用于 mac os 的 sprite kit 应用程序。(顺便说一句,此 iOS 代码工作正常)。AppDelegate 代码:

import Cocoa
import SpriteKit

extension SKNode {
    class func unarchiveFromFile(file : String) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!
@IBOutlet weak var skView: SKView!

func applicationDidFinishLaunching(aNotification: NSNotification) {
    /* Pick a size for the scene */
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        self.skView!.presentScene(scene)

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        self.skView!.ignoresSiblingOrder = true

        self.skView!.showsFPS = true
        self.skView!.showsNodeCount = true
    }
}

func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool {
    return true
}
}

以及场景代码:

import Foundation
import SpriteKit

struct Detection {
    static var no : UInt32 = 0
    static var all : UInt32 = UInt32.max
    static var monster : UInt32 = 0b1
    static var suric : UInt32 = 0b10
    static var ninja : UInt32 = 0b100
}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(#min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}


func + (left: CGPoint, right: CGPoint) -> CGPoint {
    return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

func - (left: CGPoint, right: CGPoint) -> CGPoint {
    return CGPoint(x: left.x - right.x, y: left.y - right.y)
}

func * (point: CGPoint, scalar: CGFloat) -> CGPoint {
    return CGPoint(x: point.x * scalar, y: point.y * scalar)
}

func / (point: CGPoint, scalar: CGFloat) -> CGPoint {
    return CGPoint(x: point.x / scalar, y: point.y / scalar)
}

#if !(arch(x86_64) || arch(arm64))
func sqrt(a: CGFloat) -> CGFloat {
return CGFloat(sqrtf(Float(a)))
}
#endif

extension CGPoint {
func length() -> CGFloat {
    return sqrt(x*x + y*y)
}

func normalized() -> CGPoint {
    return self / length()
}
}


class GameScene: SKScene, SKPhysicsContactDelegate {

let player = SKSpriteNode(imageNamed: "player.png")

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.whiteColor()

    physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    physicsWorld.contactDelegate = self
    player.position = CGPoint(x: self.size.width * 0.1, y: self.size.height / 2)
    addChild(player)


    runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(createMonster), SKAction.waitForDuration(1)])))
}

override func mouseDown(theEvent: NSEvent) {
    let location = theEvent.locationInNode(self)

    let suric = SKSpriteNode(imageNamed: "projectile.png")
    suric.position = player.position
    suric.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
    suric.physicsBody?.categoryBitMask = Detection.suric
    suric.physicsBody?.collisionBitMask = Detection.no
    suric.physicsBody?.contactTestBitMask = Detection.monster
    suric.physicsBody?.usesPreciseCollisionDetection = true
    suric.physicsBody?.dynamic = true
    suric.physicsBody?.angularVelocity = -10.0

    let offset = location - suric.position

    if offset.x < 0 {
        return
    }

    addChild(suric)

    let direc = offset.normalized()
    let shoot = direc * 1000
    let dest = shoot + suric.position

    let move = SKAction.moveTo(dest, duration: 2.0)
    let stop = SKAction.removeFromParent()
    suric.runAction(SKAction.sequence([move, stop]))

}

func suricHit(suric : SKSpriteNode?, monster : SKSpriteNode?) {
    if suric != nil && monster != nil {
        suric!.removeFromParent()
        monster!.removeFromParent()
    }
}

func didBeginContact(contact: SKPhysicsContact) {
    var first : SKPhysicsBody
    var second : SKPhysicsBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        first = contact.bodyA
        second = contact.bodyB
    }
    else {
        first = contact.bodyB
        second = contact.bodyA
    }

    if (first.categoryBitMask & Detection.monster != 0) && (second.categoryBitMask & Detection.suric != 0) {
        suricHit(first.node as? SKSpriteNode, monster: second.node as? SKSpriteNode)
    }
}

func createMonster() {
    let monster = SKSpriteNode(imageNamed: "monster.png")

    let y = random(min: monster.size.height / 2, size.height - monster.size.height)
    monster.position = CGPoint(x: self.size.width + monster.size.width / 2, y: y)
    monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.size, center: CGPoint(x: monster.position.x / 2, y: monster.position.y))
    monster.physicsBody?.usesPreciseCollisionDetection = true
    monster.physicsBody?.categoryBitMask = Detection.monster
    monster.physicsBody?.contactTestBitMask = Detection.suric
    monster.physicsBody?.collisionBitMask = Detection.no
    monster.physicsBody?.dynamic = true
    addChild(monster)

    let duration = random(min: 2.0, 4.0)
    let move = SKAction.moveTo(CGPoint(x: -monster.size.width / 2, y: y), duration: NSTimeInterval(duration))
    let done = SKAction.removeFromParent()

    monster.runAction(SKAction.sequence([move, done]))
}
}

并且联系功能的工作方式非常奇怪。suric即使和之间没有实际接触,它也会运行monster。我不知道为什么会这样。这只是我的错还是只是 Xcode 错误?

4

1 回答 1

1

你的弹丸物理体太大。节点通过物理实体而不是它们的实际大小进行接触。

suric.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)

将物理体的大小更改为更小的东西

self.size.width / 2

像这样的东西

suric.physicsBody = SKPhysicsBody(circleOfRadius: suric.size);
于 2015-04-23T06:46:39.583 回答