【问题标题】:Code Error. The modifier public is not valid. Why?代码错误。修饰符 public 无效。为什么?
【发布时间】:2020-02-29 23:39:34
【问题描述】:

我正在 Unity 中制作 2D 游戏,并试图让我的可移动角色在每次屏幕上出现对话时停止。

我正在使用 Fungus 扩展进行对话,因为我是编码新手。我尝试的每件事都会遇到问题。

我当前的问题是修饰符 'public' 对此项目无效。

有人知道如何解决这个问题吗?我附上了下面的代码。我认为问题在于public void CantMove()public void CanMove() 行。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    public Rigidbody2D theRB;
    public float jumpForce;

    private bool isGrounded;
    public Transform groundCheckPoint;
    public LayerMask whatIsGround;

    private bool canDoubleJump;

    private bool canMove = true;

    private Animator anim;
    private SpriteRenderer theSR;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if(!canMove)
        {
            theRB.velocity = new Vector2(0, 0);
        }
        else
        {
            theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);
        }

        public void CantMove()
        {
            canMove = false;
        }
        public void CanMove()
        {
            canMove = true;
        }

       //theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);

       isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatIsGround);

       if(isGrounded)
       {
            canDoubleJump = true;
        }

       if(Input.GetButtonDown("Jump"))
       {
            if (isGrounded)
            {
                theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
            }
            else
            {
                if(canDoubleJump)
                {
                    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                    canDoubleJump = false;
                }
            }
        }

        if(theRB.velocity.x > 0)
        {
            theSR.flipX = true;
        }   else if(theRB.velocity.x < 0)
        {
            theSR.flipX = false;
        }

        anim.SetFloat("moveSpeed", Mathf.Abs( theRB.velocity.x));
        anim.SetBool("isGrounded", isGrounded);
    }
}

'''

【问题讨论】:

    标签: xcode unity3d game-physics public public-key


    【解决方案1】:

    您的问题是您为 CanMove 和 CantMove 定义的两个函数是在 Update 函数体中声明的...这使它们成为本地范围的函数,这意味着它们永远不能具有公共访问权限,并且只能从 Update 函数中调用自己。

    像这样将这两个函数移到 Update 函数体之外...

    void Update() {
       ...
    }
    
    public void CantMove() {
        canMove = false;
    }
    
    public void CanMove() {
        canMove = true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2015-10-22
      • 2015-11-13
      • 2011-04-12
      • 2017-06-04
      相关资源
      最近更新 更多