不加香精的专栏

引擎cocos2d-x-3.1.1

一、 cocos创建一个项目,随便是lua还是cpp。这里用cpp演示

二、创建完成之后运行下项目

之后创建两个类。如下

TestLib.cpp文件

#include "TestLib.h"#include "People.h"Scene* TestLib::createScene(){// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = TestLib::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;}// on "init" you need to initialize your instancebool TestLib::init(){//////////////////////////////// 1. super init firstif ( !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 objectauto closeItem = MenuItemImage::create("CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(TestLib::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 objectauto menu = Menu::create(closeItem, NULL);menu->setPosition(Vec2::ZERO);this->addChild(menu, 1);/////////////////////////////// 3. add your codes below…// add a label shows "Hello World"// create and initialize a labellab_ = LabelTTF::create("Hello World", "Arial", 24);// position the label on the center of the screenlab_->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height – lab_->getContentSize().height));// add the label as a child to this layerthis->addChild(lab_, 1);// add "HelloWorld" splash screen"auto sprite = Sprite::create("HelloWorld.png");// position the sprite on the center of the screensprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));// add the sprite as a child to this layerthis->addChild(sprite, 0);return true;}void TestLib::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;#endifDirector::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);#endif*/lab_->setString("using test lib");People* sp = People::create();sp->setPosition(ccp(200, 300));this->addChild(sp);}

TestLib.h文件#ifndef __TestLib_H__#define __TestLib_H__#include "cocos2d.h"USING_NS_CC;class TestLib : public cocos2d::Layer{public:// there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::Scene* createScene();// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init();// a selector callbackvoid menuCloseCallback(cocos2d::Ref* pSender);// implement the "static create()" method manuallyCREATE_FUNC(TestLib);LabelTTF* lab_;};#endif // __HELLOWORLD_SCENE_H__

people.cpp文件

#include "People.h"People::People(){}People::~People(){}bool People::init(){if (!Node::init()){return false;}Sprite* sp = Sprite::create("CloseNormal.png");addChild(sp);return true;}

people.h文件

#ifndef __People_H__#define __People_H__#include "cocos2d.h"USING_NS_CC;class People : public cocos2d::Node{public:People();~People();virtual bool init();CREATE_FUNC(People);};#endif // __HELLOWORLD_SCENE_H__

三、 两个试验类已经创建完成,这个时候运行下程序。并且修改AppDelegate,原先是引到helloworld,改成引到testlib。程序运行通过就成,是为了保证试验类没有错误。

四、修改工程配置。

1 打开工程属性

2 找到配置属性-》常规-》配置类型 改成静态库

3 选择完成后点-》应用-》确定。之后就是生成lib

4 生成的文件在 testCpp\proj.win32\Debug.win32该目录下

为了一些琐事吵架,然后冷战,疯狂思念对方,最后和好。

不加香精的专栏

相关文章:

你感兴趣的文章:

标签云: