2015-10-18 66 views
0

在開始時我宣佈了節點。爲什麼我的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 
    } 

任何尚未有當它們被循環的兩個圖像之間的微小的間隙。隨着我使用遊戲速度變量增加循環速度,這個差距會增大。

任何人都可以向我解釋我做錯了嗎?

我檢查了圖像本身並沒有引起問題。

感謝,

史蒂芬

+0

你爲什麼不使用'SKAction'這個? – ColdSteel

+0

這個差距可能是因爲更新方法每秒調用大約60次(如果我沒有錯,默認值是60fps),您應該使用'SKAction'來簡單地翻轉圖像,效率會更高。 這是你的出發點https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/AddingActionstoSprites/AddingActionstoSprites.html – ColdSteel

+0

Thankyou,這有幫助。 – Fiducial13

回答

1

的差距可能是因爲更新方法在第二個叫做約60倍(默認爲60fps,如果我沒看錯), 你應該使用SKAction簡單地翻轉你的圖片, 它會更有效率。這裏是你的出發點:

https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/AddingActionstoSprites/AddingActionstoSprites.html

+0

這雖然有所幫助,但我仍然在圖像之間找到一條細線,比以前小得多,但仍然很明顯。有任何想法嗎? – Fiducial13

+0

哦...使圖像大1像素。 – ColdSteel

+0

掩蓋行 - 我有這樣的事情......我想我使用了3個圖像 - 我不remmeber - 你可以嘗試真正使它在這裏像素更大。 – ColdSteel

相關問題