cocos2d-x3.0学习之场景切换代码

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer

{

public:

    // there‘s no ‘id‘ in cpp, so we recommend returning the class instance pointer

    static cocos2d::Scene* createScene();

    // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone

    virtual bool init();  

    

    // a selector callback

    void menuCloseCallback(cocos2d::Ref* pSender);

    

    // implement the "static create()" method manually

    CREATE_FUNC(HelloWorld);

void HelloWorld::menujumpCallback(cocos2d::Ref* pSender);

cocos2d::Scene*createfirst();

void HelloWorld::menureturn(cocos2d::Ref*pSender);

};

#endif // __HELLOWORLD_SCENE_H__

========================================================================================

HelloWorldScene.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()

{

    // ‘scene‘ is an autorelease object

    auto scene = Scene::create();

    

    // ‘layer‘ is an autorelease object

    auto layer = HelloWorld::create();

    // add layer as a child to scene

    scene->addChild(layer);

    // return the scene

    return scene;

}

void HelloWorld::menureturn(cocos2d::Ref*pSender)

{

auto ss=HelloWorld::createScene();

auto ts=TransitionZoomFlipY::create(1.0f,ss);

Director::getInstance()->replaceScene(ts);

}

Scene*HelloWorld::createfirst()

{

auto s=Scene::create();

auto layer=LayerColor::create(Color4B(100,10,128,128));

auto sprite=Sprite::create("bg.png");

layer->addChild(sprite);

auto l=LabelTTF::create("Welcome to play game","Arial",25);

auto d=Director::sharedDirector()->getWinSize();

auto width=d.width/2;

auto height=d.height/2;

l->setPosition(Point(width,height));

layer->addChild(l);

//create menu

auto returnmenu=MenuItemImage::create("leaf.png","icon.png",CC_CALLBACK_1(HelloWorld::menureturn , this));

returnmenu->setPosition(Point(400,400));

auto mm=Menu::create(returnmenu,NULL);

mm->setPosition(Point(400,400));

s->addChild(layer);

return s;

}

// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !Layer::init() )

    {

        return false;

    }

    

    Size visibleSize = Director::getInstance()->getVisibleSize();

    Vec2 origin = Director::getInstance()->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

    auto closeItem = MenuItemImage::create(

                                           "CloseNormal.png",

                                           "CloseSelected.png",

                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,

                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it‘s an autorelease object

    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Vec2::ZERO);

auto click=MenuItemImage::create("hagar1.png","hagar2.png",  CC_CALLBACK_1(HelloWorld::menujumpCallback , this));

click->setPosition(Point(100,100));

auto m=Menu::create(click,NULL);

m->setPosition(Vec2::ONE);

this->addChild(menu,1);

this->addChild(m,1);

    

    auto label = LabelTTF::create("cocos2d-x game", "Arial", 30);

    

    // position the label on the center of the screen

    label->setPosition(Vec2(origin.x + visibleSize.width/2,

                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer

    this->addChild(label, 1);

    // add "HelloWorld" splash screen"

    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen

    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer

    this->addChild(sprite, 0);

    return true;

}

void HelloWorld::menujumpCallback(Ref* pSender)

{

auto scene=HelloWorld::createfirst();

auto s2=TransitionZoomFlipY::create(1.0f,scene);

Director::getInstance()->replaceScene(s2);

}

//导演类的结束方法

void HelloWorld::menuCloseCallback(Ref* pSender)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)

MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");

    return;

#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}

时间: 2024-08-10 02:32:12

cocos2d-x3.0学习之场景切换代码的相关文章

05--简单场景切换与精灵创建

场景就是游戏的不同状态,如游戏菜单.游戏关卡等等.而场景的切换由导演类CCDirector来完成,通常我们使用replaceScene(CCScene *pScene)函数来切换场景.为了方便我使用上一节的项目,新建一个场景GameScene类和一个图层GameLayer类. //GameScene.h #pragma once #include "cocos2d.h" //使用VS类向导添加,需要手工添加这句 class GameScene : public cocos2d::CCS

JavaScript强化教程 -- cocosjs场景切换

场景切换 在main.js,将StartScene作为我们初始化运行的场景,代码如下:     cc.LoaderScene.preload(g_resources, function () {         cc.director.runScene(new PlayScene());     }, this); 在StartScene的开始菜单按钮的响应函数中加入场景切换代码.点击开始按钮切换场景进入到我们的PlayScene.代码如下:     var startItem = new cc

quick cocos2d x场景切换的生命周期函数调用学习

先上一个场景的基本模版: 1 local ModelScene = class("ModelScene", function() 2 return display.newScene("ModelScene") 3 end) 4 5 function ModelScene:ctor() 6 self.sceneName = "ModelScene" 7 -- 注册点击事件监听 8 self.layer = display.newLayer() 9

【Cocos2D学习】Cocos2d-x之CCScene场景切换效果的使用

我们要怎样切入游戏场景呢?其实就会要用到CCScene场景切换的知识,然后场景的切换效果,也即是场景的一种过渡效果...   Cocos2d-x提供了很多场景间切换的效果,可以方便大家使用 过渡类: //用CCTransition绕y轴翻转x轴过渡到FilipXpScene CCDirector::sharedDirector()->replaceScene( CCTransitionFlipX::create(2, pScene) ); CCTransitionFlipX::create(&quo

Cocos2d-X研究之3.0 场景切换特效汇总

Cocos2d-X研究之3.0 场景切换特效汇总 2014-08-05      0个评论    来源:游戏编程    收藏    我要投稿 cocos2d-x 3.0中场景切换特效比较多,而且游戏开发中也经常需要用到这些特效,来使场景切换时不至于那么干巴,遂这里汇总一下,开发中使用. 场景切换用到导演类Directory,大多数用的都是替换场景,当然也可以用出栈进栈的方式来进行场景的替换,这里以replaceScene来举例.3.0事件分发机制—触摸事件监听不明白的可以看下. 场景切换特效的应

cocos2dx 3.1从零学习(二)——菜单、场景切换、场景传值

回顾一下上一篇的内容,我们已经学会了创建一个新的场景scene,添加sprite和label到层中,掌握了定时事件schedule.我们可以顺利的写出打飞机的主场景框架. 上一篇的内容我练习了七个新场景,每一个场景都展示不同的东西,像背景定时切换.各种字体的随机颜色和位置等.每次要切换一个场景都要修改AppDelegate中的调用代码,非常的不方便查看,这一篇我们写场景的切换.每当我们创建一个新的场景的时候只要添加对应按钮到主界面,点击即可以切换过去查看对应的效果.这个有点类似官方提供的cppt

Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源代码分析

上一章我们分析了Scene与Layer相关类的源代码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源代码. 直接看TransitionScene的定义 class CC_DLL TransitionScene : public Scene { public: /** Orientation Type used by some transitions */ enum class Orientation { /// An horizontal or

Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源码分析

上一章我们分析了Scene与Layer相关类的源码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源码. 直接看TransitionScene的定义 class CC_DLL TransitionScene : public Scene { public: /** Orientation Type used by some transitions */ enum class Orientation { /// An horizontal orie

Cocos2dx 学习笔记整理----场景切换

据说Cocos2dx场景切换的方法有32种:cocos2dx 常见的32种切换场景的动画 无需一一求证,只需要知道切换场景需要怎么做就行了. 作为导演CCDirector,切换场景的事情当然归它管了. 切换场景的接口如下: ? 1 CCDirector::sharedDirector()->replaceScene(cocos2d:CCScene * pScene); 所以,我们只要把需要切换的场景实例传进去就可以了. ? 1 2 CCScene * pScene = GameMain::sce