《30天精通iPhone手机编程》-Day27-十字方向键

《吃豆人》是经典的老少皆宜的休闲类游戏,下面就是这个游戏的动作的代码片段

//播放音频文件
- (void)playSound:(NSString*)soundAct{
	//音频文件的路径名称
	NSString *path = [NSString stringWithFormat:@"%@%@",
					  [[NSBundle mainBundle] resourcePath],
					  soundAct];
	NSLog(@"%@\n", path);
	//声明一个系统声音标识符
	SystemSoundID soundID;
	//转换字符类型变量的路径文件为URL连接
	NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
	//使用AudioToolbox框架提供的创建声音服务,定义音频的路径和创建声音标识符
	AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
	//播放指定的音频标识符
	AudioServicesPlaySystemSound(soundID);
}

- (IBAction)onLeft:(id)sender {
	if(PacMan.center.x > 50){
		[UIImageView beginAnimations:nil context:PacMan];
		[UIImageView setAnimationDuration:0.5];
		PacMan.center=CGPointMake(PacMan.center.x-10, PacMan.center.y);
		[PacMan setImage:[UIImage imageNamed:@"PacMan_Left.png"]]; 
		[UIImageView commitAnimations];
	}
	soundFile = [NSString stringWithFormat:@"/Sound.wav"];
	[self playSound: soundFile];
}
- (IBAction)onRight:(id)sender {
	if(PacMan.center.x < 270){
		[UIImageView beginAnimations:nil context:PacMan];
		[UIImageView setAnimationDuration:0.5];
		PacMan.center=CGPointMake(PacMan.center.x+10, PacMan.center.y);
		[PacMan setImage:[UIImage imageNamed:@"pacMan_Right.png"]]; 
		[UIImageView commitAnimations];
	}
	soundFile = [NSString stringWithFormat:@"/Sound.wav"];
	[self playSound: soundFile];
}
- (IBAction)onUp:(id)sender {
	if(PacMan.center.y>50){
		[UIImageView beginAnimations:nil context:PacMan];
		[UIImageView setAnimationDuration:0.5];
		PacMan.center=CGPointMake(PacMan.center.x, PacMan.center.y-10);
		[PacMan setImage:[UIImage imageNamed:@"PacMan_Up.png"]]; 
		[UIImageView commitAnimations];
	}
	soundFile = [NSString stringWithFormat:@"/Sound.wav"];
    [self playSound: soundFile];
}
- (IBAction)onDown:(id)sender {
	if(PacMan.center.y < 270){
		[UIImageView beginAnimations:nil context:PacMan];
		[UIImageView setAnimationDuration:0.5];
		PacMan.center=CGPointMake(PacMan.center.x, PacMan.center.y+10);
		[PacMan setImage:[UIImage imageNamed:@"PacMan_Down.png"]]; 
		[UIImageView commitAnimations];
	}
	soundFile = [NSString stringWithFormat:@"/Sound.wav"];
	[self playSound: soundFile];
}


你可能感兴趣的:(游戏,编程,框架,iPhone,手机,Path)