2010-03-25 32 views
0

我试图在touchesEnded上播放声音,但我遇到问题。有多个对象可以移动,所以如果下面的代码在任何对象移动时都成立,它会不断播放声音。我如何只玩一次?touchesEnded声音?

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    if(CGRectContainsRect([image1 frame], [image2 frame])){ 

     [self playsound]; 
    } 
} 

回答

1

如果你只希望它一定对象调用touchesEnded玩,你首先需要确定目标,然后你可以做一个快速的if-then语句。

如果你只想让它播放一次,那么只需给它一个像int playCount = 0;这样的快速变量,然后在播放完成后将其设置为playCount = 1;,然后对其执行if-then语句(即播放if playCount为0,如果playCount为1,则不播放)。