引擎cocos2d-x-3.1.1
一、 cocos创建一个项目。随便是lua还是cpp。这里用cpp演示
二、创建完毕之后执行下项目
之后创建两个类。例如以下
TestLib.cpp文件
#include "TestLib.h"
#include "People.h"
Scene* TestLib::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = TestLib::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool TestLib::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(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 object
auto 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 label
lab_ = LabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
lab_->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - lab_->getContentSize().height));
// add the label as a child to this layer
this->addChild(lab_, 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 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;
#endif
Director::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);
}
#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 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(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。程序执行通过就成,是为了保证试验类没有错误。
四、改动project配置。
1 打开project属性
2 找到配置属性-》常规-》配置类型 改成静态库
3 选择完毕后点-》应用-》确定。之后就是生成lib
4 生成的文件在 testCpp\proj.win32\Debug.win32该文件夹下
5 新建还有一个cppproject,这两个加上testlib.h和people.h。一共四个文件拷贝到新project的class文件夹下
使用lib库有两个方法,一个是直接把lib引到project文件夹下,在须要用到的地方include一下就能够。
代码是:#pragma comment(lib, "testCpp.lib")
另外一个方法是直接设置库路径,就不用把lib引到project文件夹下了。直接点开project属性,在配置属性-》连接器-》常规-》附加库文件夹中加入库
之后确认。
在配置属性-》连接器-》输入-》附加依赖项。如图填写
之后确定。执行project就能够了
说明 .obj的文件不须要像lib一样。
直接加入到project文件夹下直接用就能够