Hey guys,
I'm having a huge problem and can't understand how to fix it.
I was trying to recreate an Xcode app project with a Xcode Playground, now the point is that in Xcode Project on the side of the screen there are the options to add files.
Like this:
And in the Playground it is like this:
But here, when I add the Swift Files under Shared Sources, and create the ContentView() in Main - Xcode Playground, it says me that it can't find the Helicopter, Pixel, Obstacle, so it can't find the Shared Recourses' files.
Here's all the code. ContentView(Main):
import SwiftUI import PlaygroundSupport struct ContentView: View { @State private var heliPosition = CGPoint(x:100, y: 100) @State private var obstPosition = CGPoint(x:1000, y: 300) @State private var isPaused = false @State private var score = 0 @State var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect() var body: some View { GeometryReader { geo in ZStack{ Helicopter() .position(self.heliPosition) .onReceive(self.timer) {_ in self.gravity() } Obstacle() .position(self.obstPosition) .onReceive(self.timer) {_ in self.obstMove() } Text("\(self.score)") .foregroundColor(.white) .position(x: geo.size.width - 100, y: geo.size.height / 10 ) self.isPaused ? Button("Resume") { self.resume() } : nil } .frame(width: geo.size.width, height: geo.size.height) .background(Color.black) .gesture( TapGesture() .onEnded{ withAnimation{ self.heliPosition.y += 100 } }) .onReceive(self.timer) { _ in self.collisionDetection(); self.score += 1 } } .edgesIgnoringSafeArea(.all) } func gravity() { withAnimation{ self.heliPosition.y -= 20 } } func obstMove() { if self.obstPosition.y > 0 { withAnimation{ self.obstPosition.y -= 20 } } else { self.obstPosition.x = 1000 self.obstPosition.y = CGFloat.random(in: 0...500) } } func pause() { self.timer.upstream.connect().cancel() } func resume() { self.timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect() self.obstPosition.x = 1000 self.heliPosition = CGPoint(x: 100, y: 100) self.isPaused = false self.score = 0 } func collisionDetection() { if abs(heliPosition.x - obstPosition.x) < (25 + 10) && abs(heliPosition.y - obstPosition.y) < (25 + 100) { self.pause() self.isPaused = true } } } PlaygroundPage.current.setLiveView(ContentView())
Pixel:
import SwiftUI public struct Pixel: View { let size: CGFloat let color: Color public var body: some View { Rectangle() .frame(width: size, height: size) .foregroundColor(color) } }
Helicopter:
import SwiftUI public struct Helicopter: View { let rows = 5 let columns = 5 let size: CGFloat = 10 let heliBlocks: [[Color]] = [[.gray,.gray,.gray,.gray,.gray], [.clear,.clear,.green,.clear,.clear], [.green,.green,.green,.green,.gray], [.clear,.clear,.green,.green,.green], [.clear,.clear,.gray,.clear,.gray]] public var body: some View { VStack (spacing: 0) { ForEach((0...self.rows - 1), id: \.self) { row in HStack (spacing: 0) { ForEach((0...self.columns - 1), id: \.self) { col in VStack (spacing: 0) { Pixel(size: self.size, color: self.heliBlocks[row][col]) } } } } } } }
Obstacle:
import SwiftUI public struct Obstacle: View { let width: CGFloat = 20 let height: CGFloat = 200 public var body: some View { Rectangle() .frame(width: width, height: height) .foregroundColor(Color.green) } }
Guys please, if anyone knows how to fix this, please help me. I will be grateful to you.