Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SpriteKit
import GameplayKit
class GameScene: SKScene {
static var shared: GameScene?
var frameCount = 0
var bigSprite: SKSpriteNode?
var bigSpriteIsSetup = false
var spriteTexture: SKTexture?
var textureAtlas: SKTextureAtlas?
override func didMove(to view: SKView) {
GameScene.shared = self
}
override func update(_ currentTime: TimeInterval) {
if !bigSpriteIsSetup {
spriteTexture = SKTexture(imageNamed: "channel-background")
bigSprite = SKSpriteNode(texture: spriteTexture)
bigSprite?.size = frame.size
bigSprite?.color = .blue
bigSprite?.colorBlendFactor = 1.0
bigSprite?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
bigSprite?.position = CGPoint(x: 100 + -self.size.width / 4, y: -100 + self.size.height / 4)
bigSprite!.setScale(0.5)
self.addChild(bigSprite!)
bigSpriteIsSetup = true
}
let littleSprite = SKSpriteNode(texture: spriteTexture)
littleSprite.size = CGSize(width: 10.0, height: 10.0)
littleSprite.color = .green
littleSprite.colorBlendFactor = 1.0
littleSprite.anchorPoint = CGPoint(x: 0.5, y: 0.5)
littleSprite.zPosition = 2.0
let x = Int.random(in: Int(-self.bigSprite!.size.width)..<Int(self.bigSprite!.size.width))
let y = Int.random(in: Int(-self.bigSprite!.size.height)..<Int(self.bigSprite!.size.height))
littleSprite.position = CGPoint(x: x, y: y)
let fw = -self.scene!.size.width * 2
let fh = self.scene!.size.height * 2
if CGRect(x: 0, y: 0, width: fw, height: fh).contains(littleSprite.position) {
print("add \(littleSprite.position)")
bigSprite!.addChild(littleSprite)
} else {
print("discard \(littleSprite.position)")
}
frameCount += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment