Objective c Obj-c不会用mod解析if语句
所以我用一个简单的if语句来提高物体在得分时的速度。我使用了一个非常简单的if语句,但它不起作用。在此上下文中,“pigSpeed”控制对象的速度Objective c Obj-c不会用mod解析if语句,objective-c,if-statement,modulo,Objective C,If Statement,Modulo,所以我用一个简单的if语句来提高物体在得分时的速度。我使用了一个非常简单的if语句,但它不起作用。在此上下文中,“pigSpeed”控制对象的速度 float difference = (self.view.bounds.size.height/4) - flyingPig.center.x; score += -(int)difference; if (score % 1000 == 0 & score > 0) { pigS
float difference = (self.view.bounds.size.height/4) - flyingPig.center.x;
score += -(int)difference;
if (score % 1000 == 0 & score > 0)
{
pigSpeed++;
NSLog(@"pigfaster");
}
我知道分数可以正常工作,就像它显示的那样,就像它显示的那样,但是if语句不起作用,什么也没有发生。我做错了什么。如果需要,我可以添加更多代码
&
是按位and运算符&&
是您要在此处使用的逻辑和。您的if语句实际上应该是:
if (score % 1000 == 0 && score > 0)
&
是按位and运算符&&
是您要在此处使用的逻辑和。您的if语句实际上应该是:
if (score % 1000 == 0 && score > 0)
我同意andyvn22-您应该使用&&而不是&。然而,还有一个问题 假设您的分数为999 在你们计算出差值后,它的差值=-5 你的分数将变为1004分。1004%1000将是4(不是零)
我认为你应该改变你的状态,即使你跳到1000米以上,你仍然可以提高速度 我同意andyvn22-您应该使用&&而不是&。然而,还有一个问题 假设您的分数为999 在你们计算出差值后,它的差值=-5 你的分数将变为1004分。1004%1000将是4(不是零)
我认为你应该改变你的状态,即使你跳到1000米以上,你仍然可以提高速度 关于@victor ronin所说的话
float difference = ( self.view.bounds.size.height / 4.0f ) - flyingPig.center.x;
int newScore = score + difference ;
if ( newScore > 0 && ( newScore / 1000 > score / 1000 ) )
{
// did thousands place increment?
++pigSpeed;
}
score = newScore ;
关于@victor ronin所说的话
float difference = ( self.view.bounds.size.height / 4.0f ) - flyingPig.center.x;
int newScore = score + difference ;
if ( newScore > 0 && ( newScore / 1000 > score / 1000 ) )
{
// did thousands place increment?
++pigSpeed;
}
score = newScore ;
我不得不给一点回旋余地,所以“如果(分数%1000==2&&score>999)”我不得不给一点回旋余地,所以“如果(分数%1000==2&&score>999)”你说的“不起作用”是什么意思?请更具体地描述该行为-“它在if语句上崩溃”,“if语句从来都不是真的”,等等,&&操作符没有更改任何内容。但我还是设法解决了这个问题,我不得不给自己留点余地,所以“如果(分数%1000==2&&score>999)”你说的“不起作用”是什么意思?请更具体地描述该行为-“它在if语句上崩溃”,“if语句从来都不是真的”,等等,&&操作符没有更改任何内容。但我还是设法解决了这个问题,我不得不给自己留点余地,所以“如果(分数%1000==2&&score>999)”