2016-06-09 67 views
0

我剛剛開始使用sprite套件,並且遇到了路障。我正試圖重新創造舊的atari遊戲小行星。我目前正試圖找出如何從屏幕的一側移動節點「出貨」並從相反側出來。例如,pacman將從屏幕的右側移出屏幕的左側。非常感謝幫助。 由於提前, 賈裏德我如何讓一個節點從屏幕的一側移動到另一側?

 import SpriteKit 

     class GameScene: SKScene, SKPhysicsContactDelegate{ 

     let base = SKSpriteNode(imageNamed: "Base") 
     let ball = SKSpriteNode(imageNamed: "Ball") 
     let ship = SKSpriteNode(imageNamed: "Ship") 
     let shoot = SKSpriteNode(imageNamed: "shootButton") 

     override func didMoveToView(view: SKView){ 

    //  var DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100)) 
    //  DynamicView.backgroundColor=UIColor.greenColor() 
    //  DynamicView.layer.cornerRadius=2 
    //  DynamicView.layer.borderWidth=2 
    //  self.view!.addSubview(DynamicView) 

     self.anchorPoint = CGPointMake(0.5, 0.5) 

     self.physicsWorld.contactDelegate = self 
     self.physicsWorld.gravity = CGVectorMake(0.0, 0.0) 

     self.addChild(base) 
     base.position = CGPointMake(-350, -200) 

     self.addChild(shoot) 
     shoot.position = CGPointMake(350, -200) 

     self.addChild(ball) 
     ball.position = base.position 

     self.addChild(ship) 
     ship.position = CGPointMake(20, 47) 
     ship.xScale = 0.7 
     ship.yScale = 0.7 

     ship.physicsBody?.dynamic = true 
     ship.physicsBody?.allowsRotation = true 
     ship.physicsBody?.affectedByGravity = true 
     ship.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship"), size: ship.size) 


     self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 



     ball.alpha = 0.4 
     base.alpha = 0.4 


    } 





// func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
//  /* Called when a touch begins */ 
//   
//  for touch in (touches as! Set<UITouch>) { 
//   let location = touch.locationInNode(self) 
//    
//    
//     } 
// } 



    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch in (touches) { 
      let location = touch.locationInNode(self) 

      let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y) 
      let angle = atan2(v.dy, v.dx) 

      let deg = angle * CGFloat(180/M_PI) 


//   print(deg + 180) 


      let length:CGFloat = base.frame.size.height/2 

      let xDist:CGFloat = sin(angle - 1.57079633) * length 
      let yDist:CGFloat = cos(angle - 1.57079633) * length 

      ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist) 

      if CGRectContainsPoint(base.frame, location) { 
       ball.position = location 
       } 
       else{ 
       ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist) 
       } 
     ship.zRotation = angle - 1.57079633 

      ship.physicsBody?.mass = 2 




      var shipRotation : CGFloat = ship.zRotation 
      var calcRotation : Float = Float(angle - 1.57079633) + Float(M_PI_2); 

      let intensity : CGFloat = 2000.0 // put your value 
      let xVelocity = intensity * CGFloat(cosf(calcRotation)) 
      let yVelocity = intensity * CGFloat(sinf(calcRotation)) 
      let vector : CGVector = CGVectorMake(xVelocity, yVelocity) 

      //Apply force to spaceship 
      ship.physicsBody?.applyForce(vector) 


     } 

    } 

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 


     let move:SKAction = SKAction.moveTo(base.position, duration: 0.2) 
     move.timingMode = .EaseOut 
     ball.runAction(move) 


    } 
} 

// overridefunc update(currentTime: CFTimeInterval) { 
//  /* Called before each frame is rendered */ 
//  
//} 
+1

你到底在問什麼?請添加更多詳細信息。 – Laurel

回答

0

看起來你正在使用的物理移動的船(而不是在update()方法,通過一定的量,來更新其位置)

所以盡你所能做法是重寫didiSimulatePhysics()方法(在SpriteKit遊戲引擎完成所有物理計算並移動所有節點後調用該方法),並且如果您的飛船在屏幕外(您可以通過查看它的位置是否在screne的x之外或者通過使用intersectsRect方法來查看是否船舶的框架不再重疊t他屏幕框架),如果是,只需將其包裹到屏幕的另一側。

0

func update(currentTime)檢查ship.position.x < 0ship.position.x > scene.width。如果true,則將ship.position.x設置爲對面。

+0

感謝您的建議。我會測試它。非常感激, –

相關問題