我有一个静态事件,每当玩家点击左击按钮时就会触发。
我有一个侦听这个事件的游戏对象列表,但是这个函数应该只在ableToShoot为true时执行。
即使它是假的,看起来也会执行。我确实有一个函数,它调用一个函数,使列表中的下一个对象为ableToShoot = true,但是这似乎将其置于无限循环中。
这是LeftClickEvent脚本。
public class InputManager : MonoBehaviour
{
public delegate void ShootProjectile();
public static event ShootProjectile Shoot;
private void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Shoot != null)
{
Shoot();
}
}
}
else if (Input.GetMouseButtonDown(0))
{
if (Shoot != null)
{
Shoot();
}
}
}
}这是监听LeftClickEvent的脚本:
public class ProjectileMovement : MonoBehaviour
{
public float speed = 3;
private bool ableToShoot;
public int itemInList = 0;
public StartLevelMechanics mechanics;
HitParticles particles;
public bool AbleToShoot
{
get
{
return ableToShoot;
}
set
{
ableToShoot = value;
}
}
private void Awake()
{
InputManager.Shoot += ShootProjectile;
particles = GameObject.Find("HitParticles").GetComponent<HitParticles>();
AbleToShoot = false;
}
public void ShootProjectile()
{
if (AbleToShoot)
{
GetComponentInChildren<Animator>().SetTrigger("Shoot");
iTween.MoveTo(this.gameObject, iTween.Hash("position", new Vector3(transform.position.x, 10, transform.position.z),
"easetype", iTween.EaseType.spring, "time", 1f, "oncomplete", "DestroyProjectile", "oncompletetarget",
this.gameObject));
mechanics.NewBall();
return;
}
}
}这是将下一个游戏对象AbleToShoot转换为true的机制脚本。
public void NewBall()
{
if (projectiles.Count == 1)
{
projectiles.Clear();
}
else
{
projectiles.Remove(projectiles[0]);
}
if (projectiles.Count == 0)
{
levelOver();
return;
}
lastBall = projectiles[0];
ProjectileMovement movement = lastBall.GetComponent<ProjectileMovement>();
iTween.MoveTo(lastBall, iTween.Hash("position", ShootStartPosition,
"time", 0.3f, "easetype", iTween.EaseType.easeOutQuad));
RotateBalls();
AbleToShoot();
return;
}所以我感觉这个事件以某种方式被放到了一个队列中,这个队列在每次AbleToShoot == true时都会执行,但是我在文档中找不到任何东西。
发布于 2018-08-24 07:01:50
我认为这是因为它创建了类的多个副本。在不知道它在其他地方使用的情况下,这只是一个猜测。尝试更改它,使变量ableToShoot只有一个副本:
private static bool ableToShoot;https://stackoverflow.com/questions/51974419
复制相似问题