gpt4 book ai didi

ios - 如何使用 SpriteKit 替换背景图像?

转载 作者:行者123 更新时间:2023-11-30 13:51:54 25 4
gpt4 key购买 nike

如果这是一个愚蠢的问题,我提前道歉,但我正在学习,而且我似乎找不到任何帮助。我的大部分经验都来自 Storyboard,因此 SpriteKit 是我第一次真正尝试以编程方式完成所有事情。

我有一款与《Flappy Bird》非常相似的横向卷轴 2D 游戏。每当玩家达到分数的倍数(例如 10)时,我就需要更改背景。背景是一张慢慢向屏幕左侧移动并自行替换的图像。

问题是当它达到 10 时,我无法用另一个图像替换该图像。我已经使用 zPosition 成功在旧图像之上添加了一张新图像,但原始图像仍在下方,这看起来很笨拙且不稳定。

我尝试从父级中删除原始背景图像,但我似乎不知道该怎么做。

这是我的代码。提前致谢。

var currentBg = SKTexture(imageNamed: "img/bgYellow.png")

func makeBackground() {

let movebg = SKAction.moveByX(-currentBg.size().width, y: 0, duration: 9)
let replacebg = SKAction.moveByX(currentBg.size().width, y: 0, duration: 0)
let movebgForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))

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

bg = SKSpriteNode(texture: currentBg)
bg.position = CGPoint(x: currentBg.size().width/2 + currentBg.size().width * i, y: CGRectGetMidY(self.frame))
bg.size.height = self.frame.height
bg.runAction(movebgForever)
movingObjects.addChild(bg)

}

}

fun didMoveToView() {

//Set up game, etc...

makeBackground()

}

if score == 10 {

//I've tried adding the following line to remove the first image, but no luck
//movingObjects.removeChildrenInArray([bg])

currentBg = SKTexture(imageNamed: "img/bgRed.png")
makeBackground()

} else if score == 20 {

currentBg = SKTexture(imageNamed: "img/bgBlue.png")
makeBackground()

} //Else etc...

最佳答案

您正在更改纹理而不是您的背景节点纹理。

var background: SKSpriteNode!

然后您需要创建背景

    background = SKSpriteNode(texture: SKTexture(imageNamed: "img/bgYellow.png"), color: UIColor.clearColor(), size: CGSizeMake(world.frame.size.width, world.frame.size.height))
background.position = CGPointMake(world.frame.size.width / 2, world.frame.size.height / 2)
// etc

要更改背景图片:

if score == 10 {

background.texture = SKTexture(imageNamed: "img/bgRed.png")
makeBackground()
}

关于ios - 如何使用 SpriteKit 替换背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com