2015-12-11 22 views
0

我想讓我的固定怪物在我的移動玩家身上射擊。玩家位置和怪物位置都獲得正確的值,並且該代碼被寫入每秒鐘被調用的函數中。現在彈丸出現,但不會離開怪物。除了.applyAngularImpulse之外,還有什麼我應該使用的嗎?如何在Swift中拍攝移動的精靈?

let deltaX = player.position.x - monster.position.x 
    let deltaY = player.position.y - monster.position.y 
    let angle = atan2(deltaY, deltaX) 

    monProjectile.physicsBody?.applyAngularImpulse(angle) 
+0

你檢查本教程? http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners –

+0

是的,我有。不幸的是,我對這些想法的應用似乎並不奏效,因爲我試圖在稍微不同的情況下使用它們。感謝您的幫助! – nick

+0

沒有物理的東西:https://stackoverflow.com/questions/36230619/how-to-move-enemy-towards-a-moving-player/36235426#36235426 ...不過,你可以添加物理實體到節點(只是設置'node.physicsBody.affectedByGravity = false',並設置'node.physicsBody.collisionBitMask' = 0),以便進行接觸檢測。 – Whirlwind

回答

1

UPDATE

說完看着你提供的代碼,我懷疑有兩兩件事是有過錯:

1)您指定一個「彈丸」的形象,我不能看在你的項目中。

2)你試圖施加角衝量(即自旋)而不是常規衝量(即方向加速度)。

要解決第一個問題,請爲您的射彈添加圖像。要解決第二個問題,請考慮使用applyImpulse()CGVector

原來的答覆

關閉我的頭頂,有可能會導致這樣幾件事情:

1)如何的衝動多少是你申請?打印出該值並查看您正在使用的編號。

2)你的彈丸在創建時是否與怪物重疊?如果是這樣,它可能會碰撞並卡住。

3)彈丸是否有可能完全與其他節點發生碰撞,例如:背景圖片?

對於您的SKView,您應該考慮將showsPhysics設置爲true,以便您可以更清楚地看到發生的情況。

+0

1.它表示它將-3值作爲一個力。我認爲這是正確的,因爲我的球員使用3.0進行投籃,而怪物位於球員右側,這就是爲什麼我相信我會獲得負面價值。 2.我設置了彈丸的位置,以便它不會與精靈重疊(我已經在玩家拍攝時遇到過這個問題)。 3.我認爲這不會是因爲玩家射擊工作正常,並且使用相同的方法,只是施加不同的力量。感謝幫助! – nick

+0

我剛剛在/ r/swift上看過這篇文章,還有另一個選項發生在我身上:您的項目是否附有一個物理主體?嘗試將問號更改爲感嘆號片刻:如果您的代碼崩潰,則說明您已經滑落。 – TwoStraws

+0

我已經試過了,它沒有區別。該應用程序不會崩潰。 – nick

0
func makeShoot() { 
    let Shoot:ShootClass = ShootClass.init() 
    Shoot.physicsBody = SKPhysicsBody(texture: Shoot.texture!, 
            size: Shoot.texture!.size()) 
    Shoot.position = (self.Shoot?.position)! 
    Shoot.currentPosition = (self.Shoot?.position)! 
    Shoot.physicsBody?.isDynamic = true 
    Shoot.physicsBody?.allowsRotation = false 
    Shoot.physicsBody?.affectedByGravity = false 
    Shoot.physicsBody?.friction = 0 
    Shoot.physicsBody?.restitution = 1 
    Shoot.physicsBody?.mass = 1 
    Shoot.physicsBody?.linearDamping = 0.0 
    Shoot.physicsBody?.angularDamping = 0.0 
    Shoot.physicsBody?.categoryBitMask = ShootCategory 
    Shoot.physicsBody?.collisionBitMask = BorderCategory 
    Shoot.physicsBody?.contactTestBitMask = BorderCategory 

    PlayingView.addChild(Shoot); 

    Shoot.physicsBody?.applyImpulse(CGVector(dx: 100, dy: 100)) 

    self.moveNodeToLocation(Shoot: Shoot) 
} 



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let curTouch = touches.first! 
    let curPoint = curTouch.location(in: self) 
    if ((curPoint.x > 103.5 && curPoint.y > 50.0) || (curPoint.x < 840.0 && curPoint.y > 50.0)) { 
     StartingPoint = touches.first?.location(in: self) 
     direction?.isHidden = false 
     direction?.setScale(0.50) 
     FirstTouchLocater = SKSpriteNode(imageNamed: "ic_Shootz"); 
     FirstTouchLocater.position = curPoint 
     FirstTouchLocater.alpha = 0.5 
     self.addChild(FirstTouchLocater); 

    } 
    else{ 
     self.direction?.isHidden = true 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    let point:CGPoint = (touches.first?.location(in: self))! 

    if point.y < 40 { 
     return 
    } 

    if !(isTouch!) { 
     isTouch = true 
    } 


    let dy:CGFloat = StartingPoint!.y - point.y 

    let size:CGFloat = dy*10/self.frame.height 

    if size < 2 && size > 0.50 { 
     direction?.setScale(size) 
    } 
    print("size ======> \(size)") 




    let curTouch = touches.first! 
    let curPoint = curTouch.location(in: self) 

    if (curPoint.x <= ((StartingPoint?.x)! + 20.0)) && ((curPoint.x + 20.0) >= (StartingPoint?.x)!) && (curPoint.y <= ((StartingPoint?.y)! + 20.0)) && ((curPoint.y + 20.0) >= (StartingPoint?.y)!){ 
     self.direction?.isHidden = true 
     FirstTouchLocater?.isHidden = true 
    } 
    else if ((curPoint.x > 103.5 && curPoint.y > 50.0) || (curPoint.x < 840.0 && curPoint.y > 50.0)) { 
     let deltaX = (self.direction?.position.x)! - curPoint.x 
     let deltaY = (self.direction?.position.y)! - curPoint.y 
     let angle = atan2(deltaY, deltaX) 

     let DegreesToRadians = CGFloat.pi/180 

     self.direction?.zRotation = angle + 90 * DegreesToRadians 
     self.direction?.isHidden = false 
     FirstTouchLocater?.isHidden = false 
    } 
    else{ 
     self.direction?.isHidden = true 
     FirstTouchLocater?.isHidden = true 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if direction?.isHidden == false { 
     FirstTouchLocater.removeFromParent() 
     direction?.isHidden = true 
     direction?.setScale(0.1) 
     if timeThrow == nil && isTouch! 
     { 
      isTouch = false 
      counterY = 0; 

      let touch = touches.first 
      let touchLocation = touch?.location(in: self) 
      lastTouch = touchLocation 
      lastTouch1 = touchLocation 

      ShootThrow = 2 

      timeThrow = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.loadScreen), userInfo: nil, repeats: true) 
     } 
    } 
} 

func moveNodeToLocation(Shoot:SKSpriteNode) { 
    let dx = (lastTouch?.x)! - Shoot.position.x 
    let dy = (lastTouch?.y)! - Shoot.position.y 

    let speed1:CGFloat = 424 
    let hypo = hypot(dx, dy) 

    let newX = (speed1 * dx)/hypo 
    let newY = (speed1 * dy)/hypo 
    Shoot.physicsBody?.velocity = CGVector(dx:newX, dy: newY) 

} 

}

+2

請考慮解釋此答案如何解決原始問題中的問題。 –