IOS_31_COCOS2D Environment Construction

Source: Internet
Author: User
Tags addchild

As a result:




1. Download the cocos2d package from Git, about 100M


2. After decompression, go to the cocos2d home directory, copy the path to the terminal

3. Execute ./install.sh start installation (essentially copy to Xcode directory)






Before installing Xcode, the interface for creating a new project is as follows



After installing Xcode, the interface for creating a new project is as follows




The structure of the construction directory is as follows




Run the program directly, the effect is as follows:



Application Agent



  appdelegate.h//  31_cocos2d Primer////  Created by Beyond on 14-9-5.//  Copyright Com.beyond 2014. All rights reserved.//  appdelegate inherit from ccappdelegate/*    ccappdelegate inherit from Nsobect, comply with protocol:< Uiapplicationdelegate, ccdirectordelegate>    Ccappdelegate has members: UIWindow *window_  *  most cocos2d applications The ccappdelegate should be rewritten, ccappdelegate as the application's program entry.    At a minimum, the Startscene method should be replicated in order to return to the first scene that the app will show    if you want to further customize the cocos2d (such as customizing the display's pixel mode),     make a carbon copy Applicaton: Didfinishlaunchingwithoptions: Method */#import "cocos2d.h" @interface appdelegate:ccappdelegate@end


appdelegate.m//31_cocos2d Primer////Created by Beyond on 14-9-5.//Copyright Com.beyond 2014. All rights reserved.//appdelegate inherit from ccappdelegate/* ccappdelegate inherit from Nsobect, comply with protocol: <uiapplicationdelegate, Ccdirectordelegate> Ccappdelegate owns members: UIWindow *window_ * Most cocos2d applications should rewrite Ccappdelegate, ccappdelegate as the application's entry. At a minimum, the Startscene method should be replicated in order to return to the first scene that the app will show if you want to further customize the cocos2d (such as customizing the display's pixel mode), make a carbon applicaton:didfinishlaunchingwithoptions: method */#import "AppDelegate.h" #import "IntroScene.h" #import "HelloWorldScene.h" @implementation appdelegate//-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{// When inheriting from Ccappdelegate, the only way to implement this method is that the first time the application is executed this method//here, you can set the default value of cocos2d parameters//There is a number of simple options you c An change.//if you still call the Setupcocos2dwithoptions method, not flexible and free, then you can put your own top cocos2d[self setupcocos2dwithoptions:@{//display Fpsccsetupshowdebugstats: @ (YES),//use reduced frame rate//Ccsetupanimationinterval: @ (1.0/30.0),//Use a faster frame rate//ccsetupfixedupdateinterval: @ (1.0/180.0),//Set screen for portrait mode//ccsetupscree Norientation:ccscreenorientationportrait,//Use 16-bit color://ccsetuppixelformat:keaglcolorformatrgb565, Using a unified fixed coordinate system//ccsetupscreenmode:ccscreenmodefixed,//make IPad's act like they run at a 2x content scale. (IPad retina 4x)//ccsetuptabletscale2x: @ (YES),//For more information see Ccappdelegate.h}];return YES;} -(Ccscene *) startscene{//This method returns when the application starts, the first scene to show return [Introscene scene];} @end


Scene One

  introscene.h//  31_cocos2d Primer////  Created by Beyond on 14-9-5.//  Copyright Com.beyond 2014. All rights reserved.////importing Cocos2d.h and cocos2d-ui.h, would import anything you need to start using Cocos2d-v3#imp  Ort "Cocos2d.h" #import "cocos2d-ui.h"/** * the  intro scene *  Note, that scenes should now being based on Ccscene, and Not Cclayer, as previous versions *  Main usage for Cclayer now, was to make colored backgrounds (rectangles) * */@inte Rface introscene:ccscene+ (Introscene *) scene;-(ID) init; @end



introscene.m//31_cocos2d Primer////Created by Beyond on 14-9-5.//Copyright Com.beyond 2014. All rights reserved.//#import "IntroScene.h" #import "HelloWorldScene.h" @implementation introscene#pragma mark-life cycle + ( Introscene *) Scene{return [[Self alloc] init];}    -(ID) init{self = [super init];        if (!self) return (nil); Create a background color of dark gray ccnodecolor *background = [Ccnodecolor nodewithcolor:[cccolor colorwithred:0.2f green:0.2f blue:0.2f Alph    A:1.0F]];        [Self addchild:background];    Create a text label Cclabelttf *label = [Cclabelttf labelwithstring:@ "Hello Beyond" fontname:@ "Chalkduster" fontsize:36.0f];    Label.positiontype = ccpositiontypenormalized;    Label.color = [Cccolor Redcolor];    The positive center of the screen is the Cartesian coordinate system, where the origin is in the lower left label.position = CCP (0.5f, 0.5f);        [Self Addchild:label]; Create a Start button and click to go to the next scene ccbutton *helloworldbutton = [Ccbutton buttonwithtitle:@ "[Start]" fontname:@ "Verdana-bold" fon    TSIZE:18.0F]; Helloworldbutton.positiontype = Ccpositiontypenormalized;     In the middle of the screen, note here is the Cartesian coordinate system, the origin is lower left helloworldbutton.position = CCP (0.5f, 0.35f);    Listen for click events [Helloworldbutton settarget:self selector: @selector (onspinningclicked:)];    [Self Addchild:helloworldbutton]; Returns the created scene object return self; #pragma mark-button click event, switch to next view-(void) onspinningclicked: (ID) sender{//animation to the next scene [[Ccdirector Shareddirector] Replac Escene:[helloworldscene scene] Withtransition:[cctransition transitionpushwithdirection:cctr Ansitiondirectionleft duration:1.0f]];} @end


Scenario Two
  helloworldscene.h//  31_cocos2d Primer////  Created by Beyond on 14-9-5.//  Copyright Com.beyond 2014. All rights reserved.////importing Cocos2d.h and cocos2d-ui.h, would import anything you need to start using cocos2d v3#imp Ort "Cocos2d.h" #import "cocos2d-ui.h"/** *  Home View */@interface helloworldscene:ccscene+ (Helloworldscene *) scene;-( ID) init; @end


helloworldscene.m//31_cocos2d Primer////Created by Beyond on 14-9-5.//Copyright Com.beyond 2014. All rights reserved.//#import "HelloWorldScene.h" #import "IntroScene.h" @implementation helloworldscene{ccsprite *_ Sprite;} #pragma mark-life cycle + (Helloworldscene *) scene{return [[Self alloc] init];} -(ID) init{if (! (        self = [super init]) return (nil);        Allow interactive self.userinteractionenabled in scene mode = YES; Create a background color of dark gray ccnodecolor *background = [Ccnodecolor nodewithcolor:[cccolor colorwithred:0.2f green:0.2f blue:0.2f Alph    A:1.0F]];        [Self addchild:background];        Add a sprite and set the position centered _sprite = [Ccsprite spritewithimagenamed:@ "Colorcircle.png"];    _sprite.position = CCP (SELF.CONTENTSIZE.WIDTH/2,SELF.CONTENTSIZE.HEIGHT/2);        [Self addchild:_sprite]; Create a rotation action for the sprite, and invoke the wizard's Runaction method, repeating the action ccactionrotateby* actionspin = [Ccactionrotateby actionwithduration:1.5f    ANGLE:360]; [_sprite runaction:[ccactionrepeatforever ActionwithactiOn:actionspin]]; Top right, create a Back button, click to return to the first scene Ccbutton *backbutton = [Ccbutton buttonwithtitle:@ "[Back]" fontname:@ "verdana-bold" font    SIZE:18.0F];    Backbutton.positiontype = ccpositiontypenormalized;    On the top right of the screen note here is the Cartesian coordinate system, the origin at the lower left backbutton.position = CCP (0.85f, 0.95f);    Listen for click events [Backbutton settarget:self selector: @selector (onbackclicked:)];        [Self Addchild:backbutton]; Returns the created scene object return self; -(void) dealloc{//Clean up code goes here} #pragma mark-enter & exit//-----------------------------------------        -------------------------------(void) onenter{//must always first call the OnEnter method of the parent class [Super OnEnter]; In Pre-v3, touch enable and scheduleupdate were called here/in V3, Touch is enabled by setting Userinteractionenab    LED for the individual nodes//per frame update was automatically enabled, if update is overridden}-(void) onexit{ The OnExit method of the parent class must always be called last [Super OnExit];} #pragma mark-The user touches the screen, the sprite follows the finger movement-(void) Touchbegan: (UITOuch *) Touch withevent: (uievent *) Event {Cgpoint Touchloc = [Touch locationinnode:self];        Output Touch Point coordinates cclog (@ "Move sprite to @%@", Nsstringfromcgpoint (Touchloc));    The moving sprite to the touch point represents an absolute by representation of the relative ccactionmoveto *actionmove = [Ccactionmoveto actionwithduration:1.0f position:touchloc]; Invoke the wizard's Runaction method to perform the action [_sprite Runaction:actionmove];} #pragma mark-button click Event-(void) onbackclicked: (ID) sender{//Use transition animation to switch scenes to Introscene [[Ccdirector Shareddirector] Repla Cescene:[introscene scene] Withtransition:[cctransition Transitionpushwithdirection:cctransi Tiondirectionright duration:1.0f]];} @end










IOS_31_COCOS2D Environment Construction

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.