#ifndef _CARD_H_ #define _CARD_H_ #include "cocos2d.h" USING_NS_CC ; class CardSprite : public Sprite { public : //这里number是卡片数字,wight和height是宽和高,cardSpriteX和CardSpriteY是卡片坐标 static CardSprite * createCardSprite ( int number , int wight , int height, float CardSpriteX , floatCardSpriteY); virtual bool init(); CREATE_FUNC (CardSprite ); //获取数据 int getNumber (); //设置数据 void setNumber ( int num ); private : //显示的数字 int number ; //初始化 void enemyInit ( int number , int wight , int height, float CardSpriteX , float CardSpriteY ); //显示数字的控件 LabelTTF * labelCardNumber ; //背景 LayerColor * colorBackground ; };
#include "Card.h" USING_NS_CC ; CardSprite * CardSprite :: createCardSprite( int number , int wight, int height , float CardSpriteX , float CardSpriteY) { //这里是创建一个自定义精灵类,格式很规范化 CardSprite * enemy = new CardSprite (); if ( enemy && enemy-> init ()) { enemy ->autorelease (); enemy ->enemyInit ( number, wight ,height , CardSpriteX, CardSpriteY ); return enemy ; } CC_SAFE_DELETE (enemy ); return NULL ; } //这里也是规范化 bool CardSprite:: init () { if (! Sprite ::init ()) { return false ; } return true ; } //获取数据 int CardSprite:: getNumber () { return number ; } //设置数据 void CardSprite:: setNumber (int num) { number = num; //更新显示的数字 if ( num > 0) { labelCardNumber ->setString ( __String:: createWithFormat ("%i" , number)-> getCString ()); } else { labelCardNumber ->setString ( ""); } } //初始化 void CardSprite:: enemyInit (int number, int wight , int height , float CardSpriteX, float CardSpriteY ) { //初始化数字 this ->number = number ; //加入卡片背景颜色 colorBackground = LayerColor :: create( Color4B (200 , 190, 180 ,255 ), wight- 15 ,height - 15); colorBackground ->setPosition ( Point( CardSpriteX ,CardSpriteY )); //判断如果大于0就显示,否则就不显示 if ( number > 0) { //加入字体 labelCardNumber = LabelTTF :: create( __String ::createWithFormat ( "%i", number )->getCString (), "Consolas", 100 ); labelCardNumber ->setPosition ( Point( colorBackground ->getContentSize (). width/ 2 , colorBackground -> getContentSize(). height /2 )); labelCardNumber ->setTag ( 8); colorBackground ->addChild ( labelCardNumber); } else { //加入字体 labelCardNumber = LabelTTF:: create ("" , "Consolas", 100 ); labelCardNumber ->setPosition ( Point( colorBackground ->getContentSize (). width/ 2 , colorBackground -> getContentSize(). height /2 )); labelCardNumber ->setTag ( 8); colorBackground ->addChild ( labelCardNumber); } this ->addChild ( colorBackground); }
CardSprite *cardArr[4][4] //用来储存卡片
//根据屏幕大小创建卡片
void createCardSprite ( Size size );
//根据屏幕大小创建卡片 void GameScene:: createCardSprite (Size size) { //求出单元格的宽和高 int cardSize = ( size. height - 36) / 4 ; //绘制出4X4的单元格 for ( int i = 0 ; i < 4; i++) { for ( int j = 0 ; j < 4; j++) { //需要屏幕分辨率适配 CardSprite * card = CardSprite:: createCardSprite (2 , cardSize , cardSize , cardSize * i+ 80 ,cardSize *j + 20); cardArr [i ][ j] = card ; addChild (card ); } } }
//加入游戏背景 auto layerColorBG = LayerColor ::create ( Color4B( 180 , 170 , 160 , 255 )); this ->addChild ( layerColorBG); //创建4X4卡片 createCardSprite (visibleSize );
【来写个2048吧】—— 创建卡片类以及添加,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wxk_bigworld/article/details/24983589