从右到左滚动背景Swift / Sprite Kit

时间:2016-01-18 23:23:27

标签: ios iphone xcode swift sprite-kit

我有滚动背景,但与原始图像相比,背景图像似乎“放大”了。背景滚动得很好,但我不确定为什么图像会“放大”。任何帮助将不胜感激。

class GameScene: SKScene, SKPhysicsContactDelegate {


var blueBall:SKSpriteNode!
var backgroundImage:SKSpriteNode!
var backgroundImage2:SKSpriteNode!


override func didMoveToView(view: SKView) {



    self.view!.backgroundColor = UIColor(patternImage: UIImage(imageLiteral: "backgroundImage.png"))


    self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
    self.physicsWorld.contactDelegate = self

    blueBall = SKSpriteNode( imageNamed: "ball1111.png")
    blueBall.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    blueBall.physicsBody = SKPhysicsBody(circleOfRadius: blueBall.size.width / 0.85)

    blueBall.physicsBody!.dynamic = true
    blueBall.physicsBody!.allowsRotation = true
   self.addChild(blueBall)
    blueBall.zPosition = 2



    backgroundImage = SKSpriteNode(imageNamed: "backgroundImage.png")
    self.addChild(backgroundImage)
    backgroundImage.zPosition = 0
    backgroundImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    backgroundImage.size.height = self.size.height
    backgroundImage.size.width = self.size.width
    backgroundImage.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))


    let backgroundTexture = SKTexture(imageNamed: "backgroundImage.png")


    let shiftBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: 9)
    let replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y:0, duration: 0)
    let movingAndReplacingBackground = SKAction.repeatActionForever(SKAction.sequence([shiftBackground,replaceBackground]))

    for var i:CGFloat = 0; i<3; i++ {

        let background = SKSpriteNode(texture: backgroundTexture)
        background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: CGRectGetMidY(self.frame))
        background.size.height = self.frame.height
        background.runAction(movingAndReplacingBackground)


        self.addChild(background)




 }





}

1 个答案:

答案 0 :(得分:1)

您的图片未正确加载存在问题,因此您的号码已关闭。基本上通过使用png扩展名加载SKSpriteNode(imageNamed:&#34; backgroundImage.png&#34;),您将加载实际文件,不符合任何视网膜规则。然后不会调整宽度和高度来处理这种情况。如果您在xcassets文件夹中使用地图集,它将允许您为所有显示尺寸指定图形,并将选择设备支持的正确图形。