cocos2dx源码:相框PhotoFrame

前端之家收集整理的这篇文章主要介绍了cocos2dx源码:相框PhotoFrame前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件PhotoFrame.h

#ifndef _PHOTOFRAME_H_
#define _PHOTOFRAME_H_

#include "cocos2d.h"
#include "GmbsCocos.h"
#include "extensions/cocos-ext.h"

using namespace cocos2d::ui;

NS_CC_BEGIN

class PhotoFrame : public LayerColor
{
public:
    ~PhotoFrame();

    static PhotoFrame* create(const char* filename,const Color4B& color = Color4B(255,255,0));
    virtual bool init(const char* filename);

    void decorate(Node* photo,float horiz = 0,float vert = 0);

    void inflate(float wInf,float hInf);

protected:
    ui::Scale9Sprite* m_frameSprite;

};

NS_CC_END

#endif

cpp文件PhotoFrame.cpp

#include "PhotoFrame.h"

NS_CC_BEGIN

PhotoFrame::~PhotoFrame()
{

}

PhotoFrame* PhotoFrame::create(const char* filename,const Color4B& color)
{
    PhotoFrame* pobLayer = new PhotoFrame();
    if (pobLayer && pobLayer->initWithColor(color) && pobLayer->init(filename))
    {
        pobLayer->autorelease();
        return pobLayer;
    }
    else
    {
        CC_SAFE_DELETE(pobLayer);
        return NULL;
    }
}

bool PhotoFrame::init(const char* filename)
{
    m_frameSprite =  ui::Scale9Sprite::create(filename);
    this->addChild(m_frameSprite);

    return true;
}

void PhotoFrame::decorate(Node* photo,float wInf,float hInf)
{
    photo->addChild(this);

    Size size = photo->getContentSize();
    size.width += wInf;
    size.height += hInf;
    this->setContentSize(size);
    m_frameSprite->setContentSize(size);

    GmbsPoint pt;
    pt.reset(m_frameSprite);
    pt.xMiddleAlign(this).yMiddleAlign(this);
    m_frameSprite->setPosition(pt);

    pt.reset(this);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    this->setPosition(pt);
}

void PhotoFrame::inflate(float wInf,float hInf)
{
    Size size = this->getContentSize();
    size.width += wInf;
    size.height += hInf;
    this->setContentSize(size);
    m_frameSprite->setContentSize(size);

    GmbsPoint pt;
    Node* photo = getParent();
    pt.reset(m_frameSprite);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    m_frameSprite->setPosition(pt);

    pt.reset(this);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    this->setPosition(pt);
}

NS_CC_END
原文链接:https://www.f2er.com/cocos2dx/339336.html

猜你在找的Cocos2d-x相关文章