本文共 1614 字,大约阅读时间需要 5 分钟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //GameScene.h #include "cocos2d.h" class GameScene : public cocos2d::Layer { public : static cocos2d::Scene* createScene(); virtual bool init(); void menuCallback(cocos2d::Ref* pSender); CREATE_FUNC(GameScene); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 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(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if (!Layer::init()) //初始化父类 return false ; //添加一个图片精灵 Size visibleSize = Director::getInstance()->getVisibleSize(); //获取屏幕大小 auto 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方法 auto backItem = MenuItemImage::create( "CloseNormal.png" , "CloseSelected.png" , CC_CALLBACK_1(GameScene::menuCallback, this )); backItem->setPosition(Point(visibleSize.width-50, 50)); auto menu = Menu::create(backItem, NULL); menu->setPosition(Point::ZERO); this ->addChild(menu, 1); return true ; } //从游戏场景跳转到Hello场景 void GameScene::menuCallback(cocos2d::Ref* pSender) { //方法一 // auto scene = HelloWorld::createScene(); //创建一个新场景,因为原场景已删除 // Director::getInstance()->replaceScene(scene); //方法二 Director::getInstance()->popScene(); //原场景依然存在,直接出栈 } |
转载地址:http://ugdvo.baihongyu.com/