-3

Is there a way to declare a array that contains float variables and that has a fixed size of an int?

int balls = 5;
float array posX[balls];
private float posY[] = 0;

basically, I want to create an array so that each ball can have its own XY coords without creating a handful of new variables. I am getting errors saying that I have bad array declaration (line 3) and that posX and Balls does not exist in the current context (line 2). The code above is some of my trial and error. I'm making a xamarin forms app so maybe that has something to do with it? My full code is this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;
using Xamarin.Forms.Internals;

namespace Ball_Bounce
{
    public partial class MainPage : ContentPage
    {
        int balls = 5;
        float array posX[balls];
        private float posY[] = 0;
        float dx = 5;
        float dy = 5;
        int ballD = 100;
        bool gravity = false;
        float time = 1f / 60;
        float mass = 10;
        float g = -9.8f;
        float forceOnWalls = 0;
        int bounces = 0;
      

                        

        SKPaint blackFillPaint = new SKPaint
        {
            Style = SKPaintStyle.Fill,
            Color = SKColors.Black
        };

        SKPaint Ball = new SKPaint
        {
            Color = SKColors.Black,

        };
        public MainPage()
        {
            InitializeComponent();

            Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
               {
                   CanvasView.InvalidateSurface();
                   return true;
               });
            
        }

        private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;
            canvas.Clear(SKColors.SteelBlue);
            float width = e.Info.Width;
            float height = e.Info.Height;
            canvas.Translate(width / 2, height / 2);


            posX += dx;
            posY += dy;
            if (gravity == false)
            {
                bounces = 0;
                if (posX >= width / 2 - ballD|| posX <= -width / 2 + ballD)
                {
                    dx = -dx;
                }
                else if (posY >= height / 2 - ballD|| posY <= -height / 2 + ballD)
                {
                    dy = -dy;
                }
            }
            else if (gravity == true)
            {

                if (bounces >= 8)
                {
                    dy = 0;
                    posY = height / 2 - ballD;
                }
                dy = Gravity(dy, time);
                if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
                {
                    dx = -dx;
                }
                else if (posY >= height / 2 - ballD || posY <= -height / 2 + ballD)
                {
                    dy = -dy+dy/5;
                    bounces += 1;
                }
                

                //forceOnWalls = mass * Math.Abs(dy);
                //if (posY < height / 2 + 11*ballD/10)
                //{


                //    dy = Gravity(dy, time);

                //    if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
                //    {
                //        dx = -dx;
                //    }
                //    else if (posY > (height / 2) - (ballD + 10))
                //    {
                //        dy = -dy + 2f; //slows bouncing
                //    }
            }



                //else if (posY >= height / 2 - 9 * ballD / 10 && posY <= height / 2 - 2 * ballD && (forceOnWalls < 1 && forceOnWalls > 0))
                //{
                //    if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
                //    {
                //        dx = -dx;
                //    }
                    
                //    posY = height / 2 - ballD;
                //    dy = 0;


                

            

            
            canvas.DrawCircle(posX, posY, ballD, blackFillPaint);



        }
        void OnToggled(object sender, ToggledEventArgs e)
        {
            if (gravity == true)
            {
                dy = -5;
                gravity = false;
                
            }
            else
            {
                gravity = true;
            }
        }
        void OnSpeedValueChanged(object sender, ValueChangedEventArgs args)
        {
            double value = args.NewValue;
            float valueF = Convert.ToSingle(value);
            float signX = Math.Sign(dx);
            float signY = Math.Sign(dy);
            dx = valueF * signX;
            dy = valueF * signY;            
        }
        void OnGravityValueChanged(object sender, ValueChangedEventArgs args) //slider that controls 
        {
            double value = args.NewValue;
            float valueF = Convert.ToSingle(value);
            g = valueF;
        }
        public static float Gravity(float vOld, float time) //calculates the dy changes 
        {
            float g = 15f;
            float vNew = vOld + g * time;
            return vNew;
            
        }
    }
}
Cfun
  • 8,442
  • 4
  • 30
  • 62
  • What do you mean by "a fixed size of an int"? You declare an array and its size at the same time like so: `private float[] posY = new float[10];` (for an array of 10 floats, in this example). – Matthew Watson Nov 15 '20 at 21:53
  • 1
    The `System.Drawing.PointF` struct has `X` and `Y` float valued properties. The syntax for declaring an array of them would be `PointF [] ballPositions = new PointF[numBalls];`. You could also declare and initialize them using the *collection initialization pattern*. You also be happier with a `List` rather than an array – Flydog57 Nov 15 '20 at 21:55
  • @MatthewWatson where you have the 10, i want to use the balls variable. when I do that it says bad array declaration (CS0650) – JonTheBrownDog Nov 15 '20 at 21:58
  • If the array is a field (like your `posY`) then you have to use a `const` when initialising it, so try declaring balls as `const int balls = 5;` – Matthew Watson Nov 15 '20 at 22:04

1 Answers1

2

this is a fixed size (10) float array

float[] array = new float[10];

To prevent your errors, do your assignments inside the constructor

namespace Ball_Bounce
{
    public partial class MainPage : ContentPage
    {
        int balls;
        float[] array;
        private float posY;
    ....
  
    public MainPage() 
    {
       balls = 5;
       array = new float[balls];
       posY = 0;
    }
sasal
  • 201
  • 1
  • 7