Introduction to basic program framework and common actions of Cocos2d-iPhone V3 (1)

Source: Internet
Author: User

Introduction to basic program framework and common actions of Cocos2d-iPhone V3 (1)
Introduction to basic program framework and common actions of Cocos2d-iPhone V3 (1)

  •  
  • Part 1: a basic framework of Cocos2d-iPhone V3 1. AppDelegate

    First, seeAppDelegate.hClass is inherited fromCCAppDeleagate, Nothing special:

    #import cocos2d.h@interface AppDelegate : CCAppDelegate@end

    Check againAppDelegate.mYou only need to implement two functions.startSceneYou can simply load your custom scenario:

    #import AppDelegate.h#import MainScene.h@implementation AppDelegate-(BOOL)application:(UIApplication *)application    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    [self setupCocos2dWithOptions:@{        CCSetupShowDebugStats: @(YES),    }];    return YES;}- (CCScene *)startScene{    return [HomeScene scene];}
    2. Your own scenario class MainScene

    First lookMainScene.hWe can see there are two methods: one is static methodscene, One is the class Methodinit:

    #import cocos2d.h#import cocos2d-ui.h@interface MainScene : CCScene+ (MainScene *)scene;- (id)init;@end

    Let's take a look.MainScene.mThere are more headers here. First, let's look at the overall structure:

    #import MainScene.h@implementation MainScene{    CCSprite *_sprite;}+ (MainScene *)scene { /* ... */ }- (id)init { /* ... */ }- (void)dealloc { /* ... */ }- (void)onEnter { /* ... */ }- (void)onExit { /* ... */ }- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { /* ... */ }@end

    You must have a static method to return a custom scenario instance.sceneMethod. Also haveonEnterAndonExitIndicates that it will be called when you enter/exit the scenario.touchBeganIs a Touch Handler.

    2.1. Static Methodscene

    Nothing to say:

    + (MainScene *)scene{    return [[self alloc] init];}
    2.2.init
    - (id)init{    // Apple recommend assigning self with supers return value    self = [super init];    if (!self) return(nil);    // Enable touch handling on scene node    self.userInteractionEnabled = YES;    // Create a colored background (Dark Grey)    CCNodeColor *background =        [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f                                                   green:0.2f                                                    blue:0.2f                                                   alpha:1.0f]];    [self addChild:background];    // Add a sprite    _sprite = [CCSprite spriteWithImageNamed:@Icon-72.png];    _sprite.position  = ccp(self.contentSize.width/2,self.contentSize.height/2);    [self addChild:_sprite];    // Animate sprite with action    CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360];    [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]];    // done    return self;}
    • CallsuperOfinit
    • SetuserInteractionEnabledIsYESTo receive touch events
    • Add a background node.CCNodeColor
    • Add a genie NodeCCSprite
    • Add actions to the genie NodeCCActionRotateBy
    • Returnself2.3. Handler:onEnter

      Remember to callsuperOfonEnter:

      - (void)onEnter{    [super onEnter];}
      2.4. exit scenario Handler:onExit

      Remember to callsuperOfonExit:

      - (void)onExit{    [super onExit];}
      2.5. Touch Handler
      - (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{    CGPoint touchLoc = [touch locationInNode:self];    CCActionMoveTo *actionMove =        [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];    [_sprite runAction:actionMove];}
      • FirstUITouchParameter to obtain the Touch LocationCGPoint
      • Set according to the obtained locationCCActionAnd finally run thisCCActionPart 2: Action 1. DisplacementCCActionMoveBy
        + (id)actionWithDuration:(CCTime)duration position:(CGPoint)deltaPosition;
        2. ShiftCCActionMoveTo
        + (id)actionWithDuration:(CCTime)duration position:(CGPoint)position;
        3. Rotate an angleCCActionRotateBy

        Note that the angle is an angle (360 degrees a week), not a radian (2 π a week ):

        + (id)actionWithDuration:(CCTime)duration angle:(float)deltaAngle;
        4. RotateCCActionRotateTo

        Note that the angle is an angle (360 degrees a week), not a radian (2 π a week ):

        + (id)actionWithDuration:(CCTime)duration angle:(float)angle;
        5. Gradient appearanceCCActionFadeIn

        This action fades in the target, it modifies the opacity from 0 to 1.

        + (id)actionWithDuration:(CCTime)d;
        6. Gradient disappearsCCActionFadeOut

        This action fades out the target, it modifies the opacity from 1 to 0.

        + (id)actionWithDuration:(CCTime)d;
        7. GradientCCActionFadeTo

        You may notice spelling errors in the Cocos2d source code,opacityWrittenopactiy(CCActionInterval.hMedium ):

        /** *  Creates a fade action. * *  @param duration Action duration. *  @param opactiy  Opacity to fade to. * *  @return New fade action. */+ (id)actionWithDuration:(CCTime)duration opacity:(CGFloat)opactiy;

        -

        Reprinted please indicate from: http://blog.csdn.net/prevention

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.