cocos2d-x 3.2 之 2048 —— 第三篇,cocos2d-x2048

來源:互聯網
上載者:User

cocos2d-x 3.2 之 2048 —— 第三篇,cocos2d-x2048

***************************************轉載請註明出處:http://blog.csdn.net/lttree******************************************



OK,抓緊更新吧。

長話短說,直奔主題,第三篇:

——數字塊類的建立


數字塊是神馬?

——就是那個,你滑動的數字,還有隨機出現的數字。

我們,先建立一個類NumberTiled,繼承自Node:

NumberTiled.h:

#ifndef __test2048_NumberTiled_H__#define __test2048_NumberTiled_H__#include "cocos2d.h"USING_NS_CC;class NumberTiled : public Node{public:// 儲存行列位置 及 該位置的數字值int m_row,m_col;int m_number;// 移動到r,c 這個位置void moveTo( int r , int c );CREATE_FUNC(NumberTiled);bool init();};#endif


NumberTiled.cpp:

#include "NumberTiled.h"#include "GameDefine.h"USING_NS_CC;bool NumberTiled::init(){if( !Node::init() ){return false;}// 背景層auto bk = LayerColor::create( Color4B(200,200,200,255),GAME_TILED_WIDTH,GAME_TILED_HEIGHT );this->addChild(bk);// 數字層——隨機出一個數字,若數字等於7 則產生4否則產生2int n = rand()%10;this -> m_number = n==7?4:2;// 根據數位值,賦予不同顏色switch ( this -> m_number ){case 2:bk -> setColor(Color3B(230,220,210));break;case 4:bk -> setColor(Color3B(230,210,190));break;default:break;}// 建立字型,並將本塊的數字顯現上去TTFConfig config("HelloKitty.ttf",40);auto label = Label::createWithTTF(config, StringUtils::format("%d",this->m_number));  label -> setPosition(Point(GAME_TILED_WIDTH/2,GAME_TILED_HEIGHT/2));label -> setColor(Color3B::BLACK);bk -> addChild( label );return true;}void NumberTiled::moveTo( int r , int c ){this -> m_row = r;this -> m_col = c;this -> setPosition( Point( m_col * GAME_TILED_WIDTH + GAME_TILED_BOARD_WIDTH * (m_col + 1),m_row * GAME_TILED_HEIGHT + GAME_TILED_BOARD_WIDTH * (m_row+1) ));}

好的,這個類基礎的功能完成了,

就是初始化 和 移動(出現) 都某個位置。


接下來,要在我們的介面上隨機展現出來呀~

轉到遊戲介面,

上一篇文章中,我們添加了 邏輯數組map , 

現在,我們還要添加一個Vector(集合)來儲存所有的塊,

GameScene.h:

Vector<NumberTiled *> m_allTiled;


然後,在init中,我們建立了出來了格子,接下來,有了數字塊以後,

那就要隨機產生一個塊咯,

添加一個方法——newNumberTiled

這個函數作用就是,產生一個新塊:

void GameScene::newNumberTiled(){// 建立一個 數字塊的執行個體auto tiled = NumberTiled::create();// 找到有幾個閒置位置int freeCount = 16 - m_allTiled.size();int num = rand() % freeCount;int row = 0,col = 0,count = 0;bool find = false;// 產生數字,一定在空白地區for( ; row < GAME_ROWS ; ++row ){for( col = 0 ; col < GAME_COLS ; ++col ){if( map[row][col] == 0 ){// 記錄空白地區的數量++count;if( count >= num ){find = true;break;}}}if( find ){break;}}// 注意在colorBack中添加tiled喲colorBack -> addChild( tiled );tiled -> moveTo ( row , col );m_allTiled.pushBack(tiled);map[ row ][ col ] = m_allTiled.getIndex(tiled)+1;}

恩,具體解釋,在代碼中都已給出了。

說明一下怎麼在空的位置隨機產生塊:

首先,擷取有多少空位置,

然後,隨機產生一個數,小於空位置總數

接著設定count為0,

從第一個位置到最後一個位置遍曆,

遇到空位置,count+1,

若大於產生的隨機數,就在該位置放置數字塊。



現在,可以運行一下,看一看效果了:




Ok,這次就到這裡,

下次內容就是,對觸控螢幕幕的處理~

敬請期待喲~



本篇文章代碼:http://pan.baidu.com/s/1sjHLNp7



***************************************轉載請註明出處:http://blog.csdn.net/lttree******************************************




剛下了cocos2d-x-32,在vs2013上編譯時間出現了下面一些問題,怎解決新手入門表示各種頭痛T_T

我也試過裝cocos2d-x,不過當時好像只支援到2012就放棄了,不過你這個因為cocos2d-x也只是提供了幾個庫而已,無法解析是因為你在編譯的時候沒有在VS裡將cocos2d-x的庫給串連進來,所以無法解析這些符號,具體的設定應該是“右鍵項目屬性 → C/C++ → 常規 ”裡面把庫檔案添加進來,百度上一大堆教你如何添加庫的教程
 
cocos2d-x-32在VS2010產生解決方案失敗?

喲,我部落格上正好有此一條博文。。CSDN windvix的專欄。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.