场景一:
#ifndef
__HELLO_H__
#define
__HELLO_H__
#include
"cocos2d.h"
class
Hello
:
public
cocos2d
::
CCLayer
{
public
:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual
bool
init
();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static
cocos2d
::
CCScene
*
scene
();
// a selector callback
void
menuCloseCallback
(
CCObject
*
pSender
);
// implement the "static node()" method manually
CREATE_FUNC
(
Hello
);
private
:
cocos2d
::
CCLabelTTF
*
m_pLabel
;
cocos2d
::
CCMenuItemImage
*
m_pCloseItem
;
};
#endif
#include
"Hello.h"
#include
"../CocosDenshion/win32/MciPlayer.h"
#include
"sprite_nodes/CCAnimation.h"
#include
"AppMacros.h"
#include
"HelloWorldScene.h"
using
namespace
cocos2d
;
using
namespace
CocosDenshion
;
USING_NS_CC
;
CCScene
*
Hello
::
scene
()
{
// 'scene' is an autorelease object
CCScene
*
scene
=
CCScene
::
create
();
// 'layer' is an autorelease object
Hello
*
layer
=
Hello
::
create
();
// add layer as a child to scene
scene
->
addChild
(
layer
);
// return the scene
return
scene
;
}
// on "init" you need to initialize your instance
bool
Hello
::
init
()
{
//
// 1. super init first
if
(
!
CCLayer
::
init
()
)
{
return
false
;
}
CCSize
visibleSize
=
CCDirector
::
sharedDirector
()->
getVisibleSize
();
CCPoint
origin
=
CCDirector
::
sharedDirector
()->
getVisibleOrigin
();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
m_pCloseItem
=
CCMenuItemImage
::
create
(
"CloseNormal.png"
,
"CloseSelected.png"
,
this
,
menu_selector
(
Hello
::
menuCloseCallback
));
m_pCloseItem
->
setPosition
(
ccp
(
origin
.
x
+
visibleSize
.
width
-
m_pCloseItem
->
getContentSize
().
width
/
2
,
origin
.
y
+
m_pCloseItem
->
getContentSize
().
height
/
2
));
// create menu, it's an autorelease object
CCMenu
*
pMenu
=
CCMenu
::
create
(
m_pCloseItem
,
NULL
);
pMenu
->
setPosition
(
CCPointZero
);
this
->
addChild
(
pMenu
,
1
);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
m_pLabel
=
CCLabelTTF
::
create
(
"Hello"
,
"Arial"
,
TITLE_FONT_SIZE
);
// position the label on the center of the screen
m_pLabel
->
setPosition
(
ccp
(
origin
.
x
+
visibleSize
.
width
/
2
,
origin
.
y
+
visibleSize
.
height
-
m_pLabel
->
getContentSize
().
height
));
// add the label as a child to this layer
this
->
addChild
(
m_pLabel
,
1
);
// add "HelloWorld" splash screen"
CCSprite
*
pSprite
=
CCSprite
::
create
(
"Player.png"
);
// position the sprite on the center of the screen
pSprite
->
setPosition
(
ccp
(
visibleSize
.
width
/
2
+
origin
.
x
,
visibleSize
.
height
/
2
+
origin
.
y
));
// add the sprite as a child to this layer
this
->
addChild
(
pSprite
,
0
);
return
true
;
}
void
Hello
::
menuCloseCallback
(
CCObject
*
pSender
)
{
#if
(
CC_TARGET_PLATFORM
==
CC_PLATFORM_WINRT
)
||
(
CC_TARGET_PLATFORM
==
CC_PLATFORM_WP8
)
CCMessageBox
(
"You pressed the close button. Windows Store Apps do not implement a close button."
,
"Alert"
);
#else
CCDirector
*
pDir
=
CCDirector
::
sharedDirector
();
if
(!
pDir
)
{
CCLOG
(
"Change Scene Error"
);
return
;
}
CCScene
*
s
=
HelloWorld
::
scene
();
float
t
=
12.3f
;
CCTransitionEaseScene
*
pReturnScene
=
(
CCTransitionEaseScene
*)
CCTransitionProgressRadialCCW
::
create
(
t
,
s
);
pDir
->
replaceScene
(
s
);
#if
(
CC_TARGET_PLATFORM
==
CC_PLATFORM_IOS
)
exit
(
0
);
#endif
#endif
}
#ifndef
__HELLOWORLD_SCENE_H__
#define
__HELLOWORLD_SCENE_H__
#include
"cocos2d.h"
#include
"Hello.h"
class
HelloWorld
:
public
cocos2d
::
CCLayer
{
public
:
// 初始化
virtual
bool
init
();
// 当 init 函数执行后, 默认进入OnEnter生命周期函数
void
onEnter
()
{
CCLOG
(
"HelloWorld onEnter"
);
CCLayer
::
onEnter
();
// 务必调用
}
// 当 A场景 利用 CCTransitionScene 动画切换到B场景时,过渡动画执行完成后
// 默认调用B场景的此 生命周期函数
void
onEnterTransitionDidFinish
()
{
CCLOG
(
"HelloWorld onEnterTransitionDidFinish"
);
CCLayer
::
onEnterTransitionDidFinish
();
}
// 当场景退出后, 默认调用此 生命周期函数
void
onExit
()
{
CCLOG
(
"HelloWorld onExit"
);
CCLayer
::
onExit
();
// 务必调用
}
// 返回一个特定的场景
static
cocos2d
::
CCScene
*
scene
();
void
changeScene
(
float
dt
);
void
menuCloseCallback
(
CCObject
*
pSender
);
// 创造一个HelloWorld 的函数
CREATE_FUNC
(
HelloWorld
);
};
#endif
两个场景通过按钮来实现切换。。。。。。。。。
#include
"HelloWorldScene.h"
#include
"../CocosDenshion/win32/MciPlayer.h"
#include
"sprite_nodes/CCAnimation.h"
#include
"AppMacros.h"
#include
"Hello.h"
#include
"menu_nodes/CCMenuItem.h"
using
namespace
cocos2d
;
using
namespace
CocosDenshion
;
USING_NS_CC
;
// 以上是Hello 场景 以下是HelloWorld的场景
// 返回一个特定的场景
CCScene
*
HelloWorld
::
scene
()
{
// 创建一个场景
CCScene
*
scene
=
CCScene
::
create
();
// 将 HelloWorld 层加到场景 之中
HelloWorld
*
layer
=
HelloWorld
::
create
();
scene
->
addChild
(
layer
);
return
scene
;
}
// 对HelloWorld的场景进行初始化
bool
HelloWorld
::
init
()
{
bool
bInitSucceed
=
false
;
if
(!
CCLayer
::
init
())
{
return
bInitSucceed
;
}
CCSize
visibleSize
=
CCDirector
::
sharedDirector
()->
getVisibleSize
();
CCPoint
origin
=
CCDirector
::
sharedDirector
()->
getVisibleOrigin
();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
cocos2d
::
CCMenuItemImage
*
pCloseItem
=
CCMenuItemImage
::
create
(
"CloseNormal.png"
,
"CloseSelected.png"
,
this
,
menu_selector
(
HelloWorld
::
menuCloseCallback
));
// 设置一样的坐标
pCloseItem
->
setPosition
(
ccp
(
100
,
180
));
// create menu, it's an autorelease object
CCMenu
*
pMenu
=
CCMenu
::
create
(
pCloseItem
,
NULL
);
pMenu
->
setPosition
(
CCPointZero
);
addChild
(
pMenu
,
1
);
return
true
;
}
void
HelloWorld
::
menuCloseCallback
(
CCObject
*
pSender
)
{
// 设置初始值
CCTransitionEaseScene
*
pReturnScene
=
NULL
;
float
t
=
1.2f
;
CCScene
*
s
=
Hello
::
scene
();
pReturnScene
=
(
CCTransitionEaseScene
*)
CCTransitionProgressRadialCCW
::
create
(
t
,
s
);
CCScene
*
pNewScene
=
(
CCScene
*)
pReturnScene
;
CCDirector
::
sharedDirector
()->
replaceScene
(
s
);
}