2017-04-02 92 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 

    } 
} 

希望这会有所帮助