2016-06-09 87 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

感谢您的建议。我会测试它。非常感激, –

相关问题