有没有办法声明一个具有固定 int 大小的 float 数组?
Is there a way to declare a float array with a fixed int size?
有没有办法声明一个包含 float 变量且具有固定大小的 int 的数组?
int balls = 5;
float array posX[balls];
private float posY[] = 0;
基本上,我想创建一个数组,这样每个球都可以有自己的 XY 坐标,而无需创建一些新变量。我收到错误消息说我有错误的数组声明(第 3 行)并且 posX 和 Balls 在当前上下文中不存在(第 2 行)。上面的代码是我的一些尝试和错误。我正在制作一个 xamarin 表单应用程序,所以也许这与它有关?我的完整代码是这样的:
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;
}
}
}
这是一个固定大小 (10) 的浮点数组
float[] array = new float[10];
为防止错误,请在构造函数中进行赋值
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;
}
有没有办法声明一个包含 float 变量且具有固定大小的 int 的数组?
int balls = 5;
float array posX[balls];
private float posY[] = 0;
基本上,我想创建一个数组,这样每个球都可以有自己的 XY 坐标,而无需创建一些新变量。我收到错误消息说我有错误的数组声明(第 3 行)并且 posX 和 Balls 在当前上下文中不存在(第 2 行)。上面的代码是我的一些尝试和错误。我正在制作一个 xamarin 表单应用程序,所以也许这与它有关?我的完整代码是这样的:
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;
}
}
}
这是一个固定大小 (10) 的浮点数组
float[] array = new float[10];
为防止错误,请在构造函数中进行赋值
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;
}