void Update()
{
frameCounter++;
Move();
ChangeDirection();
DropApple();
}
void Move ()
{
if (GetComponent ().position.x < 9 && (GetComponent ().position.x) > -9)
{
if (currentDirection == true)
GetComponent ().velocity = new Vector3 (speed, 0, 0);
else if (currentDirection == false)
GetComponent ().velocity = new Vector3 (-speed, 0, 0);
}
else if (GetComponent ().position.x > 9) {
currentDirection = !currentDirection;
GetComponent ().velocity = new Vector3 (-speed, 0, 0);
}
else if (GetComponent ().position.x < -9)
{
currentDirection = !currentDirection;
GetComponent ().velocity = new Vector3 (speed, 0, 0);
}
}
void ChangeDirection()
{
currentRoll = (int) Random.Range (1, 21);
if (currentRoll == changeRoll)
currentDirection = !currentDirection;
}
void DropApple()
{
if (frameCounter % 30 == 0)
{
GameObject newApple = Instantiate (apple) as GameObject;
newApple.GetComponent().position = GetComponent().position;
}
}
في 11/شعبان/1436 12:25 ص، غمغم عمر خ باستغراب قائلاً:
بخصوص حدود الشاشة في الدالة Move. ما الذي يمثله الرقم 9؟بتاريخ 11/شعبان/1436 12:25 ص، قطب عمر خ حاجبيه بشدة وهو يقول:
هل يمكننا الحصول على عرض النافذة بطريقة ديناميكية بدلا من كتابة الرقم 9 والاضطرار لتغيير الكود في حال تغيير عرض الشاشة.void Move ()
{
if (GetComponent ().position.x <
Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,0,0)).x - 1 &&
(GetComponent ().position.x) > Camera.main.ScreenToWorldPoint(Vector3.zero).x + 1)
{
if (currentDirection == true)
GetComponent ().velocity = new Vector3 (speed, 0, 0);
else if (currentDirection == false)
GetComponent ().velocity = new Vector3 (-speed, 0, 0);
}
else if (GetComponent ().position.x >
Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,0,0)).x - 1)
{
currentDirection = !currentDirection;
GetComponent ().velocity = new Vector3 (-speed, 0, 0);
}
else if (GetComponent ().position.x < Camera.main.ScreenToWorldPoint(Vector3.zero).x + 1)
{
currentDirection = !currentDirection;
GetComponent ().velocity = new Vector3 (speed, 0, 0);
}
}