cocos2d 掷骰子问题 急求一个dome事例最好了
请各位大神看下 纠结了好几天 骰子的随机数和面数不一样 不知道是哪里出问题了 急求 解答
代码如下
-(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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在是面数不对
rand()%6才是随机数0~5,+1才对