2016-06-28 62 views
1

我正在製作一個遊戲,我希望敵人可以在一個圓圈內隨機移動。我已經讓敵人在屏幕的所有邊上隨機產生,但問題是我不知道如何讓敵人像圖像一樣在一個圓圈內隨機移動。如何讓精靈在圓圈內遵循隨機模式?

類GameScene:SKScene,SKPhysicsContactDelegate {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo") 
var enemigoTimer = NSTimer() 
} 

倍率FUNC didMoveToView(視圖:SKView){

 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) 

倍率FUNC的touchesBegan(觸摸:設置,withEvent事件:?的UIEvent){

 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)) 



} 

enter image description here

+0

所以你想讓敵人沿着一條與給定圓圈中的隨機點相交的線移動? –

+0

是的,是圓圈內的一個隨機點,但我希望敵人從一邊延續到另一邊。 –

+0

只要做得好。獲得像Unity這樣的普通遊戲引擎並使用PointWithinCircle功能:) – Fattie

回答

1

嘗試添加以下代碼:

let randomY = CGFloat(Int.random(-Int(circuloPrincipal.frame.height/2)...Int(circuloPrincipal.frame.height/2))) 
    let randomX = CGFloat(Int.random(-Int(circuloPrincipal.frame.width/2)...Int(circuloPrincipal.frame.width/2))) 

    let slopeToCirculoPrincipal = (enemigo.position.y - circuloPrincipal.position.y + randomY)/(enemigo.position.x - circuloPrincipal.position.x + randomX) 
    let constant = enemigo.position.y - slopeToCirculoPrincipal * enemigo.position.x 

    let finalX : CGFloat = enemigo.position.x < circuloPrincipal.position.x ? 1500.0 : -1500.0 // Set it to somewhere outside screen size 

    let finalY = constant + slopeToCirculoPrincipal * finalX 

    let distance = (enemigo.position.y - finalY) * (enemigo.position.y - finalY) + (enemigo.position.x - finalX) * (enemigo.position.x - finalX) 
    let enemigoSpeed : CGFloat = 100.0 
    let timeToCoverDistance = sqrt(distance)/enemigoSpeed 

    let moveAction = SKAction.moveTo(CGPointMake(finalX, finalY), duration: NSTimeInterval(timeToCoverDistance)) 
    let removeAction = SKAction.runBlock {() -> Void in 
     enemigo.removeFromParent() 

    } 
    enemigo.runAction(SKAction.sequence([moveAction,removeAction])) 

相反的:

 enemigo.runAction(SKAction.moveTo(circuloPrincipal.position, duration: 1.4)) 

你也需要在一些地方把這個擴展在您的項目:

extension Int 
    { 
static func random(range: Range<Int>) -> Int 
{ 
    var offset = 0 

    if range.startIndex < 0 // allow negative ranges 
    { 
     offset = abs(range.startIndex) 
    } 

    let mini = UInt32(range.startIndex + offset) 
    let maxi = UInt32(range.endIndex + offset) 

    return Int(mini + arc4random_uniform(maxi - mini)) - offset 
} 
    } 
+0

非常感謝你,伊凡,你搖滾! –