入口:AppDelegate->applicationDidFinishLaunching()
->CCScene *pScene = HelloWorld::scene()
------------>
HelloWorld::scene()->create();
->create()
-->调用CREATE_FUNC(HelloWorld)->调用HelloWorld->init()方法
背景色设置
CCLayerColor::initWithColor(ccc4(0,255,0,100));
创建精灵:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *player = CCSprite::spriteWithFile("Player.png",CCRectMake(0, 0, 27, 40));
//设置位置
player->setPosition(ccp(player->getContentSize().width/2,winSize.height/2));
this->addChild(player);
//设置可触摸
this->setTouchenabled(true);
//设置背景音乐
SimpleAudioEngine::shareEngine()->playBackgroundMusic"background-music-aac.wav"
,true);
this->setTouchenabled(true);--->重载方法
void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
CCCallFuncN *callFunc = CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::spriteMoveFinished));//移动结束后回调spriteMoveFinished方法
CCSequence *seque = CCSequence::actionOneTwo(moveto,callFunc);//执行的顺序
添加动作结束特效
SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
动作调用的顺序是:
在init()中
addTarget()
update()--->碰撞检测
之后-->触摸->产生子弹
在子弹和敌人结束动作后调用
spriteMoveFinished()//显示失败
增加结果显示层
GameOverScene
CC_SYNTHESIZE_READONLY(cocos2d::CCLabelTTF*,_label,Label);
//显示层变量:CCLabelTTF*,_label, 只有getLabel方法
调用->retain()方法一定要调用release()方法
CCDelayTime* delayTime = CCDelayTime::actionWithDuration(3);
CCCallFunc * callFunc = CCCallFunc::actionWithTarget(this,
callfunc_selector(GameOverLayer::gameOverDone));
CCSequence *sequence = CCSequence::actionOneTwo(delayTime,callFunc);
this->runAction(sequence);
在延时一定时间后 回调gameOverDone 方法
gameOverDone -->
CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
切换回HelloWorld场景
类解析和析构方法
初始化变量和释放资源