Part 1: a basic framework of Cocos2d-iPhone V3 1. AppDelegateFirst, 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 MainSceneFirst 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 MethodsceneNothing 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
superOfinit
- Set
userInteractionEnabledIsYESTo receive touch events
- Add a background node.
CCNodeColor
- Add a genie Node
CCSprite
- Add actions to the genie Node
CCActionRotateBy
- Return
self2.3. Handler:onEnterRemember to callsuperOfonEnter:
- (void)onEnter{ [super onEnter];}
2.4. exit scenario Handler:onExitRemember 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];}
- First
UITouchParameter to obtain the Touch LocationCGPoint
- Set according to the obtained location
CCActionAnd 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 angleCCActionRotateByNote that the angle is an angle (360 degrees a week), not a radian (2 π a week ):
+ (id)actionWithDuration:(CCTime)duration angle:(float)deltaAngle;
4. RotateCCActionRotateToNote 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 appearanceCCActionFadeInThis action fades in the target, it modifies the opacity from 0 to 1.
+ (id)actionWithDuration:(CCTime)d;
6. Gradient disappearsCCActionFadeOutThis action fades out the target, it modifies the opacity from 1 to 0.
+ (id)actionWithDuration:(CCTime)d;
7. GradientCCActionFadeToYou 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