如何让节点从屏幕的一侧移动到另一侧?

时间:2016-06-09 22:01:38

标签: swift sprite-kit screen nodes

我刚刚开始使用精灵套件并且遇到了障碍。我正在尝试重建旧的atari游戏小行星。我目前正试图找出如何从屏幕的一侧移动节点“ship”并从另一侧出来。这方面的一个例子是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 */
//    
//}

2 个答案:

答案 0 :(得分:0)

看起来你正在使用物理来移动船只(而不是在update()方法中将其位置更新一定数量)

所以你可以做的是覆盖didiSimulatePhysics()方法(在SpriteKit游戏引擎完成所有物理计算并移动所有节点后调用)并且如果你的船在屏幕外(你可以通过查看它的位置是否在screne的x&amp; y边界之外,或者通过使用intersectsRect方法来查看是否。船的框架不再与屏幕框架重叠),如果是,则只需将其包裹到另一侧。屏幕。

答案 1 :(得分:0)

func update(currentTime)中检查ship.position.x < 0ship.position.x > scene.width。如果true,请将ship.position.x设置为对方。