Unity3d如何从另一个脚本引用滑动控件?

时间:2013-03-21 02:31:33

标签: c# android unity3d swipe

我已经尝试过各种可能的方法,但没有成功。我有一个滑动脚本和一个游戏/控制脚本。我试图在我的控制脚本中引用滑动控件作为控制我的角色的方法。到目前为止,我在Unity中只收到一个错误。

Assets / RuinRunStarterKit / Scripts / csTempleRun.cs(307,25):错误CS1501:方法UpdatePlayer' takes 0'参数没有重载

我尝试在UpdatePlayer的括号内添加TouchDetector.enTouchType,但这给了我三个错误而不是一个错误。我联系了很多人,并访问了许多网站寻找答案但未能产生工作结果。我没有接受过编程方面的教育。我只是在线研究,以确定如何编程,如编程。我在这里的第二个脚本来自我在Unity资源商店购买的资产。我已经联系他们以获得添加滑动控件的支持,他们通过电子邮件向我发送了触摸脚本,但没有告诉我如何实现它。我只是想在我的手机上玩这个游戏,用滑动作为控件。这是供个人使用的。如果有人能帮助我达到预期的效果,我真的很感激。同样,我希望能够使用滑动控制角色。谢谢。这是我的脚本:

TouchDetector.cs;

    using UnityEngine;
    using System.Collections;

    public class TouchDetector : MonoBehaviour {
public delegate void deTouchEvent
    (enTouchType touchType);

public static event
    deTouchEvent
    evTouchEvent;

public enum enTouchType
{
    SwipeLeft,
    SwipeRight,
    SwipeDown,
    SwipeUp,
}   

void Start ()
{   
}   

void Update ()
{   
    if (evTouchEvent == null)
        return;

    if (Input.GetKeyDown(KeyCode.UpArrow   )) evTouchEvent(enTouchType.SwipeUp   );
    if (Input.GetKeyDown(KeyCode.DownArrow )) evTouchEvent(enTouchType.SwipeDown );
    if (Input.GetKeyDown(KeyCode.LeftArrow )) evTouchEvent(enTouchType.SwipeLeft );
    if (Input.GetKeyDown(KeyCode.RightArrow)) evTouchEvent(enTouchType.SwipeRight);

    if (Input.touchCount > 0)
    {
        foreach (Touch t in Input.touches)
        {
            Vector3 swipe = t.deltaPosition * t.deltaTime;

            if (swipe.y >  0.5f) evTouchEvent(enTouchType.SwipeUp   );
            if (swipe.y < -0.5f) evTouchEvent(enTouchType.SwipeDown );
            if (swipe.x >  0.5f) evTouchEvent(enTouchType.SwipeRight);
            if (swipe.x < -0.5f) evTouchEvent(enTouchType.SwipeLeft );
        }
    }
}
    }

csTempleRun.cs;

    void Update () 
    {   
        UpdatePlayer();

        if (m_player != null)
        {
            // Make the camera follow the player (if active)
            SmoothFollow sF = (SmoothFollow)Camera.mainCamera.GetComponent(typeof(SmoothFollow));
            sF.target = m_player.transform;         

            // Check for collisions with interactive objects
            UpdateCoins();
            UpdateMines();

            // Dynamically update the track
            CreateNewCellsIfNeeded(false);
        }       
    }


    private void UpdatePlayer(TouchDetector.enTouchType T)
{
    // if the player is dead (replaced with ragdoll) then exit since none of this code should fire.
    if (m_player == null) 
    {
        return;     
    }



    // Gradually increase the players' running speed, and update the animation to match.
    m_playerRunSpeed += Time.deltaTime * 0.005f;
    m_playerRunSpeed = Mathf.Clamp(m_playerRunSpeed, 0.5f, 3.0f);
    m_player.animation["run"].speed = m_playerRunSpeed * 2.0f;

    // ****************************************************************************************
    // INPUT

    // Player can only turn if they are not already sliding / jumping.  
    // Equally, sliding / jumping are mutually exclusive.

    if (Input.GetKeyDown (KeyCode.LeftArrow)&& m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
    }

    if (Input.GetKeyDown(KeyCode.RightArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.South;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.North;
    }   

    if (T==TouchDetector.enTouchType.SwipeDown && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        m_playerSlide = 1.0f;
        m_player.animation.Play("slide_fake");
    }            

    if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        AudioSource.PlayClipAtPoint(m_jumpAudio, m_player.transform.position);
        m_playerJump = 1.0f;
        m_playerYvel = 0.0f;
        m_player.animation.Play("jump");
    }

我希望通过发布此脚本我没有做错任何事,但我觉得我需要发布这个以获得我需要的帮助。你会注意到在csTempleRun.cs脚本中我用TouchDetector.enTouchType.SwipeDown替换了一个KeyCode调用。但我仍然遇到错误。提前感谢您的帮助。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

查看您的错误 - No overload for method UpdatePlayer' takes0' arguments。这意味着您需要为方法提供额外的数据。您需要提供智能感知的数据可以告诉您。

答案 1 :(得分:0)

在您的代码中:

void Update () 
{   
    UpdatePlayer(); 
    ...

您使用零参数调用UpdatePlayer()。但是,UpdatePlayer需要一个参数:

private void UpdatePlayer(TouchDetector.enTouchType T)
{

因此,当您调用UpdatePlayer()时,您需要发送类型为TouchDetector.enTouchType的对象作为参数。那将是以下之一:

  • SwipeLeft
  • SwipeRight
  • SwipeDown
  • SwipeUp