2017-04-02 10 views
0

팩맨은 blinky와 충돌 할 때 원래 위치에서 다시 시작하기를 원합니다.이미 생성 된 SKSpriteNode를 사용하여 SpriteKit과 충돌을 만드는 방법은 무엇입니까?

내가 이미 선언했다고 생각하면 어떻게 충돌시킬 수 있습니까?

팩맨을 움직이지만 깜박 거리며 움직입니다. 나는 그것을 팩맨 게임처럼 일하고 싶습니다.

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는 해당 위치로 이동 당신이 당신의 "블링키는"다음 블링키에 SKAction를 추가하면 팩맨의 위치를 ​​해결해야이 작업을 수행하는 당신의 "팩맨"를 따르십시오이 권리를 가지고있는 경우 .

가 좋아 난 당신이 이미 "블링키"이 질문에서 생각이

//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

편집과 같은 것 움직 이기만하면 충돌을 감지하고 싶을뿐입니다. 방법 :

먼저 당신이

public class PacmanScene: SKScene, SKPhysicsContactDelegate { 

는 그런 다음 두 스프라이트 다음 (SKPhysicsContact _ 접점) didBegin의 충돌을 처리하는 범주 비트 마스크를 추가 할 필요가 클래스에 SKPhysicsContactDelegate를 추가해야합니다.

당신은 팩맨을 설정하는 경우 블링키 당신이 didBegin을 구현해야

//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 

그런 범주 비트 마스크를 설정 어디서 그런

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

을 어떻게 할 것인지 (_ 연락처 : 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 

    } 
} 

희망이 도움이