2016-12-29 6 views
0

SKSceneでMKMapViewを使用していますが、表示されません。ここに私のコードです。SpriteKitのMKMapView - Swift

import SpriteKit 
    import MapKit 
    import CoreLocation 

    class Map : SKScene, MKMapViewDelegate { 

var startY: CGFloat = 0.0 
    var lastY: CGFloat = 0.0 
    var moveableArea = SKNode() 
     override func didMove(to view: SKView) { 

// set position & add scrolling/moveable node to screen 
     moveableArea.position = CGPoint(x: 0,y: 0) 
     self.addChild(moveableArea) 
     //moveableArea.addChild(Node Here) 
      var map : MKMapView! = MKMapView() 
      map.delegate = self 

      let location = CLLocationCoordinate2DMake(52.5031135, -6.572772100000066) 
      map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(52.5031135, -6.572772100000066), MKCoordinateSpanMake(0.05, 0.05)), animated: true) 

      var annotation = MKPointAnnotation() 
      annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude) 
      annotation.title = "This is my Town!" 
      annotation.subtitle = "Enniscorthy, Co. Wexford" 
      map.addAnnotation(annotation) 
      map.showAnnotations([annotation], animated: true)    
      map.center = CGPoint(x: self.size.width/2, y: self.size.width/2) 
      view.addSubview(map) 
     } 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 


     for touch in touches { 
      let location = touch.location(in: self) 



      startY = location.y 
      lastY = location.y 


      let tappednode = atPoint(location) 
      let tappedNodeName: String? = tappednode.name 


      if tappedNodeName == "exit" { 
       if let view = self.view { 

        let scene = MenuGame(size: self.size) 
        scene.scaleMode = .aspectFill 
        view.presentScene(scene) 
       } 
      } 
     } 



    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 




     for touch in touches { 
     let location = (touch as AnyObject).location(in: self) 
     // set the new location of touch 
     var currentY = location.y 

     // Set Top and Bottom scroll distances, measured in screenlengths 
     var topLimit:CGFloat = 0.0 
     var bottomLimit:CGFloat = 0.6 

     // Set scrolling speed - Higher number is faster speed 
     var scrollSpeed:CGFloat = 1.0 

     // calculate distance moved since last touch registered and add it to current position 
     var newY = moveableArea.position.y + ((currentY - lastY)*scrollSpeed) 

     // perform checks to see if new position will be over the limits, otherwise set as new position 
     if newY < self.size.height*(-topLimit) { 
      moveableArea.position = CGPoint(x: moveableArea.position.x, y: self.size.height*(-topLimit)) 
     } 
     else if newY > self.size.height*bottomLimit { 
      moveableArea.position = CGPoint(x: moveableArea.position.x, y: self.size.height*bottomLimit) 
     } 
     else { 
      moveableArea.position = CGPoint(x: moveableArea.position.x, y: newY) 
     } 

     // Set new last location for next time 
     lastY = currentY 
    } 

    } 

    } 

SpriteKitでこれを行う方法についてのチュートリアルはありません。だからこそ推測です。私は何が欠けていますか?

Update1:​​MoveableAreaにMKMapViewを追加するにはどうすればよいですか?

答えて

0
 let map : MKMapView! = MKMapView() 
     map.delegate = self 
    // below line is missing 
    map.frame=CGRect(x: 0,y: 0,width: self.size.width,height: self.size.width); 
     let location = CLLocationCoordinate2DMake(52.5031135, -6.572772100000066) 
     map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(52.5031135, -6.572772100000066), MKCoordinateSpanMake(0.05, 0.05)), animated: true) 

     let annotation = MKPointAnnotation() 
     annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude) 
     annotation.title = "This is my Town!" 
     annotation.subtitle = "Enniscorthy, Co. Wexford" 
     map.addAnnotation(annotation) 
     map.showAnnotations([annotation], animated: true) 
     map.center = CGPoint(x: self.size.width/2, y: self.size.width/2) 
     view.addSubview(map) 

you missed frame of your map view 
+0

完璧!私は1つの問題を抱えています。私はこのSKSceneでscrollviewとしてMovedを使用します。 (アップデートのフルコード)SkSpriteNode()とSKLabelNodes()はすべてmoveArea.addChild(---)で追加されています。これを追加するにはどうすればよいですか? –

関連する問題