嗨,我使用不同的方法为游戏添加了背景
Posted
技术标签:
【中文标题】嗨,我使用不同的方法为游戏添加了背景【英文标题】:hi Ii have added a background to the game using a different method 【发布时间】:2015-03-05 04:25:04 【问题描述】:这个游戏类似于小鸟,但我想用我自己的方式制作它。背景我使用了天空的图像。但是我不明白覆盖实际上是如何运作的。每个图像都可以单独查看。但是,当我尝试将所有图像放在一起开始游戏时,它并没有显示鸟或树。任何人都可以指导我。谢谢
/* 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】:我在这里很快看到了您的代码,并找到了一些东西供您测试。
在场景中添加孩子时,先添加小鸟,然后再添加背景。
记住布局什么时候构建它就像层一样,如果你先添加你的鸟,然后添加背景,那么你的背景在鸟的前面。
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检查代码,我只是想象一下解决方案,试试看。
【讨论】:
以上是关于嗨,我使用不同的方法为游戏添加了背景的主要内容,如果未能解决你的问题,请参考以下文章
pygame障碍游戏将障碍直接画在背景中,实现用mask侦测背景中障碍和用颜色区分不同障碍的方法
pygame障碍游戏将障碍直接画在背景中,实现用mask侦测背景中障碍和用颜色区分不同障碍的方法