移动背景Swift SKSpriteKit
我目前正在开发一个应用程序,试图设置一个移动背景。我有一个透明的云彩图像,我目前正在使用。我的问题是,我怎样才能使它移动得更平稳?我试着用速度比赛,但它看起来仍然落后。任何帮助都是福 这是一段我的视频 这是一张云图 这是我的密码。你认为我应该改变什么或做什么不同的事情移动背景Swift SKSpriteKit,swift,sprite-kit,skspritenode,cgpoint,Swift,Sprite Kit,Skspritenode,Cgpoint,我目前正在开发一个应用程序,试图设置一个移动背景。我有一个透明的云彩图像,我目前正在使用。我的问题是,我怎样才能使它移动得更平稳?我试着用速度比赛,但它看起来仍然落后。任何帮助都是福 这是一段我的视频 这是一张云图 这是我的密码。你认为我应该改变什么或做什么不同的事情 class GameScene: SKScene { // Background let background = SKSpriteNode(texture:SKTexture(imageNamed: "back
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)
}
}
}
}
class游戏场景:SKScene{
//背景
让background=SKSpriteNode(纹理:SKTexture(imagename:“background”))
//云彩
var mainCloud=SKSpriteNode()
var cloud1Next=SKSpriteNode()
//最后一帧时间
var lastFrameTime:TimeInterval=0
//从上一帧开始的时间
变量增量时间:时间间隔=0
覆盖func didMove(到视图:SKView){
background.position=CGPoint(x:0,y:0)
//准备云精灵
mainCloud=SKSpriteNode(纹理:
SKTexture(图像名为:“cloudbg1”))
mainCloud.position=CGPoint(x:0,y:0)
cloud1Next=mainCloud.copy()作为!SKSpriteNode
云1下一个位置=
CGPoint(x:mainCloud.position.x+mainCloud.size.width,
y:mainCloud.position.y)
//将精灵添加到场景中
self.addChild(背景)
self.addChild(mainCloud)
self.addChild(cloud1Next)
}
覆盖函数更新(uCurrentTime:TimeInterval){
//在渲染每个帧之前调用
//首先,更新增量时间值:
//如果没有最后一帧时间值,这是第一帧,
//所以delta时间为零。
如果lastFrameTime无效{
var newPosition=CGPoint.zero
//对于精灵及其副本:
用于在[sprite,nextSprite]中移动spriteToMove{
//根据速度向左移动精灵
newPosition=spriteToMove.position
newPosition.x-=CGFloat(速度*浮点(deltaTime))
spriteToMove.position=newPosition
//如果此精灵现在已脱离屏幕(即,其最右侧边缘为
//比场景最左边的边更左):
如果spriteToMove.frame.maxX
你的代码看起来很好。你的fps很低,因为你在模拟器中运行游戏。如果你在真实设备上运行,它应该是平滑的。你的代码看起来很好。你的fps很低,因为你在模拟器中运行游戏。如果你在真实设备上运行,它应该是平滑的。哇,我以前应该尝试过我甚至发了一篇帖子。简单的故障排除错误,我的错。感谢帮助者!至少我能够帮助别人做一些类似于我用代码所做的事情!哇,我应该在发帖子之前尝试一下。简单的故障排除错误,我的错。感谢帮助者!至少我能够帮助别人这么做这和我用代码做的事情很相似!