Skip to content

Instantly share code, notes, and snippets.

@davidinga
Created January 25, 2021 16:44
Show Gist options
  • Save davidinga/d04d270adc01f0a5d4bf67b73591e941 to your computer and use it in GitHub Desktop.
Save davidinga/d04d270adc01f0a5d4bf67b73591e941 to your computer and use it in GitHub Desktop.
SpriteKit Typecasting Question
//
// GameScene.swift
// project11
//
// Created by David Inga on 10/23/20.
//
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background.jpg")
background.position = CGPoint(x: 512, y: 384)
background.blendMode = .replace
background.zPosition = -1
addChild(background)
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
// Note: touch.location(in:) takes a UIView. We're passing in a SKScene?
// How come we don't have to typecast?
let location = touch.location(in: self)
let box = SKSpriteNode(color: UIColor.red, size: CGSize(width: 64, height: 64))
box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 64, height: 64))
box.position = location
addChild(box)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment