繁体   English   中英

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

[英]Right to Left Scrolling Background 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)




 }





}

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

暂无
暂无

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

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