gpt4 book ai didi

ios - 如何将两个 SKTextures 合二为一

转载 作者:行者123 更新时间:2023-11-28 05:31:11 27 4
gpt4 key购买 nike

我正在尝试为游戏创建背景并有两个背景图像。我可以使用两个不同的 SKSpriteNode()、SKTexture、SKAction.sequence..etc 和一些数学将它们拼接在一起,但结果有点不稳定,而且我似乎无法正确地计算数学。它们应该能够组合成一个长期变化的背景,但它总是抵消。

谁能告诉我如何将两种纹理合二为一,这样我就不必为数学问题费脑筋了。或者,如果有人发现我的数学有错误,您能指出来吗?两个背景具有相同的 1200 像素宽度

override func didMoveToView(view: SKView) {
/* Setup your scene here */

var bgTexture = SKTexture(imageNamed: "bg_2_midground_1.png")
var bgTexture2 = SKTexture(imageNamed: "bg_2_midground_2.png")

var totalBG = bgTexture.size().width + bgTexture2.size().width

var moveBG1 = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
//shift bg back to be able to repeat itself
var replaceBG1 = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0.0) //move immediately back to replace self
var moveBG1Forever = SKAction.repeatActionForever(SKAction.sequence([moveBG1,replaceBG1]))

var moveBG2 = SKAction.moveByX(-bgTexture2.size().width, y: 0, duration: 12)
//shift bg back to be able to repeat itself
var replaceBG2 = SKAction.moveByX(bgTexture2.size().width, y: 0, duration: 0.0) //move immediately back to replace self
var moveBG2Forever = SKAction.repeatActionForever(SKAction.sequence([moveBG2,replaceBG2]))

for var i:CGFloat = 0; i<2; i++ {
var bg = SKSpriteNode(texture: bgTexture)
var bg2 = SKSpriteNode(texture: bgTexture2)

bg.position = CGPointMake(bgTexture.size().width/2.0 + bgTexture.size().width * i + bgTexture2.size().width * i, CGRectGetMidY(self.frame))
bg2.position = CGPointMake(bgTexture.size().width/2.0 + bgTexture2.size().width + bgTexture2.size().width * 2 * i, CGRectGetMidY(self.frame))

bg.runAction(moveBG1Forever)
bg2.runAction(moveBG2Forever)

self.addChild(bg)
self.addChild(bg2)

println(bg.position)
println(bgTexture.size().width)

println(bg2.position)
println(bgTexture2.size().width)
}

最佳答案

不使用 SKActions,您可以使用 update 函数将所有背景节点一起移动,当每个节点离开屏幕时立即将每个节点移动到末尾。

为了使事情更易于管理,我们可以创建自定义背景 SKNode。

class BackgroundNode : SKNode
{
override init() {
super.init()

var bgTexture1 = SKTexture(imageNamed: "b1.jpg")
var bgTexture2 = SKTexture(imageNamed: "b2.png")


let totalBG = bgTexture1.size().width + bgTexture2.size().width

for index in 0..<2
{
var bg1 = SKSpriteNode(texture: bgTexture1)
var bg2 = SKSpriteNode(texture: bgTexture2)
bg1.anchorPoint = CGPointMake(0, 0)
bg2.anchorPoint = CGPointMake(0, 0)

let i = CGFloat(index)

bg1.position = CGPointMake(i * bgTexture1.size().width + i * bgTexture2.size().width, 0)
bg2.position = CGPointMake((i+1) * bgTexture1.size().width + i * bgTexture2.size().width, 0)

self.addChild(bg1)
self.addChild(bg2)
lastNode = bg2
}
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

var speedOfBackground : CGFloat = 300.0
var previousTime : CFTimeInterval = -1
var lastNode : SKSpriteNode!

func update(currentTime: CFTimeInterval) {

if previousTime != -1
{
var outOfBoundsSprite : SKSpriteNode? = nil
let deltaX = speedOfBackground * CGFloat(currentTime - previousTime)
for sknode in self.children
{
if let sprite = sknode as? SKSpriteNode
{
sprite.position = CGPointMake(sprite.position.x - deltaX, sprite.position.y)
if (sprite.position.x < -sprite.size.width)
{
outOfBoundsSprite = sprite
}
}
}
if (outOfBoundsSprite != nil)
{
outOfBoundsSprite?.position.x = lastNode.position.x + lastNode.size.width
lastNode = outOfBoundsSprite!
}
}
previousTime = currentTime
}

}

您可以改变 speedOfBackground 属性来改变移动速度。

它可以像这样在SKScene中使用

class GameScene: SKScene ,SKPhysicsContactDelegate {

var background : BackgroundNode!

override func didMoveToView(view: SKView) {

background = BackgroundNode()
background.speedOfBackground = 500.0
self.addChild(background)
}


override func update(currentTime: CFTimeInterval) {
background.update(currentTime)
}
}

关于ios - 如何将两个 SKTextures 合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247900/

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