参考SpriteKit 创建游戏的教程今天自己动手做了一下,现在记录一下自己怎么做的,今天之做了第一步,一共有三个部分。

第一步,项目搭建。

项目所用图片资源:点击打开链接

1.在Xcode打开之后,选择File
Menu > New > Project,然后你可能会看到下面的示意图所显示的内容:



随便起个名字,我就叫它:2014airplane了。

2.创建成功后,点击运行如果模板运行成功后接着来。

3.复制这些图片到你项目中的指定目录并且要确保你的“Copy
Items into destination group's folder(如果需要)”被选上。

4.代码修改如下:

MyScene.h中的代码:

//
// MyScene.h
// 2014airplane
// // Copyright (c) 2014年 com.wildcat. All rights reserved.
// #import <SpriteKit/SpriteKit.h>
#import <CoreMotion/CoreMotion.h> @interface MyScene : SKScene<UIAccelerometerDelegate>{
CGRect screenRect;
CGFloat screenHeight;
CGFloat screenWidth;
double currentMaxAccelX;
double currentMaxAccelY; }
//声明变量
//声明运动管理器
@property (strong,nonatomic)CMMotionManager *motionManager;
@property SKSpriteNode *plane; //飞机
@property SKSpriteNode *planeShadow; //飞机倒影
@property SKSpriteNode *propeller; //螺旋桨 @end

MyScene.m中的代码:

//
// MyScene.m
// 2014airplane
//
// Created by wildcat on 14-2-13.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
// #import "MyScene.h" @implementation MyScene -(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
//设置场景大小
screenRect=[[UIScreen mainScreen] bounds];
screenHeight=screenRect.size.height;
screenWidth=screenRect.size.width;
//设置飞机图片
_plane=[SKSpriteNode spriteNodeWithImageNamed:@"PLANE 8 N.png"];
_plane.scale=0.6; //缩放比例
_plane.zPosition=2; //纵坐标
_plane.position=CGPointMake(screenWidth/2, 15+_plane.size.height/2); //设置飞机的初始位置
[self addChild:_plane];
//添加背景
SKSpriteNode *backgroud=[SKSpriteNode spriteNodeWithImageNamed:@"airPlanesBackground.png"];
backgroud.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:backgroud];
//添加飞机背景
_planeShadow=[SKSpriteNode spriteNodeWithImageNamed:@"PLANE 8 SHADOW.png"];
_planeShadow.scale=0.6;
_planeShadow.zPosition=1;
_planeShadow.position=CGPointMake(screenWidth/2+15, _planeShadow.size.height/2);
[self addChild:_planeShadow];
//添加螺旋桨
_propeller=[SKSpriteNode spriteNodeWithImageNamed:@"PLANE PROPELLER 1.png"];
_propeller.scale=0.2;
_propeller.position=CGPointMake(screenWidth/2, _plane.size.height+10);
SKTexture *propeller1=[SKTexture textureWithImageNamed:@"PLANE PROPELLER 1.png"];
SKTexture *propeller2=[SKTexture textureWithImageNamed:@"PLANE PROPELLER 2.png"];
SKAction *spin=[SKAction animateWithTextures:@[propeller1,propeller2] timePerFrame:0.1];
SKAction *spinForever=[SKAction repeatActionForever:spin];
[_propeller runAction:spinForever];
[self addChild:_propeller];
//设置运动管理器
self.motionManager=[[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval=0.2;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self outputAccelertionData:accelerometerData.acceleration];
if (error) {
NSLog(@"%@",error);
}
}]; }
return self;
}
-(void)outputAccelertionData:(CMAcceleration)acceleration{
currentMaxAccelX=0;
currentMaxAccelY=0;
if (fabs(acceleration.x>fabs(currentMaxAccelX))) {
currentMaxAccelX=acceleration.x;
}
if (fabs(acceleration.y>fabs(currentMaxAccelY))) {
currentMaxAccelY=acceleration.y;
} }
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */ } //Update现在主要处理两件事情:更新你的位置和交换不同的sprite
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
//NSLog(@"one second");
float maxY = screenWidth - _plane.size.width/2;
float minY = _plane.size.width/2;
float maxX = screenHeight - _plane.size.height/2;
float minX = _plane.size.height/2;
float newY = 0;
float newX = 0;
if(currentMaxAccelX > 0.05){
newX = currentMaxAccelX * 10;
_plane.texture = [SKTexture textureWithImageNamed:@"PLANE 8 R.png"];
}
else if(currentMaxAccelX < -0.05){
newX = currentMaxAccelX*10;
_plane.texture = [SKTexture textureWithImageNamed:@"PLANE 8 L.png"];
}
else{
newX = currentMaxAccelX*10;
_plane.texture = [SKTexture textureWithImageNamed:@"PLANE 8 N.png"];
}
newY = 6.0 + currentMaxAccelY *10;
float newXshadow = newX+_planeShadow.position.x;
float newYshadow = newY+_planeShadow.position.y;
newXshadow = MIN(MAX(newXshadow,minY+15),maxY+15);
newYshadow = MIN(MAX(newYshadow,minX-15),maxX-15);
float newXpropeller = newX+_propeller.position.x;
float newYpropeller = newY+_propeller.position.y;
newXpropeller = MIN(MAX(newXpropeller,minY),maxY);
newYpropeller = MIN(MAX(newYpropeller,minX+(_plane.size.height/2)-5),maxX+(_plane.size.height/2)-5);
newX = MIN(MAX(newX+_plane.position.x,minY),maxY);
newY = MIN(MAX(newY+_plane.position.y,minX),maxX);
_plane.position = CGPointMake(newX, newY);
_planeShadow.position = CGPointMake(newXshadow, newYshadow);
_propeller.position = CGPointMake(newXpropeller, newYpropeller); } @end

运行结果如下:

转载请说明:本文转自http://blog.csdn.net/wildcatlele

IOS学习之路五(SpriteKit 开发飞机大战小游戏一)的更多相关文章

  1. canvas绘制“飞机大战”小游戏,真香!

    canvas是ArkUI开发框架里的画布组件,常用于自定义绘制图形.因为其轻量.灵活.高效等优点,被广泛应用于UI界面开发中. 本期,我们将为大家介绍canvas组件的使用. 一.canvas介绍 1 ...

  2. docker项目——搭建飞机大战小游戏

    项目2:搭建打飞机小游戏,验证数据持久化(最底下有链接) 第一步:拉取镜像 [root@localhost docker-image]# docker load < httpd_img.tar. ...

  3. 用面向对象的编程方式实现飞机大战小游戏,java版

    概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...

  4. IOS学习之路五(代码实现UITableView)

    先展示一下运行结果: 代码实现: 1.先创建一个空项目: 2.创建一个Controller:(TableViewController) 在AppDelegate.h中声明属性: //  AppDele ...

  5. IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路

    在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...

  6. Python之游戏开发-飞机大战

    Python之游戏开发-飞机大战 想要代码文件,可以加我微信:nickchen121 #!/usr/bin/env python # coding: utf-8 import pygame impor ...

  7. 带你使用h5开发移动端小游戏

    带你使用h5开发移动端小游戏 在JY1.x版本中,你要做一个pc端的小游戏,会非常的简单,包括说,你要在低版本的浏览器IE8中,也不会出现明显的卡顿现象,你只需要关心游戏的逻辑就行了,比较适合逻辑较为 ...

  8. 基于FPGA的飞机的小游戏

    基于FPGA的飞机的小游戏 实验原理 该实验主要分为4个模块,采用至上而下的设计方法进行设计.由50M的晶振电路提供时钟源,VGA显示控制模块.图形显示控制模块.移动模块的时钟为25M,由时钟分频电路 ...

  9. 纪录我的iOS学习之路

    学习资料的网址 田伟宇(Casa Taloyum)有几篇介绍iOS架构的文章,一级棒!原博客链接. iOS应用架构谈 开篇 iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 网络层设计方 ...

随机推荐

  1. HDU 4932 Miaomiao&#39;s Geometry(推理)

    HDU 4932 Miaomiao's Geometry pid=4932" target="_blank" style="">题目链接 题意: ...

  2. 浅谈javascript中的call()和apply()方法

    话说在js中,每个函数都包含两个非继承而来的放方法,apply()和call(),使得我们能在特定的作用域中调用函数. 官方定义: 语法:       fun.call(thisArg[, arg1[ ...

  3. MIFARE系列6《射频卡与读写器的通信》

    1. ATR(Answer to request) 读写器呼叫磁场内的卡片.卡片对呼叫做出应答. 对刚进入磁场得到电复位处于休闲状态的卡片,卡请求(REQA,0x26):对于已进行过读写操作并进入休眠 ...

  4. linux_创建用户_copy远程文件_解压缩_执行

    查看历史命令: history 创建用户: sudo useradd -m -s /bin/bash cph  [会自动创建cph文件夹,并将才cph文件夹权限和组设为cph] 创建用户密码: pas ...

  5. uva10718 - Bit Mask(贪心)

    题目:uva10718 - Bit Mask(贪心) 题目大意:给出32位无符号的整数n ,给定边界L和R,要求在这个边界里面找出一个整数,它和N做或运算得到的值最大. 解题思路:要求做或运算得到的值 ...

  6. vi使用高级

    下面的命令在vi命令模式被激活 h 光标左移一个字符 l 光标向右移动一个字符 j 光标下移一行 k 移动光标线 一.对整行操作 1.复制光标所在行 yy 2.删除光标所在行 dd 3.选中光标所在行 ...

  7. AWK增强的文本处理shell特征--AWK完全手册

    AWK这是一个很好的文字处理工具. 它不仅 Linux 中也是不论什么环境中现有的功能最强大的数据处理引擎之中的一个. 本文主要摘录池中龙写的Unixawk使用手冊(第二版),对当中内容略微修改.感谢 ...

  8. jQuery 添加 删除 改动select option

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  9. Java实现缓存(类似于Redis)

    Java实现缓存,类似于Redis的实现,可以缓存对象到内存中,提高访问效率.代码如下: import java.util.ArrayList; import java.util.HashMap; i ...

  10. 用bat启动sqlserver服务

    声明下这个脚本不是我写的,忘了是从哪看到的了,在此分享给大家,因为在我的理解中技术就是用来分享的,,希望原创作者看到了不要介意. 1.创建个文本,将后缀名改成.bat 2.将下边语句粘贴进去,然后保存 ...