How do I use "views" and structures / what's wrong with my code?

This thread has been locked by a moderator; it no longer accepts new replies.

what's wrong with my code? im getting error "Closure containing control flow statement cannot be used with result builder 'ViewBuilder' " next to my for loop...


//
//  ContentView.swift
//  Bouncing Balls Simulation without ChatGPT's Code
//
//  Created by Hillary Basile on 3/30/25.
//

import SwiftUI
import Foundation
import CoreGraphics


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)
    
    struct UserView: View
    {
        
        var timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
        
        
        var body: some View
        {
            VStack
            {
                //Background color
                Color.gray.edgesIgnoringSafeArea(.all)
                
                //var balls [Int] = [ball1; ball2; ball3]
               
                @ViewBuilder
                func updateBalls()
                {
                    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
                            }
                        }
                            updateBalls()
                        }
                    }
                }
            }
        }
    }
    
    
    #Preview
    {
        ContentView()
    }
}

Answered by DTS Engineer in 836046022

Let’s focus this discussion on your other thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Boost
Accepted Answer

how to delete reply?

please see the other version of this post...

You can't, but you also don't need to post a new thread for the exact same issue.

Let’s focus this discussion on your other thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How do I use "views" and structures / what's wrong with my code?
 
 
Q