Exemplo n.º 1
0
void PlayLayer::fillVacancies()
{
    Size size = CCDirector::getInstance()->getWinSize();
    // Chỗ này nhìn có vẻ phức tạp nhưng chẳng có gì đâu, chỉ là khai báo con trỏ, cấp phát bộ nhớ cho nó thôi, dùng như mảng 1 chiều
    int *colEmptyInfo = (int *)malloc(sizeof(int) * m_width);
    memset((void *)colEmptyInfo, 0, sizeof(int) * m_width); // set giá trị là 0 hết
    
    // Rơi Sushi đang có xuống khoảng trống
    SushiSprite *sushi = NULL; // Tạo 1 con trỏ Sushi = Null,
    
    // Duyệt ma trận. Lưu ý ở đây 1 chút, chúng ta thường duyệt mảng 2 chiều theo thứ tự hàng, rồi đến cột, nhưng ở đây, hơi ngược 1 tý là cột rồi đến hàng. Và lưu ý rằng Cột 0, và Hàng 0 nằm ở vị trí bên Dưới phía Trái nhé. khi tạo ma trận ta cho viên Sushi 0,0 rơi xuống trước tiên mà
    
    for (int col = 0; col < m_width; col++) { // Duyệt theo cột, từ trái sang phải
        int removedSushiOfCol = 0;
        
        // Duyệt theo hàng, từ dưới lên trên
        for (int row = 0; row < m_height; row++) {
            sushi = m_matrix[row * m_width + col]; // Sushi tại vị trí hàng, cột
            if (NULL == sushi) { // Nếu rỗng
                removedSushiOfCol++; // Đếm số Sushi đã bị "ăn"
            } else { // Nếu ko rỗng
                if (removedSushiOfCol > 0) { // Nếu bên dưới nó có ô trống = số Sushi bị ăn
                    // Làm rơi xuống
                    int newRow = row - removedSushiOfCol; //Vị trí hàng mới ( giảm xuống )
                    // Trong ma trận ta bỏ sushi ở hàng row, và chuyển nó xuống dưới qua removedSushiOfCol ô rỗng
                    m_matrix[newRow * m_width + col] = sushi;
                    m_matrix[row * m_width + col] = NULL;
                    //Di chuyển
                    Point startPosition = sushi->getPosition();
                    Point endPosition = positionOfItem(newRow, col);
                    float speed = (startPosition.y - endPosition.y) / size.height; // Tốc độ
                    sushi->stopAllActions(); // Dừng mọi chuyển động trước đó của Sushi
                    sushi->runAction(MoveTo::create(speed, endPosition)); // Di chuyển = rơi xuống
                    // set hàng mới cho Sushi tại vị trí mới này
                    sushi->setRow(newRow);
                }
            }
        }
        
        // Mảng lưu trữ số lượng Sushi bị ăn tại vị trí Cột xác định
        colEmptyInfo[col] = removedSushiOfCol;
    }
    
    // 2. Tạo mới và làm rơi các Sushi xuống khoảng trống , lấp đầy ma trận
    for (int col = 0; col < m_width; col++) { // Duyệt cột từ trái sang phải
        
        // Duyệt hàng, chỉ xét từ vị trí rỗng trở lên
        for (int row = m_height - colEmptyInfo[col]; row < m_height; row++) {
            createAndDropSushi(row, col); // Tạo Sushi và rơi xuống vị trí Row, Col
        }
    }
    m_movingVertical = true;
    m_isAnimationing = true;
    
    free(colEmptyInfo); // Giải phóng con trỏ
}
Exemplo n.º 2
0
void GameLayer::fillVacancies()
{
	// reset moving direction flag
	m_movingVertical = true;
	m_isAnimationing = true;

	Size size = CCDirector::getInstance()->getWinSize();
	int *colEmptyInfo = (int *)malloc(sizeof(int) * m_width);
	memset((void *)colEmptyInfo, 0, sizeof(int) * m_width);

	// 1. drop exist monster
	Monster *monster = NULL;
	for (int col = 0; col < m_width; col++) {
		int removedMonterOfCol = 0;
		// from buttom to top
		for (int row = 0; row < m_height; row++) {
			monster = m_matrix[row * m_width + col];
			if (NULL == monster) {
				removedMonterOfCol++;
			}
			else {
				if (removedMonterOfCol > 0) {
					// evey item have its own drop distance
					int newRow = row - removedMonterOfCol;
					// switch in matrix
					m_matrix[newRow * m_width + col] = monster;
					m_matrix[row * m_width + col] = NULL;
					// move to new position
					Point startPosition = monster->getPosition();
					Point endPosition = positionOfItem(newRow, col);
					float speed = (startPosition.y - endPosition.y) / size.height;
					monster->stopAllActions();// must stop pre drop action
					monster->runAction(CCMoveTo::create(speed, endPosition));
					// set the new row to item
					monster->setRow(newRow);
				}
			}
		}

		// record empty info
		colEmptyInfo[col] = removedMonterOfCol;
	}

	// 2. create new item and drop down.
	for (int col = 0; col < m_width; col++) {
		for (int row = m_height - colEmptyInfo[col]; row < m_height; row++) {
			createAndDropMonster(row, col);
		}
	}

	free(colEmptyInfo);
}
Exemplo n.º 3
0
void GameLayer::createAndDropMonster(int row, int col)
{
	Size size = Director::getInstance()->getWinSize();

	Monster *monster = Monster::create(row, col);

	// create animation
	Point endPosition = positionOfItem(row, col);
	Point startPosition = Point(endPosition.x, endPosition.y + size.height / 2);
	monster->setPosition(startPosition);
	float speed = startPosition.y / (1.5 * size.height);
	monster->runAction(MoveTo::create(speed, endPosition));
	this->addChild(monster);

	m_matrix[row * m_width + col] = monster;
}
Exemplo n.º 4
0
void PlayLayer::fillVacancies()
{
    Size size = CCDirector::getInstance()->getWinSize();
    int *colEmptyInfo = (int *)malloc(sizeof(int) * m_width);
    memset((void *)colEmptyInfo, 0, sizeof(int) * m_width);
    
    // 1. drop exist sushi
    SushiSprite *sushi = NULL;
    for (int col = 0; col < m_width; col++) {
        int removedSushiOfCol = 0;
        // from buttom to top
        for (int row = 0; row < m_height; row++) {
            sushi = m_matrix[row * m_width + col];
            if (NULL == sushi) {
                removedSushiOfCol++;
            } else {
                if (removedSushiOfCol > 0) {
                    // evey item have its own drop distance
                    int newRow = row - removedSushiOfCol;
                    // switch in matrix
                    m_matrix[newRow * m_width + col] = sushi;
                    m_matrix[row * m_width + col] = NULL;
                    // move to new position
                    Point startPosition = sushi->getPosition();
                    Point endPosition = positionOfItem(newRow, col);
                    float speed = (startPosition.y - endPosition.y) / size.height;
                    sushi->stopAllActions();// must stop pre drop action
                    sushi->runAction(CCMoveTo::create(speed, endPosition));
                    // set the new row to item
                    sushi->setRow(newRow);
                }
            }
        }
        
        // record empty info
        colEmptyInfo[col] = removedSushiOfCol;
    }
    
    // 2. create new item and drop down.
    for (int col = 0; col < m_width; col++) {
        for (int row = m_height - colEmptyInfo[col]; row < m_height; row++) {
            createAndDropSushi(row, col);
        }
    }
    
    free(colEmptyInfo);
}
Exemplo n.º 5
0
//创建SushiSprite,并下落到指定位置
void PlayLayer::createAndDropSushi(int row, int col)
{
    Size size = Director::getInstance()->getWinSize();
    
    SushiSprite *sushi = SushiSprite::create(row, col);
    
	// 创建并执行下落动画
    Point endPosition = positionOfItem(row, col);
    Point startPosition = Point(endPosition.x, endPosition.y + size.height /2);
    sushi->setPosition(startPosition);
	float speed = startPosition.y / (2 * size.height);
    sushi->runAction(MoveTo::create(speed, endPosition));

	//将寿司添加到精灵表单里。注意,如果没有添加到精灵表单里,而是添加到层里的话,就不会得到性能的优化。
    spriteSheet->addChild(sushi);

	//给指定位置的数组赋值
    m_matrix[row * m_width + col] = sushi;
}
Exemplo n.º 6
0
void BallMap::createAndDropBall(int row, int col)
{
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    BallSprite *ball = BallSprite::create(row, col);
    
    // create animation
    CCPoint endPosition = positionOfItem(ball);
    CCPoint startPosition = CCPoint(endPosition.x, endPosition.y + size.height / 2);
    ball->setPosition(startPosition);
    float speed = startPosition.y / (2.0 * size.height);
    ball->runAction(CCSequence::create(
                    CCMoveTo::create(speed, endPosition),
                                       CCCallFuncN::create(this, callfuncN_selector(BallMap::setBallNoMoving)),
                                       NULL));
    
    // add to BatchNode
    m_joyPad->get_spriteNode()->addChild(ball);

    m_matrix[row * (int)m_size.width + col] = ball;

}
Exemplo n.º 7
0
void PlayLayer::createAndDropSushi(int row, int col)
{
    Size size = Director::getInstance()->getWinSize();
    
    SushiSprite *sushi = SushiSprite::create(row, col); // Gọi đến hàm tạo ra Sushi của lớp SushiSprite
    
    // Tạo animation, or Action?
    Point endPosition = positionOfItem(row, col); // Lấy tọa độ Point từ row, col truyền vào
    Point startPosition = Point(endPosition.x, endPosition.y + size.height / 2); // (y) Điểm đầu = Điểm Cuối + 1 khoảng nửa màn hình
    sushi->setPosition(startPosition);
    
    float speed = startPosition.y / (2 * size.height); // tốc độ
    
    sushi->runAction(MoveTo::create(speed, endPosition)); // Di chuyển rơi xuống
    
    // Thêm vào Spritesheet
    spriteSheet->addChild(sushi);
    
    
    // Thêm sushi vào mảng, chỗ này là cách quy mảng 2 chiều về mảng 1 chiều nhé, a[i][j] = a[i*COL + j]
    
    m_matrix[row * m_width + col] = sushi;
}
Exemplo n.º 8
0
void BallMap::fillVacancies()
{
    // reset moving direction flag
//    m_movingVertical = true;
    m_isAnimationing = true;
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    int *colEmptyInfo = (int *)malloc(sizeof(int) * m_size.width);
    memset((void *)colEmptyInfo, 0, sizeof(int) * m_size.width);
    
    // 1. drop exist Ball
    BallSprite *ball = NULL;
    for (int col = 0; col < m_size.width; col++) {
        int removedBallOfCol = 0;
        // from buttom to top
        for (int row = 0; row < m_size.height; row++)
        {
            ball = m_matrix[row * (int)m_size.width + col];
            
            if (NULL == ball)
            {
                removedBallOfCol++;
            }
            else
            {
                if (removedBallOfCol > 0)
                {
                    // evey item have its own drop distance
                    int newRow = row - removedBallOfCol;
                    // switch in matrix
                    m_matrix[newRow * (int)m_size.width + col] = ball;
                    m_matrix[row * (int)m_size.width + col] = NULL;
                    
                    // set the new row to item
                    ball->setRow(newRow);

                    // move to new position
                    CCPoint startPosition = ball->getPosition();
                    CCPoint endPosition = positionOfItem(ball);

                    float speed = (startPosition.y - endPosition.y) / size.height;
                    
                    ball->set_isMoving(true);
//                    ball->stopAllActions();// must stop pre drop action
                    ball->runAction(CCSequence::create(
                                    CCMoveTo::create(speed, endPosition),
                                                       CCCallFuncN::create(this, callfuncN_selector(BallMap::setBallNoMoving)),
                                                       NULL));
                }
            }
        }
        
        // record empty info
        colEmptyInfo[col] = removedBallOfCol;
    }
    
    // 2. create new item and drop down.
    for (int col = 0; col < m_size.width; col++)
    {
        for (int row = m_size.height - colEmptyInfo[col]; row < m_size.height; row++)
        {
            createAndDropBall(row, col);
        }
    }
    
    free(colEmptyInfo);
    colEmptyInfo = NULL;
    
}