cocos2d 掷骰子问题 急求一个dome事例最好了

赵小璞 发布于 2014/10/06 19:22
阅读 1K+
收藏 0
请各位大神看下 纠结了好几天 骰子的随机数和面数不一样 不知道是哪里出问题了 急求 解答
代码如下

-(void)shaizi{
CGSize winSize = [CCDirector sharedDirector].winSize;

[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@\"dice.plist\"];

CCSpriteBatchNode * spriteSheet1 = [CCSpriteBatchNode batchNodeWithFile:@\"dice.png\"];
[self addChild:spriteSheet1];

_rotate = [NSMutableArray array];
for (int i = 1; i <= 6; i ++)
{
[_rotate addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@\"%d.png\",i]]];
}
CCAnimation *animation= [CCAnimation animationWithSpriteFrames:_rotate delay:0.1f];

self.shazi = [CCSprite spriteWithSpriteFrameName:@\"1.png\"];
_shazi.position = ccp(winSize.width/2, winSize.height/2);

self.rotateAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:animation] times:3];

[self.shazi runAction:_rotateAction];
[spriteSheet1 addChild:_shazi];
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@\"触摸开始时!\");
[_shazi stopAllActions];
srand((unsigned)time(0)); //每次产生的随机数不变
//骰子结果
int result1 = (rand() % 5) +1 ; //产生1~6的
switch (result1) {
case 1:_shazi = [CCSprite spriteWithFile:@\"1.png\"];
faceNum = 1;
break;
case 2:_shazi = [CCSprite spriteWithFile:@\"2.png\"];
faceNum = 2;
break;
case 3:_shazi = [CCSprite spriteWithFile:@\"3.png\"];
faceNum = 3;
break;
case 4:_shazi = [CCSprite spriteWithFile:@\"4.png\"];
faceNum = 4;
break;
case 5:_shazi = [CCSprite spriteWithFile:@\"5.png\"];
faceNum = 5;
break;
case 6:_shazi = [CCSprite spriteWithFile:@\"6.png\"];
faceNum = 6;
break;
}
NSLog(@\"%d\",result1);
}
采集
加载中
0
realanan
realanan
rand()%6才是随机数0~5,+1才对
赵小璞
赵小璞
现在是面数不对
返回顶部
顶部