IOS_31_cocos2d environment setup

Source: Internet
Author: User

IOS_31_cocos2d environment setup

The final result is as follows:




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MS6000dpdMnPz8LU2CBjb2NvczJktcTRucv1sPwstPO4xTEwME08L3A + cjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140906/2014090608350613.png" alt = "\">

2. decompress the package, go to the cocos2d home directory, and copy the path to the terminal.

3. Execute./install. sh to start the installation (the essence is to copy it to the XCode directory)






Before XCode is installed, the interface for creating a project is as follows:



After XCode is installed, the page for creating a project is as follows:




The directory structure of the created project is as follows:




Run the program directly. The effect is as follows:



Application proxy



//// AppDelegate. h // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. // AppDelegate inherits from CCAppDelegate/* CCAppDelegate inherits from NSObect and complies with the Protocol:
 
  
CCAppDelegate has members: UIWindow * window _ * Most Cocos2d applications should rewrite CCAppDelegate and CCAppDelegate as the application entry. at least the startScene method should be rewritten to return the first scene to be displayed by the application. If you want to further customize Cocos2d (for example, the pixel mode of custom display), rewrite applicaton: didfinishlaunchingwitexceptions: method */# import "cocos2d. h "@ interface AppDelegate: CCAppDelegate @ end
 


//// AppDelegate. m // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. // AppDelegate inherits from CCAppDelegate/* CCAppDelegate inherits from NSObect and complies with the Protocol:
 
  
CCAppDelegate has members: UIWindow * window _ * Most Cocos2d applications should rewrite CCAppDelegate and CCAppDelegate as the application entry. at least the startScene method should be rewritten to return the first scene to be displayed by the application. If you want to further customize Cocos2d (for example, the pixel mode of custom display), rewrite applicaton: didfinishlaunchingwitexceptions: method */# import "AppDelegate. h "# import" IntroScene. h "# import" HelloWorldScene. h "@ implementation AppDelegate //-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// when inherited from CCAppDelegate, the only method to be implemented is this method. When the application is started for the first time, it will first execute this method // here, you can set the default values of Cocos2D parameters. // There are a number of simple options you can change. // If the setupcocos2dwitexceptions method is not flexible enough, you can pin Cocos2D [self setupcocos2dwitexceptions: @ {// display FPSCCSetupShowDebugStats: @ (YES ), // use the reduced Frame Rate // CCSetupAnimationInterval: @ (1.0/30.0), // use an accelerated Frame Rate // CCSetupFixedUpdateInterval: @ (1.0/180.0 ), // set the screen to portrait mode // CCSetupScreenOrientation: CCScreenOrientationPortrait, // use the 16-bit color: // CCSetupPixelFormat: kEAGLColorFormatRGB565, // use a unified and 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 the first scenario to be displayed when the application is started. return [IntroScene scene];} @ end
 


Scenario 1

//// IntroScene. h // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. //// Importing cocos2d. h and cocos2d-ui.h, will import anything you need to start using cocos2d-v3 # import "cocos2d. h "# import" cocos2d-ui.h "/*** The intro scene * Note, that scenes shoshould now be based on CCScene, and not CCLayer, as previous versions * Main usage for CCLayer now, is to make colored backgrounds (rectangles) **/@ interface IntroScene: CCScene + (IntroScene *) scene;-(id) init; @ end



//// IntroScene. m // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. // # import "IntroScene. h "# import" HelloWorldScene. h "@ implementation IntroScene # pragma mark-lifecycle + (IntroScene *) scene {return [[self alloc] init];}-(id) init {self = [super init]; if (! Self) return (nil); // the background color is dark gray. CCNodeColor * background = [CCNodeColor nodeWithColor: [CCColor colorWithRed: 0.2f green: 0.2f blue: 0.2f alpha: 1.0f]; [self addChild: background]; // create a text tag, CCLabelTTF * label = [CCLabelTTF labelWithString: @ "Hello Beyond" fontName: @ "Chalkduster" fontSize: 36366f]; label. positionType = CCPositionTypeNormalized; label. color = [CCColor redColor]; // in the center of the screen, note that the Cartesian coordinate system is used, and the origin is in the label at the bottom left. position = ccp (0.5f, 0.5f); [self addChild: label]; // create a start button and click it to enter the CCButton * helloWorldButton = [CCButton buttonWithTitle: @ "[Start]" fontName: @ "Verdana-Bold" fontSize: 18366f]; helloWorldButton. positionType = CCPositionTypeNormalized; // The center of the screen depends on the bottom. Note that the Cartesian coordinate system is used. The origin is in the lower left corner of helloWorldButton. position = ccp (0.5f, 0.35f); // listen to the click event [helloWorldButton setTarget: self selector: @ selector (onSpinningClicked :)]; [self addChild: helloWorldButton]; // return the created scene object return self;} # pragma mark-click the event and switch to the next scene-(void) onSpinningClicked :( id) sender {// switch the animation to the next scenario [[CCDirector shareddire] replaceScene: [HelloWorldScene scene] withTransition: [CCTransition failed: cctransitiondireleft left duration: 1.0f];} @ end


Scenario 2
//// HelloWorldScene. h // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. //// Importing cocos2d. h and cocos2d-ui.h, will import anything you need to start using Cocos2D v3 # import "cocos2d. h "# import" cocos2d-ui.h "/*** main scenario */@ interface HelloWorldScene: CCScene + (HelloWorldScene *) scene;-(id) init; @ end


//// HelloWorldScene. m // 31_cocos2D getting started /// 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-lifecycle + (HelloWorldScene *) scene {return [[self alloc] init];}-(id) init {if (! (Self = [super init]) return (nil); // allow interaction in the scenario mode. userInteractionEnabled = YES; // the background color is dark gray. CCNodeColor * background = [CCNodeColor nodeWithColor: [CCColor colorWithRed: 0.2f green: 0.2f blue: 0.2f alpha: 1.0f]; [self addChild: background]; // Add a genie and set the center position _ 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 genie and call the runAction method of the genie to repeat the action CCActionRotateBy * actionSpin = [CCActionRotateBy actionWithDuration: 1.5f angle: 360]; [_ sprite runAction: [CCActionRepeatForever actionWithAction: actionSpin]; // create a return button in the upper right corner. After clicking, return to the previous scenario CCButton * backButton = [CCButton buttonWithTitle: @ "[Back]" fontName: @ "Verdana-Bold" fontSize: 18366f]; backButton. positionType = CCPositionTypeNormalized; // in the upper-right corner of the screen, note that the Cartesian coordinate system is used. The origin is in the backButton at the lower-left corner. position = ccp (0.85f, 0.95f); // listen to the click event [backButton setTarget: self selector: @ selector (onBackClicked :)]; [self addChild: backButton]; // return the created scenario object return self;}-(void) dealloc {// clean up code goes here} # pragma mark-Enter & Exit // empty (void) onEnter {// always first call the onEnter method of the parent class [super onEnter]; // In pre-v3, touch enable and scheduleUpdate was called here // In v3, touch is enabled by setting userInteractionEnabled for the individual nodes // Per frame update is automatically enabled, if update is overridden}-(void) onExit {// The onExit method of the parent class [super onExit] must always be called at the end;} # pragma mark-the user can touch the screen, and the genie can follow his finger to move-(void) touchBegan :( UITouch *) touch withEvent :( UIEvent *) event {CGPoint touchLoc = [touch locationInNode: self]; // output the coordinate CCLOG of the touch point (@ "Move sprite to @ % @", NSStringFromCGPoint (touchLoc); // move the genie To the touch point To indicate absolute By, indicating relative CCActionMoveTo * actionMove = [CCActionMoveTo actionWithDuration: 1.0f position: touchLoc]; // call the runAction method of the genie to execute the action [_ sprite runAction: actionMove] ;}# pragma mark-click the event-(void) onBackClicked :( id) sender {// use the transition animation. Switch the scene to IntroScene [[CCDirector shareddire] replaceScene: [IntroScene scene] withTransition: [CCTransition duration: Transition duration: 1.0f];} @ end










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.