嗨,我已经使用不同的方法为游戏添加了背景

时间:2015-03-05 04:25:04

标签: swift swift-playground generics

游戏类似于翻盖鸟,但我想以自己的方式制作它。背景我使用了天空的图像。但是我不明白覆盖是如何实际运作的。每张图片都可以单独看到。然而,当我尝试将所有图像一起开始游戏时,它并没有显示鸟类或树木。任何人都可以指导我。谢谢

    /* Setup your scene here */
    //Physics

    bird = SKSpriteNode(texture: BirdTexture)
    bird.setScale(0.5)
    bird.position = CGPoint (x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)
    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/2.0)


    //background is set here
    var backgroundTexture = SKTexture(imageNamed: "background.png")
    var sprite1 = SKSpriteNode(texture: backgroundTexture)
    sprite1.setScale(2.0)
    sprite1.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
    self.addChild(sprite1)
    var background = SKNode()
    background.position = CGPointMake(0, backgroundTexture.size().height)
    background.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, backgroundTexture.size().height * 2.0))
    background.physicsBody?.dynamic = true
    self.addChild(background)


    //ground is set here similar to the background.

   //tree set here
    let pipeDown = SKSpriteNode (texture: pipeDownTexture)
    pipeDown.setScale(2.0)
    pipeDown.position = CGPointMake(100.0 , CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap))
    pipeDown.physicsBody = SKPhysicsBody (rectangleOfSize: pipeDown.size)
    pipeDown.physicsBody?.dynamic = false
    pipePair.addChild(pipeDown)

    //pipe Up is set similarly




    pipePair.runAction(PipesMoveAndRemove)
    self.addChild(pipePair)

}

我已经改变了你为游戏添加背景的方式,但是在这之后我看不到鸟和树(管道)。我该如何搞定。????

1 个答案:

答案 0 :(得分:0)

我在这里很快看到了你的代码,我发现了一些你要测试的东西。

看看你在场景中添加孩子,首先添加鸟,然后添加你的背景。

记住布局什么时候它会像图层一样工作,如果你先添加你的鸟和背景之后,那么你的背景就在鸟的前面。

var BirdTexture = SKTexture(imageNamed:"Bird")
BirdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird = SKSpriteNode(texture: BirdTexture)
bird.setScale(0.5)
bird.position = CGPoint (x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/2.0)
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = false

//background is set here
var backgroundTexture = SKTexture(imageNamed: "background.png")
var sprite1 = SKSpriteNode(texture: backgroundTexture)
sprite1.setScale(2.0)
sprite1.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
self.addChild(sprite1)
var background = SKNode()
background.position = CGPointMake(0, backgroundTexture.size().height)
background.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, backgroundTexture.size().height * 2.0))
background.physicsBody?.dynamic = true



//ground is set here
var groundTexture = SKTexture(imageNamed:"ground")
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/7.0)
self.addChild(sprite)
        var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))

ground.physicsBody?.dynamic = true

//I removed your sequence and put here
self.addChild(background)
self.addChild(ground)
self.addChild(bird)

抱歉,我没有用xcode检查代码,我想象一下解决方案,试试吧。