Post

Replies

Boosts

Views

Activity

Reply to How do I use "views" and structures / what's wrong with my code?
here is a version of my code without any of that structure/view/body parts of the language that I don't understand maybe this will be easier to fix because its basically just the logic of the program that I want to write...sort of modeled after my knowledge of C++, and im trying to get to a version that is the correct swift syntax, etc. like I said in the comments, im really looking for help with structures - bodies, views, @state, @observedobject, etc class Balls { //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 } 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() //Background color Color.gray.edgesIgnoringSafeArea(.all) for item in Balls { Circle() .fill(Color.black) .frame(width: 50, height: 50) .position(Balls[].xPosition, Balls[].yPosition) .onReceive(timer) { self.yVelocity += self.gravity self.xPosition = CGPoint(self.xPosition + self.xVelocity) self .yPosition = CGPoint (self.yPosition + self.yVelocity) if self.yPosition >= 500 - 25 { self.yPosition = 500 - 25 self.yVelocity = -self.yVelocity * self.restitution } if self.xPosition <= 25 { self.xPosition = 25 self.xVelocity = -self.xVelocity } if self.xPosition >= 375 { self.xPosition = 375 self .xVelocity = -self.velocityX } 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 } } }
Topic: UI Frameworks SubTopic: SwiftUI
Apr ’25