2017-04-02 42 views
0

我想讓pacman從閃爍的原始位置重新啓動,即正在移動。如何使用已創建的SKSpriteNode與SpriteKit創建碰撞?

考慮到我已經宣佈它們,我該如何讓它們發生碰撞?

你移動pacman,但單獨閃爍的動作。我希望它像pacman遊戲一樣工作。

public class PacmanScene: SKScene { 

let playerSpeed: CGFloat = 40.0 
var pacman: SKSpriteNode? 
var playerTextures: [SKTexture] = [] 
var lastTouch: CGPoint? = nil 
var blinky: SKSpriteNode? 
var clyde: SKSpriteNode? 
var inky: SKSpriteNode? 
var pinky: SKSpriteNode? 
override public init(size: CGSize) { 
let pacmanTexture = SKTexture(imageNamed: "pacman01.png") 
    pacman = SKSpriteNode(texture: pacmanTexture) 
    pacman?.name = "pacman" 

    pacman?.position = CGPoint(x:30, y:30) 
    pacman?.zPosition = 1.0 
    pacman?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (pacman?.size.width)!, height: (pacman?.size.height)!)) 
    pacman?.physicsBody?.allowsRotation = true 
    pacman?.physicsBody?.affectedByGravity = false 
    pacman?.physicsBody?.mass = 2 
let blinkyTexture = SKTexture(imageNamed: "blinky.png") 
    blinky = SKSpriteNode(texture: blinkyTexture) 
    blinky?.name = "blinky" 

    blinky?.position = CGPoint(x: 15, y: 60) 
    blinky?.zPosition = 1.0 
    blinky?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (blinky?.size.width)!, height: (blinky?.size.height)!)) 
    blinky?.physicsBody?.allowsRotation = false 
    blinky?.physicsBody?.affectedByGravity = false 
    blinky?.physicsBody?.mass = 1000 

    super.init(size: size) 
    addChild(pacman!) 
    addChild(blinky!) 

    override public func didMove(to view: SKView) { 

    let bmoveUp = SKAction.moveBy(x: 0, y: 450, duration: 4.0) 

    let bmoveRight = SKAction.moveBy(x:20, y:0, duration: 1.0) 

    let bmoveDown = SKAction.moveBy(x:0, y: -450, duration: 4.0) 

    let bmoveLeft = SKAction.moveBy(x:-20, y:0, duration: 1.0) 


    let bsequence = SKAction.sequence([bmoveUp, bmoveRight, bmoveDown, bmoveLeft]) 

    let bendlessAction = SKAction.repeatForever(bsequence) 
    blinky?.run(bendlessAction) 
} 

回答

0

如果IV得到這個權利,你希望你的「Blinky」跟隨你的「吃豆子」要做到這一點,你就必須制定出吃豆子的位置,然後一個SKAction添加到您的Blinky移動到那個位置。

嘗試這樣的事情

//Speed blinky moves 
let blinkySpeed = 100 

override func update(_ currentTime: TimeInterval) { 
    // Called before each frame is rendered 
    updateBlinky() 
} 

func updateBlinky() { 
    //Set the point that blinky moves to 
    let point = CGPoint(x: pacman.position.x, y: pacman.position.y) 
    //Get the distance its got to travel 
    let distance = distanceBetweenPoints(first: pacman.position, second: blinky.position) 
    //Get the time is got to take from the speed and distance 
    let time = distance/blinkySpeed 
    //Create and run the action 
    let action = SKAction.move(to: point, duration: TimeInterval(time)) 
    blinky.run(action) 
} 

//work out the distance between the sprites 
func distanceBetweenPoints(first: CGPoint, second: CGPoint) -> Int { 
    return Int(hypot(second.x - first.x, second.y - first.y)) 
} 

最終的結果會是這樣的

The end result would be something like this

編輯:

好吧,我想從你的問題你已經有了的 「Blinky」移動你只是想檢測碰撞。

首先,你需要的SKPhysicsContactDelegate添加到您的類

public class PacmanScene: SKScene, SKPhysicsContactDelegate { 

然後你需要添加類別位掩碼到兩個精靈則處理衝突的didBegin(_聯繫人:SKPhysicsContact)方法。

我會做什麼

//Create Physics category struct 
struct PhysicsCategory { 
    static let pacman : UInt32 = 0x1 << 1 
    static var blinky : UInt32 = 0x1 << 2 
} 

然後你在哪裏設置了吃豆人和的Blinky設置類別位掩碼

//Set the category bit mask for pacman 
pacman?.physicsBody?.categoryBitMask = PhysicsCategory.pacman 
//Set what categories you want to test contact 
pacman?.physicsBody?.contactTestBitMask = PhysicsCategory.blinky 
//Set what categories you want to collide with each other 
pacman?.physicsBody?.collisionBitMask = PhysicsCategory.blinky 

//Set the category bit mask for blinky 
blinky?.physicsBody?.categoryBitMask = PhysicsCategory.blinky 
//Set what categories you want to test contact 
blinky?.physicsBody?.contactTestBitMask = PhysicsCategory.pacman 
//Set what categories you want to collide with each other 
blinky?.physicsBody?.collisionBitMask = PhysicsCategory.pacman 

那麼你就需要實現didBegin(_聯繫人:SKPhysicsContact)方法處理碰撞

func didBegin(_ contact: SKPhysicsContact) { 
    //Work out which contact was pacman 
    let other = contact.bodyA.categoryBitMask == PhysicsCategory.pacman ? contact.bodyB : contact.bodyA 
    //Test what it hit 
    switch other.categoryBitMask { 

     case PhysicsCategory.blinky: 
      print("pacman hit blinky") 
      //Move pacman 
      pacman?.position = CGPoint(x:30, y:30) 

     default: 
     break 

    } 
} 

希望這會有所幫助