0

I'm trying to make an image of the size 183x250 loop infinitely while speeding up.

Looping is fine if it doesnt speed up, however if I speed it up by reducing the duration of TimeInterval the image doesn't get stitched and breaks.

import SpriteKit import SwiftUI

class MainMenu: SKScene {

private var backgroundNodes: [SKSpriteNode] = []
private let numBackgroundNodes = 2
private let backgroundNodeHeight: CGFloat = 2000
private var moveupDuration: TimeInterval = 7

override func update(_ currentTime: TimeInterval) {
    if Int(currentTime) % Int(moveupDuration) == 10 {
        speedUp()
    }
    
    print("current second: \(moveupDuration)")
}

override func didMove(to view: SKView) { self.backgroundColor = UIColor.clear

    let backgroundTexture = SKTexture(imageNamed: "road.png")
    let totalBackgroundHeight = CGFloat(numBackgroundNodes) * backgroundNodeHeight
   
    for i in 0..<numBackgroundNodes {
        let backgroundNode = SKSpriteNode(texture: backgroundTexture, size: CGSize(width: self.frame.width, height: backgroundNodeHeight))
        backgroundNode.anchorPoint = CGPoint(x: 0.5, y: 0)
        backgroundNode.position = CGPoint(x: self.frame.midX, y: self.frame.minY + (CGFloat(i) * backgroundNodeHeight))
        backgroundNode.zPosition = -1
        self.addChild(backgroundNode)
        backgroundNodes.append(backgroundNode)
    }

func speedUp() {
    moveupDuration -= 1
    let moveUp = SKAction.moveBy(x: 0, y: -backgroundNodeHeight, duration: moveupDuration)
    let resetPosition = SKAction.moveBy(x:0, y: backgroundNodeHeight, duration: moveupDuration)
    let sequence = SKAction.sequence([moveUp, resetPosition])
    let repeatForever = SKAction.repeatForever(sequence)

    for backgroundNode in backgroundNodes {
        backgroundNode.run(repeatForever)
    }
}

I was expecting it to just infintely still loop and not glitch out.

  • you're adding multiple actions to the same nodes, and they conflict causing glitches. make sure you remove any existing actions (`backgroundNode.removeAction(forKey: "road loop")`) before you add a new action (`backgroundNode.run(repeatForever, withKey: "road loop")`). the key allows you to name the action, so you can remove it if you need to. your code has a few other bugs still, but this should help. – Fault Apr 17 '23 at 13:24

0 Answers0