2DゲームをUIKitからCocos2dに移植しています。UIKitを使用したとき、5つの異なる正方形のフレームを設定するその一部は、次のようになりました。
- (void)setFrames {
[playerSquare setCenter:CGPointMake(160, 240)];
for(int iii = 0; iii < [squares count]; iii++) {
int randX = arc4random() % 321;
int randY = arc4random() % 481;
[(UIButton*)[squares objectAtIndex:iii] setFrame:CGRectMake(randX, randY, [(UIButton*)[squares objectAtIndex:iii] frame].size.width, [(UIButton*)[squares objectAtIndex:iii] frame].size.height)];
CGRect playerRect = CGRectMake(80, 160, 160, 160);
for(UIButton* b in squares) {
if((CGRectIntersectsRect(b.frame, [(UIButton*)[squares objectAtIndex:iii] frame]) && ![b isEqual:[squares objectAtIndex:iii]])) {
//make sure no two squares touch
iii--;
break;
} else if(CGRectIntersectsRect(playerRect, [(UIButton*)[squares objectAtIndex:iii] frame])) {
//no square is close to center of the screen
iii--;
break;
} else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(UIButton*)[squares objectAtIndex:iii] frame])) {
//no square is close to the edges of the screen
iii--;
break;
}
}
}
}
これまでのところ、私はcocos2dで同じことを達成しようとしました:
- (void)setFrames {
for(int idx = 0; idx < [squares count]; idx--) {
int randX = arc4random() % 321;
int randY = arc4random() % 481;
[(CCSprite*)[squares objectAtIndex:idx] setPosition:ccp(randX, randY)];
CGRect playerRect = CGRectMake(80, 160, 160, 160);
for(CCSprite* b in squares) {
if( (CGRectIntersectsRect(b.boundingBox, [(CCSprite*)[squares objectAtIndex:idx] boundingBox]) && ![b isEqual:[squares objectAtIndex:idx]]) ) {
//make sure no two squares touch
idx--;
break;
} else if(CGRectIntersectsRect(playerRect, [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
//no square is close to center of the screen
idx--;
break;
} else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
//no square is close to the edges of the screen
idx--;
break;
}
}
}
}
しかし、この変更された方法は機能しません。正方形の1つは適切な位置に配置されますが、他の4つは常にcocos2dポイント0、0でスポーンします。(画面の左下隅)誰かが私を正しい方向に向けることができますか?