문제

I made an app to fool around with that is basically a flappy bird clone. I need to know how to make the sound when I touch the screen. I tried using a button but the sound would only play as a touched the button, not the whole screen. When I touched the button, the bird just fell. I know I probably have to use TouchBegan, but what codes do I use and where do I put them? The more detail the better because im a huge rookie in this stuff. Thanks!

도움이 되었습니까?

해결책

If you are using a UIView subclass, you can implement the following method to determine if a tap has occurred. From there, just play your sound. (Note, using ARC)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // now that we know that a touch has occurred, lets play a sound
    NSString *pathToMySound = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"aif"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID([NSURL fileURLWithPath: pathToMySound], &soundID);
    AudioServicesPlaySystemSound(soundID);
}

다른 팁

Try to add the UITapGestureRecogniser to the main view which would probably be self.view, or whatever you have named it.

make sure that user interaction is enabled, and create a selector that will play the sound every time it gets called.

this article might help you, I haven't tried it, but it looks like it going in the right direction. I would add the UIGesture programatically though.

--This is how you crate a UITapGesture

//this stuff goes  in your viewDidLoad for example
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSthCool:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap]; //self or wherever you want to add it

.....
.....

- (void) doSthCool: (UITapGestureRecognizer *)recognizer{
     //Code to handle the gesture
     NSLog(@"flap");
}

Also, when trying to use a URL don't us CFURLRef if you don't know what its doing, instead try something like this:

NSURL *path = [NSURL URLWithString:@"whatever the url/path is."];

I do suggest that you google some stuff when you get stuck, it's the best way to learn :)

good luck mate! and have fun coding :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top