Player.cs错误CS1525:意外的符号`public'

时间:2015-02-10 17:19:12

标签: c# unity3d

嘿伙计们我收到这个错误正如我在标题中提到的那样我自昨晚以来一直试图调试并最终归结为这一个错误,我似乎无法击败。这里是代码请看看你是否有机会错误说它在线(107,16)并抱歉我刚刚加入的链接不知道怎么把它放在代码中。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;   //Allows us to use UI.

namespace Completed 
{
    public class Player
    {
        public float restartLevelDelay = 1f;           
        public int pointsPerFood = 10;                         
        public int pointsPerSoda = 20;                         
        public int wallDamage = 1;                                     
        public Text foodText;                                          
        public AudioClip moveSound1;                           
        public AudioClip moveSound2;                           
        public AudioClip eatSound1;                                    
        public AudioClip eatSound2;                                    
        public AudioClip drinkSound1;                          
        public AudioClip drinkSound2;                          
        public AudioClip gameOverSound;                        

        private Animator animator;                                     
        private int food;                                                      
        private Vector2 touchOrigin = -Vector2.one;    


        //Start overrides the Start function of MovingObject
        protected override void Start ()
        {
                //Get a component reference to the Player's animator component
                animator = GetComponent<Animator>();

                //Get the current food point total stored in GameManager.instance between levels.
                food = GameManager.instance.playerFoodPoints;

                //Set the foodText to reflect the current player food total.
                foodText.text = "Food: " + food;

                //Call the Start function of the MovingObject base class.
                base.Start ();
        }


        //This function is called when the behaviour becomes disabled or inactive.
        private void OnDisable ()
        {

                GameManager.instance.playerFoodPoints = food;
        }


        private void Update ()
        {
                //If it's not the player's turn, exit the function.
                if(!GameManager.instance.playersTurn) return;

                int horizontal = 0;     //Used to store the horizontal move direction.
                int vertical = 0;               //Used to store the vertical move direction.




                //Check if we have a non-zero value for horizontal or vertical
                if(horizontal != 0 || vertical != 0)
                {

                        AttemptMove<Wall> (horizontal, vertical);
                }
        }


        protected override void AttemptMove <T> (int xDir, int yDir)
        {
                //Every time player moves, subtract from food points total.
                food--;

                //Update food text display to reflect current score.
                foodText.text = "Food: " + food;

                //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
                base.AttemptMove <T> (xDir, yDir);

                //Hit allows us to reference the result of the Linecast done in Move.
                RaycastHit2D hit;

                //If Move returns true, meaning Player was able to move into an empty space.
                if (Move (xDir, yDir, out hit))
                {
                        //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
                        SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
                }

                {
                //Since the player has moved and lost food points, check if the game has ended.
                CheckIfGameOver ();

                //Set the playersTurn boolean of GameManager to false now that players turn is over.
                GameManager.instance.playersTurn = false;
        }


        //OnCantMove overrides the abstract function OnCantMove in MovingObject.
        //It takes a generic parameter T which in the case of Player is a Wall which the player can attack and destroy.
          public override void OnCantMove <T> (T component)
        {
                //Set hitWall to equal the component passed in as a parameter.
                Wall hitWall = component as Wall;

                //Call the DamageWall function of the Wall we are hitting.
                hitWall.DamageWall (wallDamage);

                //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
                animator.SetTrigger ("playerChop");
        }


        //OnTriggerEnter2D is sent when another object enters a trigger collider attached to this object (2D physics only).
        private void OnTriggerEnter2D (Collider2D other)
        {
                //Check if the tag of the trigger collided with is Exit.
                if(other.tag == "Exit")
                {
                        //Invoke the Restart function to start the next level with a delay of restartLevelDelay (default 1 second).
                        Invoke ("Restart", restartLevelDelay);

                        //Disable the player object since level is over.
                        enabled = false;
                }

                //Check if the tag of the trigger collided with is Food.
                else if(other.tag == "Food")
                {
                        //Add pointsPerFood to the players current food total.
                        food += pointsPerFood;

                        //Update foodText to represent current total and notify player that they gained points
                        foodText.text = "+" + pointsPerFood + " Food: " + food; //nothing


                        //Call the RandomizeSfx function of SoundManager and pass in two eating sounds to choose between to play the eating sound effect.
                        SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);

                        //Disable the food object the player collided with.
                        other.gameObject.SetActive (false);
                }

                //Check if the tag of the trigger collided with is Soda.
                else if(other.tag == "Soda")
                {
                        //Add pointsPerSoda to players food points total
                        food += pointsPerSoda;

                        //Update foodText to represent current total and notify player that they gained points
                        foodText.text = "+" + pointsPerSoda + " Food: " + food;

                        //Call the RandomizeSfx function of SoundManager and pass in two drinking sounds to choose between to play the drinking sound effect.
                        SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);

                        //Disable the soda object the player collided with.
                        other.gameObject.SetActive (false);
                }
        }


        //Restart reloads the scene when called.
        private void Restart ()
        {
                //Load the last scene loaded, in this case Main, the only scene in the game.
                Application.LoadLevel (Application.loadedLevel);
        }


        //LoseFood is called when an enemy attacks the player.
        //It takes a parameter loss which specifies how many points to lose.
        public void LoseFood (int loss)
        {
                //Set the trigger for the player animator to transition to the playerHit animation.
                animator.SetTrigger ("playerHit");

                //Subtract lost food points from the players total.
                food -= loss;

                //Update the food display with the new total.
                foodText.text = "-" + loss + " Food: " + food;

                //Check to see if game has ended.
                CheckIfGameOver ();
        }


        //CheckIfGameOver checks if the player is out of food points and if so, ends the game.

        private void CheckIfGameOver ()
        {
                //Check if food point total is less than or equal to zero.
                if (food <= 0)
                {
                        //Call the PlaySingle function of SoundManager and pass it the gameOverSound as the audio clip to play.
                        SoundManager.instance.PlaySingle (gameOverSound);

                        //Stop the background music.
                        SoundManager.instance.musicSource.Stop();

                        //Call the GameOver function of GameManager.
                        GameManager.instance.GameOver ();
                }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在函数protected override void AttemptMove中,删除open {

// ... previous code ... 
if (Move (xDir, yDir, out hit))
{
    //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
    SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
}

        // {    REMOVE THIS ONE!
//Since the player has moved and lost food points, check if the game has ended.
CheckIfGameOver ();

//Set the playersTurn boolean of GameManager to false now that players turn is over.
GameManager.instance.playersTurn = false;