IOS 2D Game Development Framework SpriteKit

Source: Internet
Author: User

Recently found that Xcode's own 2D game development Framework Spritekit can be directly introduced into the app for hybrid development, that is, can develop both business applications and games with the Apple app, how I feel this is a small discovery of their own .... Oh ..., checked the fact that someone else had done it ..... The discovery of this feature is of course very open, so I have two cases ready to learn. In the past spare time also learned a cocos2d-x such cross-platform game framework, also did a small case, so feel this framework is not troublesome, and simpler than cocos2d-x. And this framework I should not be like c2d-x to learn to throw away where no matter, because iOS development is my current work. Perhaps after I do the app will hide a few small games in the inside, such as the app has been reviewed and put out, what a pleasant idea.

Today is the first time to take notes, I directly create a Xcode spritekit small case, I understand this.

The game involves the use of the class is very few, mainly around the wizard, the scene two core class in the Turn, the wizard is the role of the game, the role of players, the game inside the monster role, scene is the background of the game, Spritekit inside these classes are inherited and Sknode. Here's one you can look at

If you put the above several categories to make a test, do a small game should be no trouble for you, if you want to do a complex game, it is certainly not, the framework now seems to be able to write OpenGL to do character rendering, it is complex, complexity is not less than C language.

Below I paste the code, I in originally the official own Case Foundation, added the texture class use, the scene background setting, the node layer's setting

First, texture

The texture is to divide the entire action into several pictures, then put it in an array, and then you will need to use this

The texture sprite adds a behavior that adds the above texture array to the behavior, and then a coherent action comes out.

In fact, in iOS development, you can also bind a view to an animation composed of several images.

I don't have a picture of a whole set of behaviors here, just to implement this function.

Skscene: The code that executes when the scene is initialized implements this function.

-(Instancetype) Initwithsize: (cgsize) size{if (self = [Super Initwithsize:size]) {/* First create the first sprite node, set the sprite node background picture , here set the sprite node picture way a lot, I also use the texture here, you can also directly use. BackgroundImage, it's the same. *//Skspritenode * Firstsknodex = [Skspritenode Spriteno        dewithimagenamed:<# (nonnull NSString *) #>];       UIImage *backgourndtextureimge=[uiimage imagenamed:@ "Planeback"];         Sktexture *backgourndtexture = [Sktexture texturewithimage:backgourndtextureimge];        Skspritenode * Firstsknode = [Skspritenode spritenodewithtexture:backgourndtexture size:self.size];        Firstsknode.position = Cgpointmake (Cgrectgetmidx (Self.frame), Cgrectgetmidy (Self.frame));        Firstsknode.name = @ "Backgournd"; Firstsknode.zposition = 0;/* Set the position of the wizard to 0 layer, 0 = the lowest level, this property = the same as the HTML zindex, the layer in the sprite must remember the settings, otherwise initialized or after an action you do not see the possibility of */[self add                Child:firstsknode]; /* Below is the behavior animation texture bearimages.atlas= represents a folder, remember to Add. Atlas, and then put the entire set of behavioral segmentation images in the */Sktextureatlas *atl as = [SKTExtureatlas atlasnamed:@ "Bearimages.atlas"];        Sktexture *f1 = [Atlas texturenamed:@ "Planeback"];        Sktexture *f2 = [Atlas texturenamed:@ "PlaneBack1"];         Nsarray *monsterwalktextures = @[f1,f2]; /* Below is a loop execution action, the effect here is that the genie keeps switching monsterwalktextures inside the texture picture */skaction *walkanimation = [Skaction animatewithtext                Ures:monsterwalktextures timeperframe:0.1];    [Firstsknode runaction:[skaction repeatactionforever:walkanimation]; } return self;}

-(void) Didmovetoview: (Skview *) View {     }/* below this is the official original method, you create a case by Xcode, you know, click on the increase of a plane */-(void) Touchesbegan :(Nsset *) touches withevent: (Uievent *) Event {/    * Called when a touch begins *        /for (Uitouch *touch in touches) { C3/>cgpoint location = [Touch locationinnode:self];                Skspritenode *sprite = [Skspritenode spritenodewithimagenamed:@ "Spaceship"];        sprite.zposition=1;//layer in scene        Sprite.xscale = 0.15;//Sprite size        sprite.yscale = 0.15;//elf size        sprite.position = location;//elf location        //per second turn     skaction *action = [skaction rotatebyangle:m_pi duration:1];        Repetitive behavior        [sprite runaction:[skaction repeatactionforever:action]];                [Self addchild:sprite];}    } -(void) Update: (cftimeinterval) currenttime {/    * called before each frame is rendered */   }

There are minor changes in the calling place.

-(void) viewdidload{    [Super Viewdidload];    Configure the view.    Skview * Skview = (Skview *) Self.view;    Skview.showsfps = YES;    Skview.showsnodecount = YES;    /* Sprite Kit applies additional optimizations to improve rendering performance */    Skview.ignoressiblingorder = yes;
   gamescene *scene = [[Gamescene alloc]initwithsize:skview.bounds.size];        Scene.scalemode = Skscenescalemodeaspectfill;        [Skview Presentscene:scene];}

IOS 2D Game Development Framework SpriteKit

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.