2017-09-21 150 views
1

的方向在這裏,你有遊戲的PRINTSCREEN:正在旋轉的觸摸的方向移動SKSpriteNode到觸摸

enter image description here

我有一個靜態SKSpriteNode(播放器)。這是我對這些代碼:

class GameplayScene: SKScene, SKPhysicsContactDelegate { 

static let Pi = CGFloat(Double.pi) 
static let DegreesToRadians = Pi/180 

override func touchesMoved(_ touches: Set<UITouch>, with event: 
UIEvent?) { 
    let curTouch = touches.first! as UITouch 
    let curPoint = curTouch.location(in: self) 

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

    self.player.zRotation = angle + 90 * GameplayScene.DegreesToRadians 
} 
} 

現在,我想從在同一方向上屏幕的觸摸移動玩家每秒拍攝子彈。當子彈移出屏幕時,我想實現「SKAction.removeFromParent」。

這裏是我的fireMissile功能:

func fireMissile() { 
    let missile = SKSpriteNode(color: .yellow, size: CGSize(width: 20, 
    height: 5)) 
    missile.name = "Missile" 
    missile.position = CGPoint(x: player.position.x + 30, y: 
    player.position.y) 
    missile.zPosition = 2 
    missile.physicsBody = SKPhysicsBody(rectangleOf: missile.size) 
    missile.physicsBody?.isDynamic = false 
    missile.physicsBody?.categoryBitMask = ColliderType.Bullet 
    missile.physicsBody?.collisionBitMask = ColliderType.Enemy 
    missile.physicsBody?.contactTestBitMask = ColliderType.Enemy 

    self.addChild(missile) 

} 
+0

我不是已經給你這個答案了嗎? – Knight0fDragon

+0

困惑,還是你在這裏問兩個不同的問題?你需要幫助去除子彈,還是幫助旋轉坦克?或幫助移動坦克?這些都是獨立的問題:) – Fluidity

+0

Fluidify - 我想讓基本上以恆定速度在屏幕上觸摸的方向(從玩家)射擊子彈。對不起,沒有明確說明這一點:P – Flinigan

回答

1

對於定時器,你要使用這樣的:

run(SKAction.repeatForever(SKAction.sequence([SKAction.run { 
     yourFunction() 
     }, SKAction.wait(forDuration: 1)])), withKey: "key") 

至於導彈射擊,按順序添加SKActions。第一個應該把彈頭放在你的播放器前面,第二個將移動導彈,第三個將刪除,但是這隻會在其他線路之後由於順序而運行。如果你想在動畫中後期停下來,確保你有這個序列的關鍵。應該看起來像這樣:

missile.zRotation = player.zRotation 
missile.position = CGPoint(x:player.size.width/2*cos(player.zRotation)+player.position.x ,y:player.size.height*sin(player.zRotation)+player.position.y) 
missile.run(SKAction.sequence([SKAction.move(to: CGPoint(x:frame.size.width*cos(player.zRotation)+player.position.x ,y:frame.size.height*sin(player.zRotation)+player.position.y), duration: 0.5), SKAction.removeFromParent()]), withKey: "move") 
+0

最後的運行可能會有輕微的語法錯誤,但您可能會修復那 – Bruce

+1

你不想使用'定時器'與sprite工具包,你想通過動作 – Knight0fDragon

+0

使用內置計時方法感謝布魯斯 - 是的,我確實有一個語法問題在最後一行說'Type'([SKAction] ) - > SKAction'沒有下標成員「。不,沒有解決:/ – Flinigan