Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 精灵之间的碰撞检测赢得';行不通_Swift_Sprite_Collision - Fatal编程技术网

Swift 精灵之间的碰撞检测赢得';行不通

Swift 精灵之间的碰撞检测赢得';行不通,swift,sprite,collision,Swift,Sprite,Collision,我有三个移动的精灵。一个是球,一个是线,一个是三角形。当球击中线时,我想进行score++操作,然后使用addBall()函数生成另一个球。当球击中三角形时,我想结束比赛 下面是我的代码。球直接穿过球和三角形,没有任何碰撞。谁能给我指出正确的方向吗 import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { var center = SKSpriteNode() var center2 = SKSpr

我有三个移动的精灵。一个是球,一个是线,一个是三角形。当球击中线时,我想进行score++操作,然后使用addBall()函数生成另一个球。当球击中三角形时,我想结束比赛

下面是我的代码。球直接穿过球和三角形,没有任何碰撞。谁能给我指出正确的方向吗

import SpriteKit




class GameScene: SKScene, SKPhysicsContactDelegate  {



var center = SKSpriteNode()
var center2 = SKSpriteNode()
var centerLine = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
var score = 0
var setCenter = SKAction.rotateByAngle(CGFloat(-1.75), duration:1)
var setCenter2 = SKAction.rotateByAngle(CGFloat(1.75), duration:1)


struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let ball       : UInt32 = 0x1         // 1
    static let triangle   : UInt32 = 0x1 << 1    // 2
    static let line      : UInt32 = 0x1 << 2    // 4
}









    override func didMoveToView(view: SKView) {

    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")



//Background
    var bgTexture = SKTexture(imageNamed: "images/bg.png")
    bg = SKSpriteNode(texture:bgTexture)
    bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    bg.zPosition = -4
    self.addChild(center)

//Center Circle
    var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
    bigCircle = SKSpriteNode(texture:bigCircleTexture)
    bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    self.addChild(bigCircle)
    bigCircle.zPosition = -3
//Center Triangle
    var centerTexture = SKTexture(imageNamed: "center.png")
    center = SKSpriteNode(texture:centerTexture)
    center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    center.zPosition = -1
    center.physicsBody = SKPhysicsBody(rectangleOfSize: center.size)
    center.physicsBody?.categoryBitMask = PhysicsCategory.triangle
    self.addChild(center)
    center.runAction(setCenter)
    center.removeAllActions()
    spin = clockwise
    center.runAction(SKAction.repeatActionForever(spin))
//Center Triangle 2
        var centerTexture2 = SKTexture(imageNamed: "center.png")
        center2 = SKSpriteNode(texture:centerTexture)
        center2.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        center2.zPosition = -1
        center2.physicsBody = SKPhysicsBody(rectangleOfSize: center2.size)
        center2.physicsBody?.categoryBitMask = PhysicsCategory.triangle
        self.addChild(center)
        center2.runAction(setCenter2)
        center2.removeAllActions()
        spin = clockwise
        center.runAction(SKAction.repeatActionForever(spin))

//Center Line
        var centerLineTexture = SKTexture(imageNamed: "centerLine.png")
        centerLine = SKSpriteNode(texture:centerLineTexture)
        centerLine.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) + center.size.height)
        centerLine.zPosition = -2
        centerLine.physicsBody = SKPhysicsBody(rectangleOfSize: centerLine.size)
        centerLine.physicsBody?.categoryBitMask = PhysicsCategory.line
        self.addChild(centerLine)
        spin = clockwise
        centerLine.runAction(SKAction.repeatActionForever(spin))

//Create Balls

    //Ball Settings
    func randomCGFloat() -> CGFloat {
            return CGFloat(Double(arc4random())/Double(UInt32.max) )
        }

        var ball = SKSpriteNode(imageNamed: "newBall.png")

        var randomBall = arc4random_uniform(4) + 1
        var moveBall = SKAction.moveTo(CGPointMake(self.size.width/2,self.size.height/2), duration:3.0)
        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)
        ball.physicsBody?.dynamic = false
        ball.physicsBody?.allowsRotation = false
        ball.physicsBody?.categoryBitMask = PhysicsCategory.ball
        ball.physicsBody?.collisionBitMask = PhysicsCategory.triangle
        ball.physicsBody?.contactTestBitMask = PhysicsCategory.triangle
        ball.physicsBody?.collisionBitMask = PhysicsCategory.line

        //Initial Ball
        if score == 0 {
            ball.position = CGPointMake(self.size.width/2, self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }



        //Spawning and Moving Balls


        func addBall() {


        if randomBall == 1 {
            ball.position = CGPointMake(self.size.width/2, self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        else if randomBall == 2 {
            ball.position = CGPointMake(self.size.width,     self.size.height/2)
            self.addChild(ball)
            ball.runAction(moveBall)
         }

        else if randomBall == 3{
            ball.position = CGPointMake(self.size.width/2, -self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        else if randomBall == 4 {
            ball.position = CGPointMake(self.size.width - self.size.width, self.size.height/2)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        }



        func didBeginContact(contact: SKPhysicsContact) {
            if contact.bodyA.categoryBitMask == PhysicsCategory.line     {
                score++
                println("\(score)")

            } else if contact.bodyA.categoryBitMask == PhysicsCategory.triangle {
                println("lost")
            }
        }





}







override func touchesBegan(touches: NSSet, withEvent event: UIEvent)     {
    /* Called when a touch begins */
    if spin == clockwise  {
        center.removeAllActions()
        centerLine.removeAllActions()
        spin = counterClockwise



    }

    else {
        center.removeAllActions()
        centerLine.removeAllActions()
        spin  = clockwise

    }
    center.runAction(SKAction.repeatActionForever(spin))
    centerLine.runAction(SKAction.repeatActionForever(spin))
}


override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}
导入SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
var center=SKSpriteNode()
var center2=SKSpriteNode()
var中心线=SKSpriteNode()
var bg=SKSpriteNode()
var bigcirle=SKSpriteNode()
逆时针方向=SKAction.rotateByAngle(CGFloat(3.14),持续时间:1)
顺时针=SKAction.rotateByAngle(CGFloat(-3.14),持续时间:1)
var spin=SKAction()
风险值得分=0
var setCenter=SKAction.rotateByAngle(CGFloat(-1.75),持续时间:1)
var setCenter2=SKAction.rotateByAngle(CGFloat(1.75),持续时间:1)
结构物理分类{
静态let None:UInt32=0
静态let All:UInt32=UInt32.max
静态let-ball:UInt32=0x1//1

静态let triangle:UInt32=0x1首先,您需要为每个要与物理(重力、碰撞等)交互的对象设置一个
SKPhysicsBody
。当前直线和三角形不是这样


然后,分组(类别位掩码)设置不正确。确实,
0感谢您的响应。一切都有意义,我知道我的代码为什么不工作,但现在我遇到了一个新问题。当我尝试运行应用程序时,生成成功,但立即失败。我在AppDelegate.swift文件中看到一个黄色图标和一条绿色线,突出显示AppDelegate类,并显示“signal SIGBRT”。下面是我的代码。我不确定我这样更改结果时犯了什么错误。我正在尝试自己修复,但感谢他人的帮助。您可能希望发布新代码而不是旧代码。(确保没有错误地放置断点)抱歉-在我完成注释之前按enter键。我在pastbin中添加了代码,并且还将更新原始代码。您应该始终尝试保持“干净”代码。如果您提供的代码是您使用的代码,则您在函数内部无故拥有函数。a尝试清理一点()但我看不出它是否有任何错误。而且,为什么需要在UIDevice上设置方向?请尝试使用我在上面的注释中提供的代码(在编译之前可能会有一些错误需要清除)。如果它不起作用,我可能会晚一点看一看,但如果可能,您必须上载项目(因此我可以编译(所有文件、图像等)。
struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let Ball       : UInt32 = 0b1       // 1
    static let Triangle   : UInt32 = 0b10      // 2
    static let Line       : UInt32 = 0b100     // 4
}
struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let Ball       : UInt32 = 0x1         // 1
    static let Triangle   : UInt32 = 0x1 << 1    // 2
    static let Line       : UInt32 = 0x1 << 2    // 4
}