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;
}
}
}