IOS_31_cocos2d_CCScene, ioscocos2d

Source: Internet
Author: User
Tags android games

IOS_31_cocos2d_CCScene, ioscocos2d


CCScene "scenario ",

You must pass

[Ccdiresharshareddire] runWithScene: <# (CCScene *) #>]

To start the first scenario, you can also use

-(CCScene *) startScene

Method To create and return the first scenario.

Of course, complex games include many scenarios,

Scenario switching is also completed through CCDirector.

CCScene is an abstract concept and has no visual display function,


//// 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 text label Chalkduster Courier New CCLabelTTF * label = [CCLabelTTF labelWithString: @ "Hello Beyond" fontName: @ "Courier New" fontSize: 36366f]; // purple: Red + 0 + Blue // Yellow: Red + green + 0 // Cyan: 0 + green + blue label. color = [CCColor redColor]; label. positionType = CCPositionTypeNormalized; // in the center of the screen, note that the Cartesian coordinate system is used. 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



Common Operations

1. Run the first scenario

Generally, the application proxy AppDelegate applicationDidFinishLaunching: Method game parameters,

In the startScene method of the proxy, create and return the first scenario of the game.

//// AppDelegate. m // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. // AppDelegate inherits from kernel/* CCAppDelegate inherits from NSObect and complies with the Protocol: <UIApplicationDelegate, CCDirectorDelegate> CCAppDelegate has members: UIWindow * window _ * Most Cocos2d applications should rewrite kernel, CCAppDelegate serves as the application portal. 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 the Cocos2D [self setupcocos2dwitexceptions: @ {// display FPS (frame per second) CCSetupShowDebugStats: @ (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 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


[IntroScene scene] returns a CCScene object.


2. replacement scenario

<span style="font-size:18px;">[[CCDirector sharedDirector] replaceScene:scene];</span>

This method replaces the old one with the new one,

Cocos2d releases the memory of the old scenario,

Delete all nodes in the old scenario,

Stop allAction and message Scheduling,

Therefore, you do not need to manually release the memory of the old scenario.


3. Push and pop-up scenarios

Use replaceScene: to run a new scenario,

However, the memory in the old scenario is released.

Sometimes you want to run a new scenario without releasing the memory of the old scenario,

At this time, we need to use the pushScene: And popScene methods of CCDirector.

1> pushScene: Push the method into a new scenario,

New scenarios are stacked on the old ones,

However, the memory of the old scenario is not released, and the old scenario continues to be stored in the memory.

<span style="font-size:18px;">[[CCDirector sharedDirector] pushScene:scene];</span>

2> use the popScene method to pop up the upper-layer scenario and release its memory,

Re-display old scenarios retained in memory

<span style="font-size:18px;">[[CCDirector sharedDirector] popScene];</span>



CCTransition

The scenario switching is described above, but it is completed in an instant,

Sometimes you want to have some transitional effects during scenario switching,

It refers to switching scenes in the form of animations and is called "scenario transition ".

To achieve the scenario transition effect,

The CCTransition subclass must be used,

CCTransition inherits CCScene,

It contains many child classes, each of which has different scenario transition effects,







Cocos2d has a lot of transition effects available,

Are subclasses of CCTransition,

Class names generally start with CCTransition.

Note: CCTransition can only be used when replaceScene: And pushScene,

This transitional effect cannot be used when popScene appears.


Node Lifecycle

When it comes to scenario transition, we have to talk about the node lifecycle,

That is, the process from when a node is added to the screen to when it is removed from the screen,

CCNode provides the following lifecycle methods:

<Span style = "font-size: 18px;"> // call-(void) onEnter when a node is added to the screen or displayed on the screen again; // This method will be called after onEnter is called. If the scenario transition effect is used, the onEnterTransitionDidFinish method will be called only after the scenario transition is complete-(void) onEnterTransitionDidFinish; // call-(void) onExit when the node is removed from the screen or temporarily leaves the screen; </span>


During scenario switching, the node life cycle method has a certain call sequence.

Extract A common parent class BaseLayer that inherits CCLayerColor,

Complete some public operations in it, and then let the subclass inherit it

BaseLayer code

<Span style = "font-size: 18px;"> // BaseLayer. h # import "cocos2d. h "@ interface BaseLayer: CCLayerColor // used to create the scene where the layer is located + (CCScene *) scene; // the background color of the layer, which is assigned to the subclass for Implementation + (ccColor4B) bgColor; @ end </span>
<Span style = "font-size: 18px;"> // BaseLayer. m # import "BaseLayer. h "@ implementation BaseLayer # pragma mark-initialization scenario + (CCScene *) scene {// obtain the background color of the current class ccColor4B color = [self bgColor]; // create a layer BaseLayer * layer = [[self class] layerWithColor: color] based on the current class name; // receives the touch input layer. isTouchEnabled = YES; CCScene * scene = [CCScene node]; [scene addChild: layer]; return scene ;}# pragma mark-print life cycle method-(void) onEnter {// _ cmd indicates the current selector [super onEnter]; // The first % @ is the print class name, the second % @ is to print the method name NSLog (@ "% @ --> % @", NSStringFromClass ([self class]), NSStringFromSelector (_ cmd);}-(void) onEnterTransitionDidFinish {[super onEnterTransitionDidFinish]; NSLog (@ "% @ --> % @", NSStringFromClass ([self class]), NSStringFromSelector (_ cmd);}-(void) onExit {[super onExit]; NSLog (@ "% @ --> % @", NSStringFromClass ([self class]), NSStringFromSelector (_ cmd ));} @ end </span>








IOS cocos2d-x draw a straight line how to make it as a callback function, that is, I want to draw him to execute

You can try this.
Add in header file
Array * _ touches;. cpp (can also be added to ccTouchEnded)
CcTouchBegan (Touch * touch, Event * event) {if (_ touches-> count ()> = 2) {_ touches-> removeAllObjects ();} _ touches-> addObject (touch);} draw () {cocos2d: Layer: draw (); if (_ touches-> count () = 2) {// draw a straight line }}

Take a question cocos2d-xna can develop ios and android games?

Microsoft has basically abandoned XNA ~~, VS2012 is no longer integrated ~
Instead, the monogame framework inherits the XNA features and the most important thing is the porting of multiple platforms.

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.