cocos2d-x3.2场景切换

前端之家收集整理的这篇文章主要介绍了cocos2d-x3.2场景切换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//GameScene.h
#include"cocos2d.h"
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
void menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};


?
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//GameScene.cpp
#include"GameScene.h"
#include"HelloWorldScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//添加一个图片精灵
SizevisibleSize=Director::getInstance()->getVisibleSize(); //获取屏幕大小
sprite1=Sprite::create( "game.png" );
sprite1->setAnchorPoint(Point::ZERO); //设置锚点在左下角,原锚点位置在中心
sprite1->setPosition(Point(0,0));
//sprite1->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this ->addChild(sprite1);
//添加一个菜单层,当点击按钮时调用menuCallback方法
backItem=MenuItemImage::create(
"CloseNormal.png" ,
"CloseSelected.png" CC_CALLBACK_1(GameScene::menuCallback, ));
backItem->setPosition(Point(visibleSize.width-50,50));
menu=Menu::create(backItem,NULL);
menu->setPosition(Point::ZERO);
->addChild(menu,1);
true ;
}
@H_990_404@ //从游戏场景跳转到Hello场景
GameScene::menuCallback(cocos2d::Ref*pSender)
{
//方法
//autoscene=HelloWorld::createScene();//创建一个新场景,因为原场景已删除
//Director::getInstance()->replaceScene(scene);
//方法
Director::getInstance()->popScene(); //原场景依然存在,直接出栈
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//GameScene.h
#include"cocos2d.h"
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
void menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//GameScene.cpp
#include"GameScene.h"
#include"HelloWorldScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//添加一个图片精灵
SizevisibleSize=Director::getInstance()->getVisibleSize(); //获取屏幕大小
sprite1=Sprite::create( "game.png" );
sprite1->setAnchorPoint(Point::ZERO); //设置锚点在左下角,原锚点位置在中心
this ->addChild(sprite1);
调用menuCallback方法
backItem=MenuItemImage::create(
"CloseNormal.png" "CloseSelected.png" ));
menu->setPosition(Point::ZERO);
true ;
}
@H_990_404@ //从游戏场景跳转到Hello场景
GameScene::menuCallback(cocos2d::Ref*pSender)
{
//方法
//autoscene=HelloWorld::createScene();//创建一个新场景,因为原场景已删除
//Director::getInstance()->replaceScene(scene);
//方法
Director::getInstance()->popScene(); //原场景依然存在,直接出栈
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//GameScene.h
#include"cocos2d.h"
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
void menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};


?
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//GameScene.cpp
#include"GameScene.h"
#include"HelloWorldScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//添加一个图片精灵
SizevisibleSize=Director::getInstance()->getVisibleSize(); //获取屏幕大小
sprite1=Sprite::create( "game.png" );
sprite1->setAnchorPoint(Point::ZERO); //设置锚点在左下角,原锚点位置在中心
this ->addChild(sprite1);
调用menuCallback方法
backItem=MenuItemImage::create(
"CloseNormal.png" "CloseSelected.png" ));
menu->setPosition(Point::ZERO);
true ;
}
@H_990_404@ //从游戏场景跳转到Hello场景
GameScene::menuCallback(cocos2d::Ref*pSender)
{
//方法
//autoscene=HelloWorld::createScene();//创建一个新场景,因为原场景已删除
//Director::getInstance()->replaceScene(scene);
//方法
Director::getInstance()->popScene(); //原场景依然存在,直接出栈
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//GameScene.h
#include"cocos2d.h"
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
void menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//GameScene.cpp
#include"GameScene.h"
#include"HelloWorldScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//添加一个图片精灵
SizevisibleSize=Director::getInstance()->getVisibleSize(); //获取屏幕大小
sprite1=Sprite::create( "game.png" );
sprite1->setAnchorPoint(Point::ZERO); //设置锚点在左下角,原锚点位置在中心
this ->addChild(sprite1);
调用menuCallback方法
backItem=MenuItemImage::create(
"CloseNormal.png" "CloseSelected.png" ));
menu->setPosition(Point::ZERO);
true ;
}
@H_990_404@ //从游戏场景跳转到Hello场景
GameScene::menuCallback(cocos2d::Ref*pSender)
{
//方法
//autoscene=HelloWorld::createScene();//创建一个新场景,因为原场景已删除
//Director::getInstance()->replaceScene(scene);
//方法
Director::getInstance()->popScene(); //原场景依然存在,直接出栈
}
原文链接:https://www.f2er.com/cocos2dx/344237.html

猜你在找的Cocos2d-x相关文章