what's wrong with my code?
Sorry to say, nearly everything.
There are so many errors everywhere. You should start studying Swift first. As DarkPaw said, AI (ChatGPT or other) does not replace plain intelligence. It seems you don't even understand your own code, isn't it ?
There are even many typos in code:
extra }
Balls[].xPosition is wrong syntax. What do you mean here ?
The problem for the error message (there are likely many other) is you call
for item in Balls
You have to use ForEach in a view.
In addition, Balls is not an array, even less an instance of array.
And View struct should be outside of the class. Otherwise, how do you plan to access it ?
Here is a start to correct those many errors:
class Balls: Identifiable {
//var color: String
var xPosition: Int
var yPosition: Int
var xVelocity: Int
var yVelocity: Int
var radius: Int
var gravity: CGFloat
var restitution: Int
var other: Balls?
init(xPosition: Int, yPosition: Int, xVelocity: Int, yVelocity: Int, radius: Int, gravity: CGFloat, restitution: Int)
//ADD COLOR
{
//self.color = color
self.xPosition = xPosition
self.yPosition = yPosition
self.xVelocity = xVelocity
self.yVelocity = yVelocity
self.radius = radius
self.gravity = gravity
self.restitution = restitution
}
}
struct UserView: View {
let ball1: Balls = Balls (xPosition: 100, yPosition: 100, xVelocity: 3, yVelocity: 0, radius: 3, gravity: 0.3, restitution: 1)
let ball2: Balls = Balls (xPosition: 200, yPosition: 50, xVelocity: -2, yVelocity: 2, radius: 3, gravity: 0.3, restitution: 1)
let ball3: Balls = Balls (xPosition: 300, yPosition: 150, xVelocity: 4, yVelocity: -3, radius: 3, gravity: 0.3, restitution: 1)
var timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
@State var balls: [Balls]
init() {
balls = [ball1, ball2, ball3]
}
var body: some View {
VStack {
//Background color
Color.gray.edgesIgnoringSafeArea(.all)
//var balls [Int] = [ball1; ball2; ball3]
// for item in Balls {
ForEach($balls) { $ball in // You change ball later, so a binding here
Circle()
.fill(Color.black)
.frame(width: 50, height: 50)
.position(x: CGFloat($ball.xPosition.wrappedValue), y: CGFloat($ball.yPosition.wrappedValue)) // Wrappedvalue because ball is a binding
.onReceive(timer) { _ in // Need on argument in the closure
// self.yVelocity += self.gravity // self refers to View, not to a ball
ball.yVelocity = ball.yVelocity + Int(ball.gravity) // One is Int, the other Float. Why ? That forbids +=
// ball.xPosition = CGPoint(ball.xPosition + ball.xVelocity) // That's not a CGPoint, needs 2 arguments
ball.xPosition = ball.xPosition + ball.xVelocity
ball .yPosition = ball.yPosition + ball.yVelocity
if ball.yPosition >= 500 - 25 {
ball.yPosition = 500 - 25
ball.yVelocity = -ball.yVelocity * ball.restitution
}
if ball.xPosition <= 25 {
ball.xPosition = 25
ball.xVelocity = -ball.xVelocity
}
if ball.xPosition >= 375 {
ball.xPosition = 375
ball.xVelocity = -ball.xVelocity // I suppose that's what you mean insted of ball.velocityX
}
/* errrors below you'll have to correct yourself
let dx: int = other.xPosition - self.xPosition
let dy: int = other.yPosition - self.yPosition
let distance: int = sqrt (dx * dx + dy * dy)
if distance < self.radius + other.radius {
self.xVelocity = -self.xVelocity * self.restitution
self.yVelocity = -self.yVelocity * self.restitution
other.xVelocity = -other.xVelocity * self.restitution
other.yVelocity = -other.yVelocity * self.restitution
}
*/
}
}
}
}
}