水平边界在SpriteKit中不起作用

时间:2015-11-11 21:16:50

标签: ios swift sprite-kit

import SpriteKit

class GameScene: SKScene {
override func didMoveToView(view: SKView) {
    /* Setup your scene here */
/*    let myLabel = SKLabelNode(fontNamed:"Chalkduster")
    myLabel.text = "Hello, World!";
    myLabel.fontSize = 45;
    myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

    self.addChild(myLabel)*/   let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
    self.addChild(sprite)
    let bb = SKPhysicsBody(edgeLoopFromRect: self.frame)
    bb.friction = 0
    self.physicsBody = bb

   }

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

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

        let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
        self.addChild(sprite)

        sprite.xScale = 0.15
        sprite.yScale = 0.15

        sprite.position = location
        sprite.color = UIColor.redColor()
        //sprite.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
        sprite.physicsBody = SKPhysicsBody(circleOfRadius: 2.5)
        //sprite.physicsBody?.usesPreciseCollisionDetection = true
        sprite.physicsBody?.affectedByGravity = true
        sprite.physicsBody?.allowsRotation = false
        sprite.physicsBody?.restitution = 0.5
        sprite.physicsBody?.linearDamping = 0
        sprite.physicsBody?.friction = 0
        //self.physicsWorld.

        self.physicsWorld.gravity = CGVectorMake(0,-2.5)

        //self.physicsWorld.gravity = CGVectorMake(0.25, -0.25)
        //var action2 = SKAction.rotateByAngle(180.0, duration:5)
        //sprite.runAction( action2)
        //action = SKAction.applyTorque(2, duration: 3)
        //sprite.runAction(SKAction.repeatActionForever(action))

        //let boundaries = UICollisionBehavior(items: [sprite])
    }
}
}

水平边界似乎不起作用。如果我从同一点落下球,由于摩擦力设置为零,它们会一直移出屏幕。请帮我理解问题所在。

1 个答案:

答案 0 :(得分:2)

可能是因为您的场景大小与视图的大小不同。默认情况下,场景是从.sks文件加载的,大小为1024x768。

要解决此问题,您可以在GameViewController中进行以下更改:

./gradlew app:test

希望这有帮助!