在开始时我宣布了节点。为什么我的SpriteKit场景中的循环图像之间存在差距?
//地面节点的声明。
var groundImage: SKSpriteNode = SKSpriteNode()
var groundImage2: SKSpriteNode = SKSpriteNode()
在viewDidLoad中我有:
//Creates an instance of both sprites that are of the same image that are to be lined up against each other in the x axis creating the illution of continuity.
groundImage = SKSpriteNode(imageNamed: "Ground.png")
groundImage2 = SKSpriteNode(imageNamed: "Ground.png")
//Specifies the Z position of the images.
groundImage.zPosition = 3
groundImage2.zPosition = 3
//Scales the images to the correct size of the screen.
groundImage.size.width = self.frame.width
groundImage.size.height = self.groundImage.size.height/2
groundImage2.size.width = self.frame.width
groundImage2.size.height = self.groundImage2.size.height/2
//Specicies the x position of the images. By offsetting the second you create the illution of a long, continuous image.
groundImage.position.x = view.bounds.size.width * 0.5
groundImage2.position.x = view.bounds.size.width * 1.5
//Specifies the y postion of the images, obviously these are the same as they are not to be offset at any time.
groundImage.position.y = (view.bounds.size.height - view.bounds.size.height) + self.groundImage.size.height/2
groundImage2.position.y = (view.bounds.size.height - view.bounds.size.height) + self.groundImage2.size.height/2
//Not sure what this does yet.
groundImage.texture?.filteringMode = SKTextureFilteringMode.Nearest
groundImage2.texture?.filteringMode = SKTextureFilteringMode.Nearest
//Adds instances of the sprites to the scene.
self.addChild(groundImage)
self.addChild(groundImage2)
在更新方法我有:
//This is how the image is moved relative the number specified. The number in the variable is how many pixels the frame is being moved each frame refresh.
groundImage.position.x -= gameSpeed
groundImage2.position.x -= gameSpeed
if (groundImage.position.x <= -self.view!.bounds.size.width/2)
{
groundImage.position.x = self.view!.bounds.size.width * 1.5 // - 2
}
if (groundImage2.position.x <= -self.view!.bounds.size.width/2)
{
groundImage2.position.x = self.view!.bounds.size.width * 1.5 // - 2
}
任何尚未有当它们被循环的两个图像之间的微小的间隙。随着我使用游戏速度变量增加循环速度,这个差距会增大。
任何人都可以向我解释我做错了吗?
我检查了图像本身并没有引起问题。
感谢,
史蒂芬
你为什么不使用'SKAction'这个? – ColdSteel
这个差距可能是因为更新方法每秒调用大约60次(如果我没有错,默认值是60fps),您应该使用'SKAction'来简单地翻转图像,效率会更高。 这是你的出发点https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/AddingActionstoSprites/AddingActionstoSprites.html – ColdSteel
Thankyou,这有帮助。 – Fiducial13