理想情况下,我的程序将基于2个变量创建一系列堆叠的正方形。 1是每个系列的平方数,另一个是将要多少个平方。每个系列均从屏幕中心以不同角度均匀分布。如果有2个系列,则一个面向0度,另一个面向180度。如果为3,则第一个将面对0度,第二个将面对120度,第三个将面对240度。在我的数学或实现这种效果的方法中,我搞砸了。
由于某种原因,当前的结果是斐波那契螺旋线。这是下面的可疑函数(带有所有变量定义) 另外,button是我上的一堂课,而不是一个错字:
public button Led; //Object used to create series
public Slider ledNumber;// controls how many LEDS there are per series
public Slider IncrementsPerRotation;//Controls how many series there are
public Text ledText;//text above ledNumber slider
public Text incrementText;//text above IncrementsPerRotation Slider
public Transform canvas;//Canvas where objects will be created
List<List<button>> LedConfig = new List<List<button>>();//where all the LEDs will be stored
private float buttonW, middleX,middleY; // the width of the LED and the middle values of the screen
private int prevLed = 0, prevIncrements = 0;// previous values for both sliders
// Start is called before the first frame update
void Start()
{
buttonW = Led.transform.localScale.x;// set LED width
Vector2 temp = Camera.main.ScreenToWorldPoint(new Vector2 (Screen.width / 2, Screen.height / 2));// get middle values
//assign middle values
middleX = temp.x;
middleY = temp.y;
}
// Update is called once per frame
void Update()
{
if (prevLed != (int)ledNumber.value && (int)ledNumber.value > 0)//has the slider changed and is it positive
{
Debug.Log("prevLED Changed");
if (prevLed < ledNumber.value)// am I removing or adding buttons
{
Debug.Log("Adding LED");
for (int x = 0; x < (int)ledNumber.value - prevLed; ++x)//for the difference in the number changed
{
for (int y = 0; y < IncrementsPerRotation.value; y++)//add a led to the top row
{
float angle = 360 / IncrementsPerRotation.value * y;// get angle
// if there isnt enough lists, add another one;
if(LedConfig.Count-1 < y)
{
LedConfig.Add(new List<button>());
++prevIncrements;
}
//object: LED Where: (see GetPos) What angle: angle converted to radians
LedConfig[y].Add(Instantiate(Led, GetPos(y, angle), new Quaternion(Quaternion.identity.x, Quaternion.identity.y, angle * (Mathf.PI / 180f), Quaternion.identity.w),canvas));
Debug.Log("got past instantiate");
}
++prevLed;
Debug.Log("end of outer for loop");
}
}
else
{
Debug.Log("Removing LED");
for (int x = 0; x < prevLed - (int)ledNumber.value; ++x)//for the difference in the number changed
{
for (int y = 0; y < LedConfig.Count; y++)//delete the top row of LEDs
{
LedConfig[y][LedConfig[y].Count - 1].delete();//delete the LED
LedConfig[y].RemoveAt(LedConfig[y].Count - 1);//remove it from the list
}
--prevLed;
Debug.Log("end of outer for loop");
}
}
}
if (prevIncrements != (int)IncrementsPerRotation.value && (int)IncrementsPerRotation.value > 0)
{
deleteAll();
prevLed = 0;
}
}
void deleteAll()
{
for (int x = 0; x < LedConfig.Count; ++x)//for the difference in the number changed
{
for (int y = 0; y < LedConfig[x].Count; y++)//delete the top row of LEDs
{
LedConfig[y][LedConfig[y].Count - (y + 1)].delete();//delete the LED
LedConfig[y].RemoveAt(LedConfig[y].Count - (y + 1));//remove it from the list
}
}
}
private Vector2 GetPos(int a,float angle)
{
float angleInRadians = angle * (Mathf.PI / 180f);
++a;
Debug.Log("x " + (buttonW * a) * (Mathf.Sin(angleInRadians) * 180 / Mathf.PI));
Debug.Log("y " + (buttonW * a) * (Mathf.Cos(angleInRadians) * 180 / Mathf.PI));
Debug.Log("middleX " + middleX);
Debug.Log("middleY " + middleY);
Debug.Log("tot X " + (middleX + buttonW * a * Mathf.Sin(angleInRadians)));
Debug.Log("tot Y " + (middleY + buttonW * a * Mathf.Cos(angleInRadians)));
return new Vector2(middleX + buttonW * a * Mathf.Sin(angleInRadians), middleY + buttonW * a * (Mathf.Cos(angleInRadians)));
}
该代码有很多错误: 移动ledNumber会在同一位置创建多个正方形
移动IncrementsPerRotations会导致索引超出范围错误,这会暂停游戏,如果继续游戏(通过按暂停按钮),则斐波那契螺旋线将产生正方形,并且正方形处于任意角度。
这不是全部代码,仅是与错误相关的部分。
编辑:Here是每个旋转值1-4的增量和3的led数的期望输出的外观的视觉辅助。