码迷,mamicode.com
首页 > 其他好文 > 详细

华容道04---RoleSprite的设计

时间:2015-04-14 21:36:15      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x3.4   华容道   游戏   

RoleSprite负责显示角色,需要有一个数据的引用,注意他的锚点是在左下角的,为了便于计算。

代码:

RoleSprite.h

#ifndef _ROLE_SPRITE_H_
#define _ROLE_SPRITE_H_

#include "cocos2d.h"

class Role ;
class RoleSprite : public cocos2d::Node
{
public:
	static RoleSprite* create(Role * pRole ,int row,int col,const cocos2d::Rect & bounds,cocos2d::Node * pParent ,int zOrder);
public:
	RoleSprite(Role * role,int row,int col);
	~RoleSprite();

	virtual bool init() ;
private:
	Role * m_pRole;
	int m_row ;
	int m_col ;
};

#endif

RoleSprite.cpp

#include "RoleSprite.h"
#include "data/Role.h"
USING_NS_CC ;
RoleSprite * RoleSprite::create(Role * pRole ,int row,int col,const cocos2d::Rect & bounds,cocos2d::Node * pParent ,int zOrder)
{
	auto pRet = new RoleSprite(pRole,row,col);

	pRet->setContentSize(bounds.size) ;
	pRet->setPosition(bounds.origin) ;
	pRet->setAnchorPoint(Vec2(0,0)) ;
	if (pRet->init())
	{
		pParent->addChild(pRet,zOrder) ;
		pRet->autorelease();
	}
	else
	{
		delete pRet;
		return nullptr ;
	}

	return pRet ;
}

RoleSprite::RoleSprite(Role * role,int row,int col)
	:m_pRole(nullptr)
	,m_row(0)
	,m_col(0)
{
	m_pRole = role ;
	CC_SAFE_RETAIN(m_pRole) ; 
	m_row = row ;
	m_col = col ;
}

RoleSprite::~RoleSprite()
{
	CC_SAFE_RELEASE_NULL(m_pRole);
}

bool RoleSprite::init()
{
	Sprite* pSkin = Sprite::create(m_pRole->getImageName()->getCString()) ;
	pSkin->setPosition(this->getBoundingBox().size.width / 2,this->getBoundingBox().size.height / 2) ;
	addChild(pSkin) ;
	return true;
}



 

 

华容道04---RoleSprite的设计

标签:cocos2d-x3.4   华容道   游戏   

原文地址:http://blog.csdn.net/c_boy_lu/article/details/45047359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!