參考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 #import @interface MyScene : SKScene{ 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
運行結果如下:
轉載請說明:本文轉自https://blog.csdn.net/wildcatlele