Basic Architecture-AppDelegate-ViewController: Basic VC. -MyScene: animation scenarios, processing actions, and so on.
Instantiate a ViewController in AppDelegate and instantiate a MyScene in ViewController.
AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[ViewController alloc] init]; [self.window makeKeyAndVisible]; return YES;}
ViewController- (void)loadView{ self.view = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];}- (void)viewDidLoad{ [super viewDidLoad]; SKView * skView = (SKView *)self.view; SKScene * scene = [MyScene sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; [skView presentScene:scene];}
This is easy to understand. Initialize the view in loadView. Remember to do this either in init or viewDidLoad.
In viewDidLoad, first instantiate a MyScene and set this MyScene with scaleMode to SKSceneScaleModeAspectFill. At last, we will present the scene on The view.
The above steps are common practices.
MyScene-(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0]; SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@Chalkduster]; myLabel.text = @Hello, World!; myLabel.fontSize = 30; myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); [self addChild:myLabel]; } return self;}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called when a touch begins */ for (UITouch *touch in touches) { CGPoint location = [touch locationInNode:self]; SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@Spaceship]; sprite.position = location; SKAction *action = [SKAction rotateByAngle:M_PI duration:1]; [sprite runAction:[SKAction repeatActionForever:action]]; [self addChild:sprite]; NSLog(@for loop); } NSLog(@touchesBegan);}
Implement the touchesBegan method, which is inherited by MyScene from UIResponder and defined:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
The inheritance relationship is like this:
MyScene -> SKScene -> SKEffectNode -> SKNode -> UIResponder
Let's look back at touchesBegan.
- Obtain the touch point location first.
- Create a sprite using the spriteNodeWithImageNamed API.
- Set the sprite position.
- Create a SKAction and let sprite repeat the action.
- Finally, add this sprite to scene.
Reprinted please indicate the blog from laruence: http://prevention.iteye.com