Part 1: a basic framework of Cocos2d-iPhone V3 1. AppDelegateFirst, seeAppDelegate.h
Class is inherited fromCCAppDeleagate
, Nothing special:
#import cocos2d.h@interface AppDelegate : CCAppDelegate@end
Check againAppDelegate.m
You only need to implement two functions.startScene
You 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 MainSceneFirst lookMainScene.h
We 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.m
There 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.scene
Method. Also haveonEnter
AndonExit
Indicates that it will be called when you enter/exit the scenario.touchBegan
Is 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;}
- Call
super
Ofinit
- Set
userInteractionEnabled
IsYES
To receive touch events
- Add a background node.
CCNodeColor
- Add a genie Node
CCSprite
- Add actions to the genie Node
CCActionRotateBy
- Return
self
2.3. Handler:onEnter
Remember to callsuper
OfonEnter
:
- (void)onEnter{ [super onEnter];}
2.4. exit scenario Handler:onExit
Remember to callsuper
OfonExit
:
- (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];}
- First
UITouch
Parameter to obtain the Touch LocationCGPoint
- Set according to the obtained location
CCAction
And finally run thisCCAction
Part 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,opacity
Writtenopactiy
(CCActionInterval.h
Medium ):
/** * 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