Post not yet marked as solved
Hello,
I'm working in SpriteKit and I need to create a 'playable area', an area that is same size as the device screen so that I can stop my player from moving off-screen.
I'm using the following line of code:
var playableRect: CGRect = UIScreen.main.bounds
But the resulting rectangle is about a quarter of the device screen, with a corner of that rectangle at what looks to be the center of the screen. And the device orientation doesn't change that.
I've tried everything I can think of. Nothing is working.
How do I create a rectangle that's the same size as the device screen?
Thank you
Post not yet marked as solved
Hi,
I'm trying to use SpriteKit with SwiftUI. But I'm having trouble resizing the GameScene when I use it in Swiftui.
Here is my current coding.
First, the coding for GameScene:
import SpriteKit
import GameplayKit
@objcMembers
class GameScene: SKScene {
let player = SKSpriteNode(imageNamed: "player-motorbike")
var touchingPlayer = false
override func didMove(to view: SKView) {
view.allowsTransparency = true
self.backgroundColor = .clear
if let particles = SKEmitterNode(fileNamed: "Mud") {
let farRightPt = frame.maxX // start the emitter at the far right x-point of the view
particles.advanceSimulationTime(10)
particles.position.x = farRightPt
addChild(particles)
}
let nearLeftPt = frame.minX * 3 / 4 // start the player at a quarter of the way to the far left x-point of the view
player.position.x = nearLeftPt
player.zPosition = 1
addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user touches the screen
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
if tappedNodes.contains(player) {
touchingPlayer = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touchingPlayer else { return }
guard let touch = touches.first else { return }
let location = touch.location(in: self)
player.position = location
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user stops touching the screen
touchingPlayer = false
}
override func update(_ currentTime: TimeInterval) {
// this method is called before each frame is rendered
}
}
And now my coding for adding that GameScene to my SwiftUI programming:
import SwiftUI
import SpriteKit
import GameplayKit
struct ContentView2: View {
var scene: SKScene {
let scene = GameScene()
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
scene.size = CGSize(width: width, height: height)
scene.scaleMode = .fill
return scene
}
var body: some View {
ZStack {
Image("road")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
SpriteView(scene: scene, options: [.allowsTransparency])
.ignoresSafeArea()
}
}
}
struct ContentView2_Previews: PreviewProvider {
static var previews: some View {
ContentView2()
}
}
The coding above pushes the GameScene down so that it only occupies about a third of the screen.
I've tried other ways to resize the GameScene. The most luck I've had is with setting:
scene.size = CGSize(width: 300, height: 400)
That allows the GameScene to fill up the entire screen, but the objects show as distorted.
I think I should probably use GeometryReader here, but since the scene isn't some View, that doesn't work either.
Does anyone know how to resize an SKScene so that it can be used in SwiftUI?
Thank you.
Post not yet marked as solved
I'm trying to make a game using SwiftUI and SpriteKit. And I want to be able to layout the game in a ZStack with the game layered over the background image. In order to do that, I need to make the background of the SKScene transparent. But when I do that, the nodes that I add to the SKScene no longer show up.
Here's the coding that I'm using:
import SwiftUI
import SpriteKit
import GameplayKit
class GameScene: SKScene {
	
	override func didMove(to view: SKView) {
		
		view.allowsTransparency = true
		self.backgroundColor = .clear
		view.alpha = 0.0
		view.isOpaque = true
		view.backgroundColor = SKColor.clear.withAlphaComponent(0.0)
		
		if let particles = SKEmitterNode(fileNamed: "Mud") {
			particles.position.x = 512
			addChild(particles)
		}
	}
}
struct ContentView: View {
	
	var scene: SKScene {
		
		let scene = GameScene()
		scene.size = CGSize(width: 300, height: 400)
		scene.scaleMode = .fill
		
		return scene
	}
	
	var body: some View {
		ZStack {
			Image("road")
				.resizable()
				.aspectRatio(contentMode: .fill)
				.ignoresSafeArea()
			SpriteView(scene: scene)
				.ignoresSafeArea()
				.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
		}
	}
}
So the background of the SKScene is, in fact, transparent and I can see the image layered underneath it in the ZStack. But, everything else in the scene is also transparent so that I can't see the emitter effect at all.
How can I make just the background of the SKScene transparent?
Thank you.
Post not yet marked as solved
Hello,I'm pretty new to using SpriteKit. So I'm taking a course on it from Udemy.The scenekit action editor/timeline that shows in this course shows second markings. But those second markings don't show in the Xcode I'm using.Can anyone please tell me how to show the time markings on the scenekit action editor/timeline?BTW, I'm using Xcode Beta 11.2, but I have the same problem in Xcode 11.1Thank you!Mary