Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法更改使用类函数在main中创建的int值_C#_Visual Studio 2012 - Fatal编程技术网

C# 无法更改使用类函数在main中创建的int值

C# 无法更改使用类函数在main中创建的int值,c#,visual-studio-2012,C#,Visual Studio 2012,-我无法更改使用类函数在main中创建的int值- 我正在制作一个游戏,我有一个名为sideCollision的函数,该函数检查玩家是否正在触摸pictureBox,如果玩家正在触摸pictureBox,则名为Score的整数将递增1 以下是函数: public void sideCollision(Control player,Control pointRadius,Control topPipe,Control bottomPipe,Control gameOver,int force,bo

-我无法更改使用类函数在main中创建的int值-

我正在制作一个游戏,我有一个名为
sideCollision
的函数,该函数检查玩家是否正在触摸
pictureBox
,如果玩家正在触摸pictureBox,则名为
Score
的整数将递增1

以下是函数:

public void sideCollision(Control player,Control pointRadius,Control topPipe,Control bottomPipe,Control gameOver,int force,bool timer,int score,int pointRadiusCounter)
{
    if (player.Right > pointRadius.Left && player.Left < pointRadius.Right - player.Width / 2 && player.Bottom > pointRadius.Top)
    {
        if (timer == true && pointRadiusCounter == 0)
        {
            pointRadiusCounter = 1;
            score++;
        }
    }
}
public void sideCollision(控制玩家、控制点半径、控制顶部管道、控制底部管道、控制游戏结束、int-force、bool计时器、int-score、int-pointradiusconter)
{
if(player.Right>pointRadius.Left&&player.LeftpointRadius.Top)
{
if(timer==true&&pointRadiusCounter==0)
{
pointRadiusCounter=1;
分数++;
}
}
}
该函数检测球员触碰墙壁,但不会将分数增加1。我也有一条信息在我的主要说“分数尚未分配给,将始终有其默认值0”


我需要知道如何使用函数更改分数值,因为函数没有更改
score

int
的值是一种值类型。这意味着它是通过值传递的,并且在函数中有原始变量的副本。可以通过显式地通过引用传递来解决此问题:

public void sideCollision(Control player,Control pointRadius,Control topPipe,Control bottomPipe,Control gameOver,int force,bool timer, ref int score, ref int pointRadiusCounter)
{
    if (player.Right > pointRadius.Left && player.Left < pointRadius.Right - player.Width / 2 && player.Bottom > pointRadius.Top)
    {
        if (timer == true && pointRadiusCounter == 0)
        {
            pointRadiusCounter = 1;
            score++;
        }
    }
}
public void sideCollision(控制玩家、控制点半径、控制顶部管道、控制底部管道、控制gameOver、int-force、bool计时器、ref-int分数、ref-int点半径计数器)
{
if(player.Right>pointRadius.Left&&player.LeftpointRadius.Top)
{
if(timer==true&&pointRadiusCounter==0)
{
pointRadiusCounter=1;
分数++;
}
}
}

请原谅我有点离题,但这可能有助于您避免像您目前遇到的问题:

我会考虑考虑哪些变量需要作为一个变量来传递。 参数以及哪些变量也可以从所有 类,因为它们的值将是 在代码的任何点(例如玩家的生命点, 球员得分、球员位置)

然后您可以将它们转移到自己的类中(在本例中 “player”类),使这些变量成为该类的属性,以及 通过创建属性,使其他类可以访问这些属性
public static
Singleton类
,如果只有一个玩家 比赛

传递尽可能多的变量
侧面碰撞(…)
在执行项目时会造成很多麻烦 变得更大。安排变量、方法并确定优先级, 类等可以帮助您防止这种情况。所以慢慢来想想 关于什么是重要的,你在哪里需要它等等


在这样的情况下,不应该使用“按引用传递”。相反,在这种情况下,你可以使用这样的东西

private void SomeMethod()
{
    var score = 0;
    var pointRadiusCounter = 0;
    if (CollisionOccurred(...))
    {
        score++;
        pointRadiusCounter++;
    }
    ...
    ...
    ...
}

public bool CollisionOccurred(Control player,Control pointRadius,Control topPipe,Control bottomPipe,Control gameOver,int force,bool timer)
{
    if (player.Right > pointRadius.Left && player.Left < pointRadius.Right - player.Width / 2 && player.Bottom > pointRadius.Top)
    {
        if (timer == true && pointRadiusCounter == 0)
        {
            return true;
        }
    }
    return false;
}
private void SomeMethod()
{
var得分=0;
var pointRadiusCounter=0;
如果(发生碰撞(…))
{
分数++;
pointRadiusCounter++;
}
...
...
...
}
发生公共布尔冲突(控制玩家、控制点半径、控制顶部管道、控制底部管道、控制游戏结束、内力、布尔计时器)
{
if(player.Right>pointRadius.Left&&player.LeftpointRadius.Top)
{
if(timer==true&&pointRadiusCounter==0)
{
返回true;
}
}
返回false;
}

是的,
分数
是一个参数-一个局部变量。它是按值传递的。请阅读如何称呼这种方法?您可以将score作为引用传递进来,但是最好弄清楚您实际上在做什么。我很确定这会在调用代码中引入错误。但是,虽然我同意这会解决问题,但它可能会在OP未来的代码中引入坏习惯。嗨,当我使用ref关键字时,我在尝试调用函数时收到一个错误,它说“最好的重载匹配有一些无效的参数”别担心,我没有意识到在调用函数时也需要ref关键字,我认为只有在我的类中创建函数时才需要它。感谢您的帮助:)@ReubenWard请注意,Microsoft不建议使用此方法。您应该尽量减少
ref
用法的数量。例如,您可以返回bool,而不是向上计数
分数
,而是在从方法接收到
true
时向外计数。