if (col.gameObject.tag.Contains("Fruit")) { //�ж���Ϸ�Ƿ���� if ((int)GameManager.instance.gameState < (int)GameState.Gameover) { if(col.gameObject.GetComponent<Fruit>().fruitState==FruitState.Collision) { GameManager.instance.gameState = GameState.Gameover; Invoke("OpenMoveAndCalculateScore", 0.5f); } } if (GameManager.instance.gameState == GameState.CalculateScore) { float currentScore = col.GetComponent<Fruit>().fruitScore; GameManager.instance.totalScore += currentScore; GameManager.instance.totalScaoreText.text = "��ǰ�÷֣�" + GameManager.instance.totalScore.ToString(); GameObject.Destroy(col.gameObject);//����ˮ�� }分析这段代码
时间: 2023-12-06 21:42:03 浏览: 283
这段代码主要是在检测游戏中的水果是否被碰撞,并且在游戏状态不为GameOver时,如果水果的状态为Collision,将游戏状态设置为GameOver,并且通过Invoke方法调用OpenMoveAndCalculateScore方法。在游戏状态为CalculateScore时,将当前水果得分加入总得分中,并且更新总得分文本,最后销毁碰撞的水果对象。其中,游戏状态是通过GameManager.instance.gameState变量来进行管理的。
相关问题
public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }
这是一个在2D游戏中检测碰撞的函数。当游戏对象与另一个游戏对象发生碰撞时,该函数会被调用。在函数中,首先判断碰撞的游戏对象的标签是否为“GoldCoin”,如果是,则销毁该游戏对象,增加得分,并播放声音。如果不是,则停止播放声音。然后判断碰撞的游戏对象的标签是否为“zhi”,如果是,则销毁该游戏对象并减少血量。最后,如果碰撞的游戏对象的标签是“zhi2”,则忽略该碰撞。
public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 播放脚步声 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) { if (!audioSource.isPlaying) { audioSource.clip = footstepSound; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }
这段代码主要是一个角色的移动和攻击行为,使用了键盘和鼠标输入来响应玩家操作。角色可以向上、下、左、右移动,并且在移动时会播放脚步声音效。角色还可以使用鼠标左键攻击,发射一个子弹。如果角色与"GoldCoin"标签的游戏对象碰撞,就会增加分数,如果角色与"zhi"标签的游戏对象碰撞,就会减少血量。如果角色与"zhi2"标签的游戏对象碰撞,就会忽略该碰撞。
阅读全文