私は、敵が円内のランダムなパターンに従って動くようにしたいというゲームを作っています。私はすでに敵が画面のすべての側面にランダムに出現するようにしましたが、問題は、画像のように円内のランダムなパターンに従って敵を動かす方法を知らないということです。スプライトを円内のランダムなパターンに従う方法を教えてください。
クラスGameScene:SKScene、SKPhysicsContactDelegate didMoveToView(ビュー:SKView)FUNC {
var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var enemigoTimer = NSTimer()
}
オーバーライド{
touchesBegan(:セット、withEventイベントたUIEvent?タッチ)FUNC { circuloPrincipal.size = CGSize(width: 225, height: 225)
circuloPrincipal.position = CGPoint(x: frame.width/2, y: frame.height/2)
circuloPrincipal.color = colorAzul
circuloPrincipal.colorBlendFactor = 1.0
circuloPrincipal.name = "circuloPrincipal"
circuloPrincipal.zPosition = 1.0
self.addChild(circuloPrincipal)
オーバーライド
enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true)
}
func enemigos(){
let enemigo = SKSpriteNode(imageNamed: "enemigo")
enemigo.size = CGSize(width: 25, height: 25)
enemigo.zPosition = 2.0
enemigo.name = "enemigo"
let posisionRandom = arc4random() % 4
switch posisionRandom {
case 0:
enemigo.position.x = 0
let posisionY = arc4random_uniform(UInt32(frame.size.height))
enemigo.position.y = CGFloat(posisionY)
self.addChild(enemigo)
break
case 1:
enemigo.position.y = 0
let posisionX = arc4random_uniform(UInt32(frame.size.width))
enemigo.position.x = CGFloat(posisionX)
self.addChild(enemigo)
break
case 2:
enemigo.position.y = frame.size.height
let posisionX = arc4random_uniform(UInt32(frame.size.width))
enemigo.position.x = CGFloat(posisionX)
self.addChild(enemigo)
break
case 3:
enemigo.position.x = frame.size.width
let posisionY = arc4random_uniform(UInt32(frame.size.height))
enemigo.position.y = CGFloat(posisionY)
self.addChild(enemigo)
break
default:
break
}
enemigo.runAction(SKAction.moveTo(circuloPrincipal.position, duration: 1.4))
}
それで、敵を円の任意の点と交差する線に沿って移動させたいのですか? –
はい、円内のランダムな点ですが、私は敵が一方の側から他方の側へとその経路を継続したいと考えています。 –
ちょうどそれを正しくしてください。 Unityのような通常のゲームエンジンを手に入れて、PointWithinCircle関数を使用してください:) – Fattie