void GetTheOneLayer::layerInit(int matrix) { srand((unsigned)time(NULL));//add random seed int line = this->getContentSize().width / matrix; int ranX = rand() % matrix; int ranY = rand() % matrix; int ranColor = rand() % 5; int ranRed = rand() % 225 + 15; int ranGreen = rand() % 225 + 15; int ranBlue = rand() % 225 + 15; //log("rgb is %i , %i , %i", ranRed, ranGreen, ranBlue); for(int i = 0; i < matrix; i++) { for(int j = 0; j < matrix; j++) { if(i == ranX && j == ranY) { BlockSprite *block = BlockSprite::createBlock(true, ranRed, ranGreen, ranBlue, line - 3, line - 3, line * i, line * j); block->setPosX(i); block->setPosY(j); block->setRanColor(ranColor); this->addChild(block); }else { BlockSprite *block = BlockSprite::createBlock(false, ranRed, ranGreen, ranBlue, line - 3, line - 3, line * i, line * j); block->setPosX(i); block->setPosY(j); block->setRanColor(ranColor); this->addChild(block); } } } //add event listener auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(GetTheOneLayer::onTouchBegan, this, matrix, ranX, ranY); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); }