CocosX学习实录03
今天天气真好
发现C++有点意思
写了头文件,cpp能少写一个括号
一个括号,省多少个回车啊
啊哈哈~~~~
触摸方法的声明
一律先在h头文件声明,再在cpp写
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-(void) onEnter
-(void) onEnterTransitionDidFinish
-(void) onExit
上面4句,对应C++是
virtual void ccTouchesBegan(CCSet *touches,CCEvent *event);
virtual void ccTouchesMove(CCSet *touches,CCEvent *event);
virtual void ccTouchesEnd(CCSet *touches,CCEvent *event);
virtual void onEnter();
virtual void onEnterTransitionDidFinish();
virtual void onExit();
在cpp,去掉virtural, 加上类名
void OtherScene::ccTouchesBegan(CCSet *touches,CCEvent *event){}
void OtherScene::onEnter(){}
void OtherScene::onEnterTransitionDidFinish(){}
void OtherScene::onExit(){}
触摸方法的放置
注意,放在init只有第1次有用,添加其他Layer再删除回来,就不生效,所以,得放在onEnter才有用。
this->setTouchEnabled(true);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
触摸方法的实现
1)调用父类方法。objC的super XXX对应是CCLayer::XXX
[super onEnter];
[super onEnterTransitionDidFinish];
[super onExit];
CCLayer::onEnter();
CCLayer::onEnterTransitionDidFinish();
CCLayer::onExit();
2)开始触摸。转换场景
CCScene* newScene = [LoadingScene sceneWithTargetScene:TargetSceneFirstScene];
[[CCDirector sharedDirector] replaceScene:newScene];
CCScene* newScene = LoadingScene::sceneWithTargetScene(TargetSceneFirstScene);
CCDirector::sharedDirector()->replaceScene(newScene);
3)枚举类型通用
typedef enum
{
LayerTagGameLayer,
LayerTagUILayer,
} MultiLayerSceneTags;
typedef enum
{
ActionTagGameLayerMovesBack,
ActionTagGameLayerRotates,
} MultiLayerSceneActionTags;
CCLayer子类的头文件改成C++
@interface MultiLayerScene : CCLayer
{
bool isTouchForUserInterface;
}
// Accessor methods to access the various layers of this scene
+(MultiLayerScene*) sharedLayer;
@property (readonly) GameLayer* gameLayer;
@property (readonly) UserInterfaceLayer* uiLayer;
+(CGPoint) locationFromTouch:(UITouch*)touch;
+(CGPoint) locationFromTouches:(NSSet *)touches;
+(id) scene;
如下
class MultiLayerScene:public CCLayer {
public:
MultiLayerScene();
~MultiLayerScene();
CREATE_FUNC(MultiLayerScene);
bool isTouchForUserInterface;
static MultiLayerScene* sharedLayer;
static CCPoint locationFromTouch(CCTouch * touch);
static CCPoint locationFromTouches(CCSet * touches);
static CCScene *scene;
};
CCRANDOM_0_1() 在cocos2d中使用 ,范围是[0,1]
第6章
1)typedef enum直接复制
2)注意把Sprite传给构造函数了
3)