HelloWorldScene.h
#include "cocos2d.h"
class HelloWorld :public cocos2d::CCLayer
{
public:
virtual bool init();
staticcocos2d::CCScene* scene();
void menuFun(CCObject* pSender);
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "NewScene.h"
using namespacecocos2d;
using namespaceCocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer =HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
boolHelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
returnfalse;
}
CCSprite *spr=CCSprite::create("elephone.png");
spr->setPosition(ccp(300,170));
this->addChild(spr);
CCLabelTTF *labelttf=CCLabelTTF::create("切换场景","Thonburi", 38);
labelttf->setColor(ccc3(0,255, 255));
CCMenuItemLabel *item=CCMenuItemLabel::create(labelttf,this,menu_selector(HelloWorld::menFun));
CCMenu *menu=CCMenu::create(item,NULL);
menu->setPosition(ccp(100,100));
addChild(menu,0,2);
returntrue;
}
voidHelloWorld::menFun(CCObject *pScene)
{
CCTransitionScene *sitionscene=NULL;
CCScene *s=NewScene::scene();
float t=1.2;
// CCTransitionJumpZoom动画效果。 t两个场景切换的时间,s新的场景。
sitionscene=CCTransitionJumpZoom::create(t, s);
开始切换。
CCDirector::sharedDirector()->replaceScene(sitionscene);
}
#ifndef __________NewScene__
#define __________NewScene__
#include <iostream>
#include "cocos2d.h"//要包含头文件
class NewScene :publiccocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene *scene();
CREATE_FUNC(NewScene);
};
#endif /* defined(__________NewScene__) */
#include "NewScene.h"
using namespace cocos2d;
CCScene *NewScene::scene()
{
CCScene *scene=CCScene::create();
NewScene *layer=NewScene::create();
scene->addChild(layer);
return scene;
}
boolNewScene::init()
{
if (!CCLayer::init())
{
return false;
}
CCSprite *spr=CCSprite::create("flower.png");
spr->setPosition(ccp(160,200));
addChild(spr);
return true;
}