【发布时间】:2020-11-15 21:47:43
【问题描述】:
有没有办法声明一个包含浮点变量并且具有固定大小的 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;
}
}
}
【问题讨论】:
-
“固定大小的 int”是什么意思?您同时声明一个数组及其大小,如下所示:
private float[] posY = new float[10];(在此示例中为 10 个浮点数的数组)。 -
System.Drawing.PointF结构具有X和Y浮点值属性。声明它们的数组的语法是PointF [] ballPositions = new PointF[numBalls];。您还可以使用集合初始化模式声明和初始化它们。List<PointF>而不是数组,你也会更开心 -
@MatthewWatson 你有 10 个,我想使用 balls 变量。当我这样做时,它说数组声明错误(CS0650)
-
如果数组是一个字段(比如你的
posY)那么你必须在初始化它时使用const,所以尝试将球声明为const int balls = 5;
标签: c# arrays floating-point declaration