IOS Dev (49) official SpriteKit Game template of Apple

Source: Internet
Author: User
Tags spritekit

IOS Dev (49) official SpriteKit Game template of Apple

  •  
  • 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.

    1. Obtain the touch point location first.
    2. Create a sprite using the spriteNodeWithImageNamed API.
    3. Set the sprite position.
    4. Create a SKAction and let sprite repeat the action.
    5. Finally, add this sprite to scene.

      Reprinted please indicate the blog from laruence: http://prevention.iteye.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.