Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-x_一个简单的Cocos2d-x程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// AppDelegate.h
//

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"

class  AppDelegate : private cocos2d::CCApplication
{
public:
    AppDelegate();
    virtual ~AppDelegate();
    virtual bool applicationDidFinishLaunching();
    virtual void applicationDidEnterBACkground();
    virtual void applicationWillEnterForeground();
};

#endif

//
// AppDelegate.cpp
//

#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_Cc;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

bool AppDelegate::applicationDidFinishLaunching() {
    // 初始化导演和OpenGL
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedopenGLView();

    // 导演拥有OpenGLView
    pDirector->setOpenGLView(pEGLView);
	
    // 是否显示FPS
    pDirector->setDisplayStats(true);

    // 设置FPS刷新的频率
    pDirector->setAnimationInterval(1.0 / 60);

    // 创建场景
    CCScene *pScene = HelloWorld::scene();

    // 导演调用场景开始运行
    pDirector->runWithScene(pScenE);

    return true;
}

// App进入后台
void AppDelegate::applicationDidEnterBACkground()
{
    CCDirector::sharedDirector()->stopAnimation();

    // 如果你使用简单的声音引擎,必须设置声音暂停
    // SimpleAudioENGIne::sharedENGIne()->pauseBACkgroundMusic();
}

// App进入前台
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->startAnimation();

    // 如果你使用简单的声音引擎,必须设置声音恢复
    // SimpleAudioENGIne::sharedENGIne()->resumeBACkgroundMusic();
}
//
// HelloWorldScene.h
//

#ifndef __HelLOWORLD_SCENE_H__
#define __HelLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();
    void menuCloseCallBACk(CCObject* pSender);
    void closeIconItem(CCObject *pSender);
    
    CREATE_FUNC(HelloWorld);
};

#endif
//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_Cc;

CCScene* HelloWorld::scene()
{
    // 初始化场景
    CCScene *scene = CCScene::create();
    
    // 初始化层
    HelloWorld *layer = HelloWorld::create();
    layer->setTouchEnabled(true);
    
    // 把层添加到场景
    scene->addChild(layer);

    // 返回场景
    return scene;
}

// 在“init”中初始化你的对象
bool HelloWorld::init()
{
    // 首先初始化基类
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibLeorigin();

    /////////////////////////////
    // 创建一个退出按钮,为菜单项
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png","CloseSELEcted.png",this,menu_SELEctor(HelloWorld::menuCloseCallBACk));
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2,origin.y + pCloseItem->getContentSize().height/2));
    // 创建一个菜单,把菜单项添加进去
    CCMenu* pMenu = CCMenu::create(pCloseItem,null);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu,1);

    /////////////////////////////
    // 创建一个标签
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",24);
    // 设置标签的位置
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->getContentSize().height));
    // 添加到层中
    this->addChild(pLabel,1);

    /////////////////////////////
    // 创建精灵,也就是背景图片
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");
    // 设置精灵位置
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
    // 添加到层中
    //this->addChild(pSprite,0);
    
    /////////////////////////////
    // 创建自己的精灵
    CCMenuItemImage *pIconItem = CCMenuItemImage::create("111.png","222.png",menu_SELEctor(HelloWorld::closeIconItem));
    pIconItem->setPosition(ccp(visibleSize.width/2.0,visibleSize.height/2.0));
    
    // 创建一个菜单,把菜单项添加进去
    CCMenu *pMenuIcon = CCMenu::create(pIconItem,null);
    pMenuIcon->setPosition(CCPointZero);
    this->addChild(pMenuIcon);
    
    return true;
}

void HelloWorld::closeIconItem(CCObject *pSender)
{
    CCLog("按钮回调函数!");
}

// 按钮回调函数
void HelloWorld::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::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//    exit(0);
#endif
#endif
}
//
// IconLayer.h
//

#ifndef HelloWorld_IconLayer_h
#define HelloWorld_IconLayer_h

#include "cocos2d.h"

class IconLayer : private cocos2d::CCLayer
{
public:
    //virtual bool init();
    static cocos2d::CCScene* scene();
    
};

#endif
//
// IconLayer.cpp
//

#include "IconLayer.h"

USING_NS_Cc;

cocos2d::CCScene * IconLayer::scene()
{
    CCScene *scene = CCScene::create();
    cocos2d::CCLayer *layer = IconLayer::create();
    scene->addChild(layer);
    return scene;
}

//bool IconLayer::init()
//{
//
//}


大佬总结

以上是大佬教程为你收集整理的Cocos2d-x_一个简单的Cocos2d-x程序全部内容,希望文章能够帮你解决Cocos2d-x_一个简单的Cocos2d-x程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。