cocos2dx 如何製作一個使用世界座標系的精靈
阿新 • • 發佈:2019-01-22
#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; }