gpt4 book ai didi

swift - 移动背景 Swift SpriteKit

转载 作者:行者123 更新时间:2023-11-28 08:02:44 25 4
gpt4 key购买 nike

我目前正在开发一个试图设置移动背景的应用程序。我有一个充满云的透明图像,我正在使用它。我的问题是,如何让它移动得更顺畅?我试过玩速度,但它看起来仍然很慢。任何帮助都是一种祝福。

下面是一段视频,展示了我的进展情况。 http://sendvid.com/78ggkzcj

这是云图像的图片。 Cloud Image

这是我的代码。你认为我应该改变或做不同的事情?

class GameScene: SKScene {

// Background
let background = SKSpriteNode(texture:SKTexture(imageNamed: "background"))

// Clouds
var mainCloud = SKSpriteNode()
var cloud1Next = SKSpriteNode()

// Time of last frame
var lastFrameTime : TimeInterval = 0

// Time since last frame
var deltaTime : TimeInterval = 0

override func didMove(to view: SKView) {
background.position = CGPoint(x: 0, y: 0)

// Prepare the clouds sprites
mainCloud = SKSpriteNode(texture:
SKTexture(imageNamed: "cloudbg1"))
mainCloud.position = CGPoint(x: 0, y: 0)

cloud1Next = mainCloud.copy() as! SKSpriteNode
cloud1Next.position =
CGPoint(x: mainCloud.position.x + mainCloud.size.width,
y: mainCloud.position.y)

// Add the sprites to the scene
self.addChild(background)
self.addChild(mainCloud)
self.addChild(cloud1Next)
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
// First, update the delta time values:

// If we don't have a last frame time value, this is the first frame,
// so delta time will be zero.
if lastFrameTime <= 0 {
lastFrameTime = currentTime
}

// Update delta time
deltaTime = currentTime - lastFrameTime

// Set last frame time to current time
lastFrameTime = currentTime

// Next, move each of the four pairs of sprites.
// Objects that should appear move slower than foreground objects.
self.moveSprite(sprite: mainCloud, nextSprite:cloud1Next, speed:100)
}

// Move a pair of sprites leftward based on a speed value;
// when either of the sprites goes off-screen, move it to the
// right so that it appears to be seamless movement
func moveSprite(sprite : SKSpriteNode,
nextSprite : SKSpriteNode, speed : Float) -> Void {
var newPosition = CGPoint.zero

// For both the sprite and its duplicate:
for spriteToMove in [sprite, nextSprite] {

// Shift the sprite leftward based on the speed
newPosition = spriteToMove.position
newPosition.x -= CGFloat(speed * Float(deltaTime))
spriteToMove.position = newPosition

// If this sprite is now offscreen (i.e., its rightmost edge is
// farther left than the scene's leftmost edge):
if spriteToMove.frame.maxX < self.frame.minX {

// Shift it over so that it's now to the immediate right
// of the other sprite.
// This means that the two sprites are effectively
// leap-frogging each other as they both move.
spriteToMove.position =
CGPoint(x: spriteToMove.position.x +
spriteToMove.size.width * 2,
y: spriteToMove.position.y)
}
}
}
}

最佳答案

您的代码看起来不错。您的 fps 很低,因为您在模拟器中运行游戏。如果你在真实设备上运行,它应该很流畅。

关于swift - 移动背景 Swift SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166045/

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