一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>
//加入头文件
#import <AudioToolbox/AudioToolbox.h> @interface RootViewController : UIViewController
{
NSString *soundFile;
} @end

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. //初始化界面
[self addView];
}
#pragma -mark -fucntions
-(void)addView
{
NSArray *titleArray=[[NSArray alloc]initWithObjects:@"DO",@"RE",@"MI",@"FA",@"SO",@"LA",@"SI",@"C",@"D",@"E",@"F",@"G", nil]; for (int i=0; i<12; i++) {
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 80+30*i, 70, 20)];
[button setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doClickAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
button.backgroundColor=[UIColor redColor];
[self.view addSubview:button];
} }
-(void)playSound:(NSString*)soundKey{ NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],soundKey];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID); }
#pragma -makr -doClickActions
-(void)doClickAction:(UIButton *)btn
{
NSLog(@"---doClickActions--");
NSLog(@"--btn.tag---%ld",btn.tag); if (btn.tag==0) {
soundFile = [NSString stringWithFormat:@"/001.mp3"];
[self playSound: soundFile]; }else if (btn.tag==1){
soundFile = [NSString stringWithFormat:@"/002.mp3"];
[self playSound: soundFile];
}else if (btn.tag==2){
soundFile = [NSString stringWithFormat:@"/003.mp3"];
[self playSound: soundFile];
}else if (btn.tag==3){
soundFile = [NSString stringWithFormat:@"/004.mp3"];
[self playSound: soundFile];
}else if (btn.tag==4){
soundFile = [NSString stringWithFormat:@"/005.mp3"];
[self playSound: soundFile];
}else if (btn.tag==5){
soundFile = [NSString stringWithFormat:@"/006.mp3"];
[self playSound: soundFile];
}else if (btn.tag==6){
soundFile = [NSString stringWithFormat:@"/007.mp3"];
[self playSound: soundFile];
}else if (btn.tag==7){
soundFile = [NSString stringWithFormat:@"/C.mp3"];
[self playSound: soundFile];
}else if (btn.tag==8){
soundFile = [NSString stringWithFormat:@"/D.mp3"];
[self playSound: soundFile]; }else if (btn.tag==9){
soundFile = [NSString stringWithFormat:@"/E.mp3"];
[self playSound: soundFile]; }else if (btn.tag==10){
soundFile = [NSString stringWithFormat:@"/F.mp3"];
[self playSound: soundFile]; }else if (btn.tag==11){
soundFile = [NSString stringWithFormat:@"/G.mp3"];
[self playSound: soundFile]; }
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
 
 

【代码笔记】iOS-钢琴小游戏的更多相关文章

  1. 常用Java API之Ramdom--用代码模拟猜数小游戏

    常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...

  2. Html5游戏开发-145行代码完成一个RPG小Demo

    lufy前辈写过<[代码艺术]17行代码的贪吃蛇小游戏>一文,忽悠了不少求知的兄弟进去阅读,阅读量当然是相当的大.今天我不仿也搞一个这样的教程,目地不在于忽悠人,而在于帮助他人. 先看de ...

  3. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  4. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  5. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  6. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  7. 小游戏——js+h5[canvas]+cs3制作【五子棋】小游戏

    五子棋小游戏学习—— 有一个问题是,棋盘线的颜色,在canvas中,明明设置了灰色,但在我的预览中还是黑色的,很重的颜色. 以下是复刻的源码: <!DOCTYPE html> <ht ...

  8. 3行代码,为QQ轻游戏加上语音互动能力

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 游戏和社交往往有着密不可分的关系,QQ轻游戏就是一款集成在手Q里面的游戏平台,直接通过手Q入口就能随开 ...

  9. iOS开发实战-基于SpriteKit的FlappyBird小游戏

    写在前面 最近一直在忙自己的维P恩的事情 公司项目也是一团乱 于是...随手找了个游戏项目改了改就上线了,就当充数了. SpriteKit简介 SpriteKit是iOS 7之后苹果推出的2D游戏框架 ...

  10. iOS 基础入门--Bull' Eye 小游戏 

      说明   Bull's Eye小游戏是http://www.raywenderlich.com/store/ios-apprentice里非常酷的入门demo 跟着该教程一步步做下来便有了 ...

随机推荐

  1. C语言学习020:可变参数函数

    顾名思义,可变参数函数就是参数数量可变的函数,即函数的参数数量是不确定的,比如方法getnumbertotal()我们即可以传递一个参数,也可以传递5个.6个参数 #include <stdio ...

  2. 国产达梦数据库的结合Enterprise Library的应用开发

    在上篇<基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作>介绍了如何在Enterprise Library的数据访问层上支持这种神秘的国产数 ...

  3. 优秀ASP.NET程序员修炼之路

    初级的程序员或经验不足的程序员往往只意识到自己的程序是写给计算机的,而不会在意程序其实也是写给人的,或在意得不够.不全面. 写给机器的程序,往往追求的是运行正确.执行效率能满足要求.但程序员的任务仅仅 ...

  4. 【Java每日一题】20161206

    package Dec2016; public class Ques1206 { public static void main(String[] args){ doSex(null); } publ ...

  5. java一点小的知识点

    1.在dos底下编译运行java文件时,若有包名,需要建立包名对应的文件夹,然后编译的时候写出包名:java.mypack.PackageDemo 2.代码中有包名的时候,自动创建文件夹并编译的方法: ...

  6. android 密码输入通过复选框实现明文密文显示

    editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//将文本框的内容以明文显示 edit ...

  7. 用xutils3.0进行下载

    写的例子比较简单,是用xutils3.0来进行下载项目更新 1.先通过网络请求,判断版本是否要更新 2.若要更新,则弹出一个弹窗,我用的是系统自带的Dialog,将下载的版本号及下载的内容提示展示出来 ...

  8. 去掉Mybatis Generator生成的一堆 example

    <table tableName="%" enableInsert="true" enableDeleteByPrimaryKey="true& ...

  9. VS2010下安装Cocos2dx完整教程(原)

    一.本人所使用的Cocos2dx版本(cocos2d-2.1rc0-x-2.1.3),下载地址:http://code.google.com/p/cocos2d-x/downloads/list 当前 ...

  10. Light OJ 1031---Easy Game(区间DP)

    题目链接 http://lightoj.com/volume_showproblem.php?problem=1031 Description You are playing a two player ...