1

所以最初我要制作的应用程序在向上滑动时只发出一种声音。代码是

[[SimpleAudioEngine sharedEngine] playEffect:@"Swoosh.mp3"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:@selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;

我决定我希望在每次滑动时随机播放三种声音,而不仅仅是一种。所以这就是我所做的。

NSString *Soundname;

int randSound = arc4random() % 3;
switch (randSound) {
    case 0:
        Soundname = @"Swoosh1.mp3";
        break;
    case 1:
        Soundname = @"Swoosh2.mp3";
        break;
    case 2:
        Soundname = @"Swoosh3.mp3";
        break;

}
[[SimpleAudioEngine sharedEngine] playEffect:@"Soundname"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:@selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;

我制作了字符串,然后替换了 playEffect:@"Swoosh.mp3"]; with playEffect:@"Soundname"];

我尝试了 Soundname 的多种变体,但在我看来都没有。我对编码很陌生,我知道这是我所缺少的愚蠢的东西。有人有什么想法吗?

4

1 回答 1

0

我很确定问题是因为您仍在为 playEffect 参数使用文字字符串,但您的意图是使用新定义的字符串变量。

尝试改变:

[[SimpleAudioEngine sharedEngine] playEffect:@"Soundname"];

对此(注意缺少引号):

[[SimpleAudioEngine sharedEngine] playEffect:Soundname];
于 2015-03-21T05:52:40.980 回答