2017-08-14 21 views
0

enter image description hereタッチの場所にSKSpriteNodeを移動する

上記は私のゲームのイメージです。トップダウンゲーム。プレイヤーが画面に触れるたびに、弾丸をその場所に移動させたい。私はまた、プレイヤーが画面上で彼の指の周りをドラッグできるようにしたい、と同じことが起こる。プレーヤーが撮影するたびに画面に触れる必要はありません。

私はこれまでにいくつかの異なるものを試しましたが、何も動作していないようです。

最初に、私が弾丸のための別の機能を持っているかどうかはわかりません。しかし、とにかく、これは私の弾丸の機能です。

func spawnBullets() { 
    let bullet = SKSpriteNode(imageNamed: "Bullet") 
    bullet.name = "Bullet" 
    bullet.zPosition = 4 
    bullet.position = CGPoint(x: player.position.x + 19, y: 
    player.position.y) 
    self.addChild(bullet) 
} 

私もdidMove機能の弾丸のための "タイマー" している:

override func touchesBegan(_ touches: Set<UITouch>, with event: 
UIEvent?) {   
    for touch in touches {    
     let location = touch.location(in: self)    
     let moveToPoint = SKAction.move(to: location, duration: 0.5) 
     let repeatAction = SKAction.repeatForever(moveToPoint)    
     bullet.run(moveToPoint) 
    } 

} 
+0

このコードを実行するとどうなりますか?私はこのコードを自分で実行していないか、あまりにも密接に分析していますが、コンパイルしますか? 'touchesBegan'の' bullet.run'は、私は 'bulletTarget'プロパティを作成し、それを 'moveTo'位置として使用し、SKActiを作成して適用しますあなたの 'spawnBullet'メソッドの中でbullerを動かしてください。 –

+0

私はエラーはありませんが、箇条書きは動かない。私はアプリの開発に一新しています。あなたはbulletTargetプロパティで何を意味していますか? – Flinigan

+0

'' touchesBegan'で 'bullet.run(moveToPoint)'で使われている 'bullet'変数をどこで定義しますか?あなたはどこかでそれを定義しなければなりません( 'var bullet = ...'で)。メソッドの外側で定義されている場合(つまり、クラス内のグローバルスコープの場合)、そのプロパティは「プロパティ」と呼ばれます。しかし、この「弾丸」は、あなたが「スポーンブレット」であなたのシーンに追加する「弾丸」ではないでしょう。後でコードに基づいて回答を投稿しようとします。 –

答えて

1

ここに行く - A:

var timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, 
selector: Selector("spawnBullets"), userInfo: nil, repeats: true) 

そして最後に、これは私のtouchesBegan機能ですあなたは画面の周りをドラッグすることができますシンプルなアプリとタッチの場所に向かって撃つミサイル。

船に触れると、船をドラッグできます。船の外に触れると、ミサイルは船からタッチ位置に撃ちます。

import SpriteKit 

class GameScene: SKScene { 

var ship = SKSpriteNode() 
var shipIsTouched = false 
var missileDestination = CGPoint() 
let missileSpeed: CGFloat = 800 // Points per second) 
let missileFireRate : TimeInterval = 0.2 // Seconds between each missile 

override func didMove(to view: SKView) { 
    missileDestination = CGPoint(x: 0, y: (self.size.height/2)) 
    createPlayerShip() 
    let fire = SKAction.sequence([SKAction.run(fireMissile), SKAction.wait(forDuration: missileFireRate)]) 
    run(SKAction.repeatForever(fire)) 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     if ship.contains(touch.location(in: self)) { 
      shipIsTouched = true 
     } else { 
      missileDestination = touch.location(in: self) 
      ship.zRotation = direction(to: missileDestination, from: ship.position) - CGFloat(Double.pi/2) 
     } 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if (shipIsTouched == true) { 
     ship.position = (touches.first?.location(in: self))! 
     ship.zRotation = direction(to: missileDestination, from: ship.position) - CGFloat(Double.pi/2) 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if shipIsTouched { 
     shipIsTouched = false 
    } 
} 

override func update(_ currentTime: TimeInterval) { 
    // Called before each frame is rendered 
} 

func createPlayerShip() { 
    ship = SKSpriteNode(imageNamed: "Spaceship") 
    ship.zRotation = CGFloat(-Double.pi/2.0) 
    ship.scale(to: CGSize(width: 150, height: 150)) 
    ship.position = CGPoint(x: -size.width/2 + 200, y: 0) 

    addChild(ship) 
} 


func fireMissile() { 
    let missile = SKSpriteNode(color: .white, size: CGSize(width: 50, height: 10)) 
    missile.position = ship.position 

    let missileFlightTime = travelTime(to: missileDestination, from: ship.position, atSpeed: missileSpeed) 
    missile.zRotation = direction(to: missileDestination, from: missile.position) 

    addChild(missile) 

    let missileMove = SKAction.move(to: missileDestination, 
            duration: TimeInterval(missileFlightTime)) 
    let missileRemove = SKAction.removeFromParent() 
    missile.run(SKAction.sequence([missileMove, missileRemove])) 
} 

func travelTime(to target : CGPoint, from : CGPoint, atSpeed speed : CGFloat) -> TimeInterval { 
    let distance = sqrt(pow(abs(target.x - from.x),2) + 
     pow(abs(target.y - from.y),2)) 
    return TimeInterval(distance/speed) 
} 


func direction(to target : CGPoint, from: CGPoint) -> CGFloat { 
    let x = target.x - from.x 
    let y = target.y - from.y 
    var angle = atan(y/x) 
    if x < 0 { 
     angle = angle + CGFloat.pi 
    } 
    return angle 
} 
} 

ありのmoveToは時間がかかるため、宛先はミサイルがゆっくりと移動する近かったので、もし一貫性のあるミサイルの速度を(作るために余分な策略のビット速度は、ありませんし、さらに離れた場合、彼らは動くだろうより速く)、ミサイルを目的地に向かって回転させる。

ミサイルが目的地までたどり着く湾曲した道を作ることができます。これは涼しくてもアプリには適さないかもしれません。

EDIT:

import SpriteKit 

class GameScene: SKScene { 

var ship = SKSpriteNode() 
var missileDestination = CGPoint() 
let missileSpeed: CGFloat = 800 // Points per second) 
let missileFireRate : TimeInterval = 0.2 // Seconds between each missile 

override func didMove(to view: SKView) { 
    missileDestination = CGPoint(x: size.height/2, y: 0) 
    createPlayerShip() 
    let fire = SKAction.sequence([SKAction.run(fireMissile), SKAction.wait(forDuration: missileFireRate)]) 
    run(SKAction.repeatForever(fire)) 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
      missileDestination = touch.location(in: self) 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     missileDestination = (touches.first?.location(in: self))! 
} 
:あなたはあなたの指を追跡するために船を静止し、ミサイルをしたい場合は

が、これにcreatePlayerShip(はい、私たちが失ってしまったtouchesEnded()update()まで、すべてのコードを置き換え

+0

うわー、私はこのゲームのすべてを必要としませんが、そのすべては確かに将来私にとって貴重です!ありがとう!私はプレーヤを静的にしたいので、あなたのコードからいくつか変更してそれをそうするようにしました。しかし、1つの質問ですが、どのようにプレイヤーが弾丸の方向を変えたいときはいつでも画面に触れる必要がないようにtouchesMovedをどのように使用しますか?代わりに、単に画面上で指を「ドラッグ」し、弾丸をその方向に動かしますか? – Flinigan

+0

@Flinigan私は、船をドラッグ可能にするコードを削除したので、 'ship.position'の代わりにtouchesMovedに 'missileDestination'を設定することができると思います。 –

+0

@Fineganあなたの指に「ついた」宇宙船とミサイルの私の編集を見てください...意味がないものは、質問してください。 –

関連する問題