对于这种情况,有没有办法让代码更紧凑?
// pseudocode
if (A == 1) {
if (B == 2) {
action1;
}
if (B == 3) {
action2;
}
}
if (B == 1) {
if (A == 2) {
action1;
}
if (A == 3) {
action2;
}
}
在Objective-C / Objective-C ++中?
又一个例子:
if (((int)fixtureUserDataA == FIXTURE_FOOT_SENSOR || ((int)fixtureUserDataB == FIXTURE_FOOT_SENSOR ))) {
// Hero foot sensors
if (bodyA->GetUserData() != NULL) {
CCSprite * sprite = (CCSprite*)bodyA->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
if (bodyB->GetUserData() != NULL) {
CCSprite * sprite = (CCSprite*)bodyB->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
}
是否可以只使用一种或多种透明结构?
答案 0 :(得分:3)
代表第二个例子:
checkBody(bodyA);
checkBody(bodyB);
void checkBody(Body* body)
{
CCSprite * sprite = (CCSprite*)body->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
答案 1 :(得分:-1)
if (A == 1 || B == 1)
if (A + B == 3)
action1;
else if (A + B == 4)
action2;