2016-03-03 52 views
-1

我正在爲我的大學工作,我陷入了一件事。快速拍攝移動位置

當我試圖用我的英雄射擊子彈只能去的向上或側面,我需要我的子彈去的地方永遠是我的英雄輪播PLZ幫助我,這是我的代碼

func spownBullet() { 
    let bullet = SKSpriteNode(imageNamed: "Bullet") 
    let hero = self.childNodeWithName("hero") as! SKSpriteNode 
    bullet.zPosition = 1 
    bullet.position = CGPointMake(hero.position.x, hero.position.y) 
    bullet.size = CGSize(width: 20, height: 30) 
    let bulletact = SKAction.moveToY(self.size.height + 300, duration: 1) 
    bullet.runAction(SKAction.repeatActionForever(bulletact)) 
    self.addChild(bullet) 

} 

回答

0

如果我理解正確的話,你希望你的子彈以與你的「英雄」精靈旋轉相同的角度走一定距離(例如300)。

這裏有一個函數來計算目標點:

// destination point moving at angle 
// --------------------------------- 
func endPointMovingSpriteFrom(origin:CGPoint, atAngle angle:CGFloat,forDistance distance:CGFloat) -> CGPoint 
{ 
    let deltaX = distance/sin(angle + CGFloat(M_PI)/2) 
    let deltaY = distance/cos(angle + CGFloat(M_PI)/2) 
    return CGPoint(x: origin.x - deltaX, y:origin.y - deltaY) 
} 

你可以用它來計算目的地,然後使用一個的moveTo行動目標:

let bulletDestination = endPointMovingSpriteFrom(hero.position, atAngle:hero.zRotation, forDistance:300) 
let bulletact = SKAction.moveTo(bulletDestination, duration: 1) 
+0

的功能可以是全球性的。使用它的兩行應該簡單地替換您在示例代碼中創建的那一行(創建Bulletact的地方)。 –

+0

ty但我有我應該在哪裏但第二個代碼(讓bulletDestination = endPointMovingSpriteFrom(hero.position,atAngle:hero.rotation,forDistance:300) let bulletact = SKAction.moveTo(bulletDestination,duration:1))在touchesMoved中。 。? –

+0

當我更換它時,s給我一個錯誤 –