数组值分配开关为所有变量返回相同的值

时间:2016-02-12 11:53:59

标签: c# arrays unity3d unity5

我是一个业余爱好者GameDev在我的第一场比赛中工作,这是我在这个网站上提出的第一个问题。

除了这里的介绍我的问题。我的游戏专为触摸屏而设计,玩家将手指拖过屏幕并绘制与游戏互动的斜坡。

当我尝试读取这些斜坡中的值时出现问题。

每次玩家拖动手指时,都会传递一个包含当前动作最重要值(max,min,start,end等)的数组。该数组由读取和处理此数据的解释器类接收。

我在解释器类中遇到方法问题,此方法读取播放器拖动传递的数组并将值分配给一系列数组。我们的想法是对不同的" ramps"所以我知道女巫信息对应于每个" ramp"。此方法使用基于int的开关,该开关跟踪当前图形在完整图形列表中的位置。

转换似乎运行良好,我已尝试将计数器值用于测试跟踪器是否工作正常并且一切正常。但是,当我测试我的代码时,似乎每次绘制新点时都会为所有不同的数组分配相同的值。我错过了什么?

无论如何,对于长文本感到抱歉,这是我的代码:

Vector3[] PreviousRampDetails;
Vector3[] CurrentRampDetails;
Vector3[] NextRampDetails;
Vector3[] NextNextRampDetails;    

int ProcessedRamp;

GameObject PreviousRamp;
GameObject CurrentRamp;
GameObject NextRamp;
GameObject NextNextRamp;

//This method is called every time a new touch sequence begins
//The argument RampCreated corresponds with the object that is being generated along this sequence
void RampIdentifier(GameObject RampCreated)
{
    if (RampCreated == CurrentRamp)
    {
        ProcessedRamp = 0;
    }
    else if (ProcessedRamp == 0)
    {
        NextRamp = RampCreated;
        ProcessedRamp += 1;
    }
    else if (ProcessedRamp == 1 && NextRamp != RampCreated)
    {
        NextNextRamp = RampCreated;
        ProcessedRamp += 1;
    }
    else if (ProcessedRamp == 2 && NextNextRamp != RampCreated)
    {
        if (CurrentRamp != null)
        {
            NextNextRamp = RampCreated;
            CurrentRamp = null;
        }
        else
        {
            PreviousRamp = NextRamp;
            NextRamp = NextNextRamp;
            NextNextRamp = RampCreated;
        }
    }
}

//This method is called every time the player collides with a ramp.
//The argument ramp corresponds to the Object the player collides with.
void OnContactWithRamp(GameObject Ramp)
{
    CurrentRamp = Ramp;
    if (Ramp == NextRamp)
    {
        CurrentRampDetails = NextRampDetails;
        NextRamp = NextNextRamp;
        NextRampDetails = NextNextRampDetails;
        NextNextRamp = null;
        ProcessedRamp -= 1;
    }
    else if (Ramp == NextNextRamp)
    {
        CurrentRampDetails = NextNextRampDetails;
        NextRamp = PreviousRamp;
        PreviousRampDetails = NextRampDetails;
        NextNextRamp = null;
        ProcessedRamp = 0;
    }
    else if (Ramp == PreviousRamp)
    {
        CurrentRampDetails = PreviousRampDetails;
    }
}

这似乎是给我带来麻烦的一点。我已经尝试隔离变量,以便仅在此处指定它们的值,但问题仍然存在:

//This method is called every time the player drags his finger through the screen
//The argument RampDetails contains the important information corresponding to the shape of the ramp that's being generated
void RampTracker(Vector3[] RampDetails) 
{
    switch (ProcessedRamp)
    {
        case 0:
            CurrentRampDetails = RampDetails;
            break;
        case 1:
            NextRampDetails = RampDetails;
            break;
        case 2:
            NextNextRampDetails = RampDetails;
            break;
    }
}

我现在已经反对这两天了,我无法找到答案。非常感谢你提前!

好的,所以我已经尝试过处理这个问题,正如Joe Blow所说,经过一些测试后我发现错误就在这段代码中。问题是,即使发送了所有错误消息("错误发生在某处"在值分配器方法上),值仍然被改变为全部相同。这是设置此值的脚本的唯一部分。

void RampValueAssigner(Vector3[] nprRDetails, Vector3[] ncRDetails, Vector3[] nnRDetails, Vector3[] nnnRDetails, int pR)
{
    if (pR == 1)
    {
        if (ncRDetails == nnRDetails || nnRDetails == nnnRDetails)
        {
            Debug.Log("an error has happened somewhere");
            return;
        }
        Debug.Log("No errors registered on PR 1");
        nextRampDetails = nnRDetails;
    }
    if (pR == 2)
    {
        if (ncRDetails == nnnRDetails || nnRDetails == nnnRDetails)
        {
            Debug.Log("an error has happened somewhere");
            return;
        }
        Debug.Log("No errors registered on PR 2");
        nextNextRampDetails = nnnRDetails;
    }
}

//Esta es la autentica ramp tracker
void RampTracker(Vector3[] rampDetails, int pRamp)
{
    Vector3[] a_previousRampDetails = previousRampDetails;
    Vector3[] a_currentRampDetails = currentRampDetails;
    Vector3[] a_nextRampDetails = nextRampDetails;
    Vector3[] a_nextNextRampDetails = nextNextRampDetails;

    if (pRamp == 0)
    {
        RampValueAssigner(a_previousRampDetails, rampDetails, a_nextRampDetails, a_nextNextRampDetails,0);
        return;
    } 
    if (pRamp == 1)
    {
        RampValueAssigner(a_previousRampDetails, a_currentRampDetails, rampDetails, a_nextNextRampDetails,1);
        return;
    }
    if (pRamp == 2)
    {
        RampValueAssigner(a_previousRampDetails, a_currentRampDetails, a_nextRampDetails, rampDetails,2);
        return;
    }

}

1 个答案:

答案 0 :(得分:-1)

我最终修改了跟踪器的工作方式,现在每当玩家抬起他的手指从屏幕上时,processedRamp就会被修改为+1,这样我确保其值始终保持不变整个触摸程序。

另一方面,我最终通过这种方式分配数组的值来修复错误:

void RampValueAssigner(Vector3[] nprRDetails, Vector3[] ncRDetails, Vector3[] nnRDetails, Vector3[] nnnRDetails, int pR)
{
    if (pR == 0)
    {
        if (ncRDetails == nnRDetails || ncRDetails == nnnRDetails)
        {
            return;
        }
        currentRampDetails = new Vector3[8] { ncRDetails[0], ncRDetails[1], ncRDetails[2], ncRDetails[3], ncRDetails[4], ncRDetails[5], ncRDetails[6], ncRDetails[7] };

    }
    if (pR == 1)
    {
        if (ncRDetails == nnRDetails || nnRDetails == nnnRDetails)
        {
            return;
        }
        nextRampDetails = new Vector3[8] { nnRDetails[0], nnRDetails[1], nnRDetails[2], nnRDetails[3], nnRDetails[4], nnRDetails[5], nnRDetails[6], nnRDetails[7] }; ;
    }
    if (pR >= 2)
    {
        if (ncRDetails == nnnRDetails || nnRDetails == nnnRDetails)
        {
            return;
        }
        nextNextRampDetails = new Vector3[8] { nnnRDetails[0], nnnRDetails[1], nnnRDetails[2], nnnRDetails[3], nnnRDetails[4], nnnRDetails[5], nnnRDetails[6], nnnRDetails[7] }; ;
    }
}

老实说,我不明白为什么这样做以及为什么我以前使用的方法(array = newarray)没有。我很高兴我解决了我的问题,但如果有人能解释我是怎么做的,我会很感激。

非常感谢!