简体   繁体   English

Sprite Kit中的节点在屏幕外生成

[英]Nodes Spawning off of screen in sprite kit swift

I am building a ios game with swift and I have run into a bit of a problem. 我正在快速开发iOS游戏,但遇到了一些问题。 I am trying to spawn balls from the top of the screen and have them come down towards the ground. 我正在尝试从屏幕顶部生成球,并使它们落到地面上。 They are supposed to have random x values and go down at random rates but instead of spawning on the screen the nodes spawn on an x value which is not encompassed by the screen. 它们应该具有随机的x值并以随机的速率下降,但不是在屏幕上生成节点,而是在屏幕未包含的x值上生成节点。 Please help me as I think I have done everything right. 请帮助我,因为我认为我做对了所有事情。 Here is the code for my addball function... 这是我的addball函数的代码...

func addBall(){

        //create ball sprite
        var ball = SKSpriteNode(imageNamed: "ball.png")

        //create physics for ball
        ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size) // 1
        ball.physicsBody?.dynamic = true // 2
        ball.physicsBody?.categoryBitMask = PhysicsCategory.Ball // 3
        ball.physicsBody?.contactTestBitMask = PhysicsCategory.Person & PhysicsCategory.Ground
        ball.physicsBody?.collisionBitMask = PhysicsCategory.None // 5

        //generate random postion along x axis for ball to spawn
        let actualX = random(min:ball.frame.size.width/2+1, max: self.frame.size.width - ball.frame.size.width/2-1)
        println(actualX)
        //set balls positon
        ball.position = CGPoint(x: actualX, y: size.height - ball.size.width/2)

        //add ball to scene
        addChild(ball)

        //determine speed of ball
        let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0))

        //create movement actions
        let actionMove = SKAction.moveTo(CGPoint(x:actualX, y:  -ball.size.width/2), duration: NSTimeInterval(actualDuration))

        let actionMoveDone = SKAction.removeFromParent()


        ball.runAction(SKAction.sequence([actionMove, actionMoveDone]), withKey: "action")


    }

here is the code for my random functions 这是我的随机函数的代码

func random() -> CGFloat {
        return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
    }

    func random(#min: CGFloat, max: CGFloat) -> CGFloat {
        return random() * (max - min) + min
    }

The problem here is that your SKScene likely takes up much more space than the screen of your device. 这里的问题是您的SKScene可能比设备的屏幕占用更多的空间。 Thus, when you calculate a random value using the whole scene, some of the time the ball will spawn in the area of the scene not visible to you. 因此,当您使用整个场景计算随机值时,有时球会在您看不见的场景区域中生成。

The two main properties that control the scene's size are its size and scaleMode properties. 控制场景大小的两个主要属性是其sizescaleMode属性。 The scaleMode property relates to how the scene is mapped. scaleMode属性与场景的映射方式有关。 Unless you initialized and presented this scene yourself, you can check the scaleMode in your view controller. 除非您自己初始化并演示了此场景,否则可以在视图控制器中检查scaleMode It will likely be set to aspectFill , which according to Apple means: 根据Apple的aspectFill ,它可能会设置为aspectFill

The scaling factor of each dimension is calculated and the larger of the two is chosen. 计算每个尺寸的比例因子,然后选择两者中较大的一个。 Each axis of the scene is scaled by the same scaling factor. 场景的每个轴均以相同的缩放因子缩放。 This guarantees that the entire area of the view is filled but may cause parts of the scene to be cropped. 这样可以确保填充整个视图区域,但可能会导致部分场景被裁剪。

If you don't like this, there are other scaleModes. 如果您不喜欢这样,还有其他的scaleModes。 However, in most cases this mode would actually be preferable since SpriteKit's internal scaling is able to make universal apps. 但是,在大多数情况下,此模式实际上是可取的,因为SpriteKit的内部缩放能够制作通用应用程序。 If this is fine for you, then the easiest thing to do is set hardcoded values for something like the spawn locations for your ball node. 如果这对您来说很好,那么最简单的事情就是为诸如球节点的生成位置之类的东西设置硬编码值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM