❤️初识cocoas 2D -x

来源:互联网 发布:java编程什么系统好 编辑:程序博客网 时间:2024/06/09 20:39

**************************************************

❤️游戏引擎

超大的第三方库,主要由四个关键因素:

①导演 Director

②场景 Scene   

③图层 Layer

④精灵  Sprite


一个导演有多个场景

同一个时间点 一个界面只有一个场景  

一个场景有多个图层

一个图层有多个精灵


**************************************************

director->setOpenGLView(eglView);

相当于makeKeyvisible

一般在delegate中的写法是差不多的


**************************************************

❤️添加精灵步骤:


①创建精灵

Sprite::create("hero1.png")

②设置精灵的位置

Sprite->setPosition(

③将精灵添加到当前图层

this->addChild(

**************************************************

❤️刷新

设置刷新频率

director->setAnimationInterval(1.0 / 60);

令当前对象在每一帧刷新的时候  回调update方法

this->scheduleUpdate();

this->unscheduleUpdate();

**************************************************

❤️给精灵添加动作步骤:

①创建action

MoveTo* MoveTo::create(float duration, constPoint& position)

②精灵调用方法运行action

Action * Node::runAction(Action* action)

③精灵停止action

void Node::stopAllActions()



**************************************************

❤️获取屏幕点击事件

EventListenerTouchOneByOne

①创建一个点击事件监听器

EventListenerTouchOneByOne*EventListenerTouchOneByOne::create()

②创建监听响应函数

voidEventListenerTouchOneByOne::setSwallowTouches(bool needSwallow)


③添加监听响应函数

 pListener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);

④添加监听器到当前图层

_eventDispatcher调用

voidEventDispatcher::addEventListenerWithSceneGraphPriority(EventListener*listener, Node* node)


※注意:要重写touch对应的方法

bool Layer::onTouchBegan(Touch *touch, Event*unused_event)


**************************************************

❤️常用类和方法

※class CC_DLL Size


※class CC_DLL Point


※typedef Point Vect;

这个vect重载了+-运算符


※class CC_DLL Node : public Object

※class CC_DLL Layer : public Node

void Node::onEnter()

Point Node::convertTouchToNodeSpace(Touch *touch)const



※class CC_DLL Sprite : public NodeRGBA, publicTextureProtocol

Sprite* Sprite::create(const std::string&filename)

void Sprite::setPosition(const Point& pos)

void Sprite::addChild(Node* child)

const Point& Node::getPosition() const

void Node::removeFromParent()

void Node::setTag(int var)



※math.h

extern double fabs(double);


※class CC_DLL Array : public Object, publicClonable

long count() const


long Array::getIndexOfObject(Object* object)const




*****************************

❤️注意


※cocoas2D是MRC

所以要考虑内存泄露的问题


※原点在左下角








0 0
原创粉丝点击