-
首先看AppDelegate.h
,类是继承自CCAppDeleagate
,其他没什么特别的:
#import "cocos2d.h"
@interface AppDelegate : CCAppDelegate
@end
再看AppDelegate.m
,只要实现两个函数即可,注意其中实现startScene
就可以加载你自定义的场景啦,简单吧:
#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];
}
先看看MainScene.h
,我们看有两个方法,一个是静态方法scene
,一个是类方法init
:
#import "cocos2d.h"
#import "cocos2d-ui.h"
@interface MainScene : CCScene
+ (MainScene *)scene;
- (id)init;
@end
再看看MainScene.m
,这里头东西就多了。首先看整体结构:
#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
必须要有一个静态方法返回一个自定义的场景实例scene
方法。还要有onEnter
和onExit
表示进入/离开该场景就会调用。touchBegan
是一个 Touch Handler。
scene
没什么好说的:
+ (MainScene *)scene
{
return [[self alloc] init];
}
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;
}
super
的init
userInteractionEnabled
为YES
来接收触摸事件CCNodeColor
CCSprite
CCActionRotateBy
self
onEnter
一定要记得调用super
的onEnter
:
- (void)onEnter
{
[super onEnter];
}
onExit
一定要记得调用super
的onExit
:
- (void)onExit
{
[super onExit];
}
- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLoc = [touch locationInNode:self];
CCActionMoveTo *actionMove =
[CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
[_sprite runAction:actionMove];
}
UITouch
参数来获取被触摸位置CGPoint
CCAction
,最后运行这个CCAction
CCActionMoveBy
+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)deltaPosition;
CCActionMoveTo
+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)position;
CCActionRotateBy
注意其中的 angle 是角度(一周 360 度),不是弧度(一周 2π):
+ (id)actionWithDuration:(CCTime)duration angle:(float)deltaAngle;
CCActionRotateTo
注意其中的 angle 是角度(一周 360 度),不是弧度(一周 2π):
+ (id)actionWithDuration:(CCTime)duration angle:(float)angle;
CCActionFadeIn
This action fades in the target, it modifies the opacity from 0 to 1.
+ (id)actionWithDuration:(CCTime)d;
CCActionFadeOut
This action fades out the target, it modifies the opacity from 1 to 0.
+ (id)actionWithDuration:(CCTime)d;
CCActionFadeTo
你可能会注意到 Cocos2d 的源码里有拼写错误,opacity
写成了opactiy
(CCActionInterval.h
中):
/**
* 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;
-
转载请注明来自:http://blog.csdn.net/prevention