首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用Xcode6 didBeginContact

未调用Xcode6 didBeginContact
EN

Stack Overflow用户
提问于 2015-09-18 20:32:18
回答 1查看 40关注 0票数 0

我一直试图让它在一个简单的平台游戏中工作,但接触检测给我带来了问题。当两个节点接触时,didBeginContact函数似乎没有被调用,下面是代码:

代码语言:javascript
复制
import SpriteKit
var idleFrames = [SKTexture]()
var walkFrames = [SKTexture]()
var player: SKSpriteNode!
var touchingGround = 0
var idling = 0

class GameScene: SKScene, SKPhysicsContactDelegate {
    var sp33d: CGVector = CGVectorMake(0.0,0.0)
    var jsp33d: CGFloat = 30
    var gameStick: Joystick?
    var ground: SKSpriteNode?
    let playerskin = character(charnumber: 1)

func didBeginContact(contact: SKPhysicsContact) {
    print("hit")
    let firstNode = contact.bodyA.node as! SKSpriteNode
    let secondNode = contact.bodyB.node as! SKSpriteNode

    if (contact.bodyA.categoryBitMask == category.player) && (contact.bodyB.categoryBitMask == category.ground) {
        touchingGround = 1
    }else{
        touchingGround = 0
    }
}
override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    self.physicsWorld.contactDelegate = self
    self.physicsWorld.gravity = CGVectorMake(0, -10.0)

    gameStick = Joystick()
    gameStick?.createJoystick(self.scene!.frame.width/4, nameBack: "joystick", nameMoving: "joystick1")
    gameStick!.backPart!.zPosition = 4
    gameStick!.movingPart!.zPosition = 5
    self.addChild(gameStick!.backPart!)
    self.addChild(gameStick!.movingPart!)

    player = self.childNodeWithName("player") as? SKSpriteNode
    ground = self.childNodeWithName("testGround") as? SKSpriteNode
    player?.physicsBody?.categoryBitMask = category.player
    ground?.physicsBody?.categoryBitMask = category.ground

    let idleAtlas = SKTextureAtlas(named: "idle.atlas")
    var idleframes = [SKTexture]()
    let numImages = idleAtlas.textureNames.count
    for var i=1; i<=4; i++ {
        let idleframe = "JohnIdle\(i)"
        idleframes.append(idleAtlas.textureNamed(idleframe))
    }
    idleFrames = idleframes
    let firstFrame = idleframes[0]
}

func idleflag() {
    idling = 0
    }

func idlejohn() {
    if sp33d.dx < 1 || sp33d.dx > -1{
        print("boop")
        idling = 1
        player?.runAction(SKAction.repeatAction(SKAction.animateWithTextures(idleFrames, timePerFrame: 0.4, resize: false, restore: true), count: 1))
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var nodeTouched = SKNode()
        nodeTouched = self.nodeAtPoint(location)
        if nodeTouched.name == "joystick1" {
            gameStick?.movingPart?.position = location
            if location.x > gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2{
                gameStick?.movingPart?.position = CGPointMake(gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2, gameStick!.movingPart!.position.y)
            }
            if location.y > gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2{
                gameStick?.movingPart?.position = CGPointMake(gameStick!.movingPart!.position.x, gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2)
            }
        }
    }
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var nodeTouched = SKNode()
        nodeTouched = self.nodeAtPoint(location)
        if nodeTouched.name == "joystick1" {
            gameStick?.movingPart?.position = location
            if location.x > gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2{
                gameStick?.movingPart?.position = CGPointMake(gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2, gameStick!.movingPart!.position.y)
            }
            if location.y > gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2{
                gameStick?.movingPart?.position = CGPointMake(gameStick!.movingPart!.position.x, gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2)
            }
        }
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var nodeTouched = SKNode()
        nodeTouched = self.nodeAtPoint(location)
        if nodeTouched.name == "joystick1" {
    let act = SKAction.moveTo(gameStick!.backPart!.position, duration: 0.2)
    gameStick?.movingPart?.runAction(act)
        }
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    let vX = gameStick!.movingPart!.position.x - gameStick!.backPart!.position.x

    var vY: CGFloat = gameStick!.movingPart!.position.y

    if  vY > gameStick!.backPart!.position.y + 20 && touchingGround == 1{
            sp33d = CGVectorMake(vX/13, jsp33d)
    }else{
            sp33d = CGVectorMake(vX/13, 0)
    }
    player?.physicsBody?.applyImpulse(sp33d) 

    idlejohn()
    }
}

下面是位掩码代码:

代码语言:javascript
复制
import Foundation
import SpriteKit

var charnumber = 0
var charsize = [0: CGSizeMake(80,80)]

struct category {
    static let player: UInt32 = 0x1 << 0
    static let ground: UInt32 = 0x1 << 1

}

任何帮助都将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-09-18 21:36:54

似乎你还没有初始化玩家和地面物理体

代码语言:javascript
复制
player = self.childNodeWithName("player") as? SKSpriteNode
ground = self.childNodeWithName("testGround") as? SKSpriteNode
player?.physicsBody?.categoryBitMask = category.player
ground?.physicsBody?.categoryBitMask = category.ground

player?.physicsBody = SKPhysicsBody() //在括号内选择主体类型(矩形、圆形等)地面也是如此,如果你想让物体接触,首先你需要指定身体。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32652126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档