前端之家收集整理的这篇文章主要介绍了
cocos2dx 如何制作一个使用世界坐标系的精灵,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- #pragma once
-
- #include "cocos2d.h"
- using namespace cocos2d;
-
- class TStopSprite : public Sprite
- {
- public:
- TStopSprite();
- virtual ~TStopSprite();
- CREATE_FUNC(TStopSprite);
- virtual bool init();
- virtual void update(float fDelay);
- virtual void setPosition(const Vec2& pos);
-
- private:
- Point m_worldPoint; // 修正坐标
- };
- #include "TStopSprite.h"
-
- using namespace cocos2d;
-
- TStopSprite::TStopSprite()
- {
- m_worldPoint = Point(0,0);
- }
-
- TStopSprite::~TStopSprite()
- {
-
- }
-
- bool TStopSprite::init()
- {
- if (!Sprite::init())
- {
- return false;
- }
-
-
- this->scheduleUpdate();
-
- return true;
- }
-
- void TStopSprite::update(float fDelay)
- {
- if (nullptr == this->getParent())
- {
- return;
- }
-
- // 获取baba
- auto parent = this->getParent();
- auto parentPos = parent->convertToWorldSpace(Point(0,0));
- Sprite::setPosition(m_worldPoint - parentPos);
- }
-
- void TStopSprite::setPosition(const Vec2& pos)
- {
- m_worldPoint = pos;
- }