Esempio n. 1
0
//
//用 m_srcSushi (源寿司)求得 m_destSushi
void PlayLayer::onTouchMoved(Touch *touch, Event *unused)
{
	//log("onTouchMoved");
	if (!m_srcSushi || !m_isTouchEnable)
		return;
	// 定义一些变量节省些代码
	auto row = m_srcSushi->getRow();
	auto col = m_srcSushi->getCol();
	auto location = touch->getLocation();
	auto halfSushiWidth = m_srcSushi->getContentSize().width / 2;
	auto halfSushiHeight = m_srcSushi->getContentSize().height / 2;


	// 计算源寿司四周 rect
	// 源寿司上边寿司面积
	auto upRect = Rect(m_srcSushi->getPositionX() - halfSushiWidth,
					   m_srcSushi->getPositionY() + halfSushiHeight,
					   m_srcSushi->getContentSize().width,
					   m_srcSushi->getContentSize().height);
	if (upRect.containsPoint(location))
	{
		row++;
		if (row < m_height)
		{
			m_destSushi = m_matrix[row*m_width + col];
		}
		m_movingVertical = true;//可能生成纵向的特殊寿司
		swapSushi();
		return;
	}

	auto downRect = Rect(m_srcSushi->getPositionX() - halfSushiWidth,
		m_srcSushi->getPositionY() - (halfSushiHeight*3),
		m_srcSushi->getContentSize().width,
		m_srcSushi->getContentSize().height
		);
	if (downRect.containsPoint(location))
	{
		row--;
		if (row >= 0)
		{
			m_destSushi = m_matrix[row*m_width + col];
		}
		m_movingVertical = true;
		swapSushi();
		return;
	}

	auto leftRect = Rect(m_srcSushi->getPositionX() - (halfSushiWidth * 3),
		m_srcSushi->getPositionY() - halfSushiHeight,
		m_srcSushi->getContentSize().width,
		m_srcSushi->getContentSize().height
		);
	if (leftRect.containsPoint(location))
	{
		col--;
		if (col >= 0)
		{
			m_destSushi = m_matrix[row*m_width + col];
		}
		m_movingVertical = false;//可能生成横向的特殊寿司
		swapSushi();
		return;
	}

	auto rightRect = Rect(m_srcSushi->getPositionX() + halfSushiWidth,
		m_srcSushi->getPositionY() - halfSushiHeight,
		m_srcSushi->getContentSize().width,
		m_srcSushi->getContentSize().height
		);
	
	if (rightRect.containsPoint(location))
	{
		col++;
		if (col < m_width)
		{
			m_destSushi = m_matrix[row*m_width + col];
		}
		m_movingVertical = false;
		swapSushi();
		return;
	}
}
Esempio n. 2
0
// Di chuyển Sushi
void PlayLayer::onTouchMoved(Touch *touch, Event *unused)
{
    if (!m_srcSushi || !m_isTouchEnable) { // Nếu Touch ra ngoài ( ko chứa Sushi nào ) hoặc ko được phép Touch
        return;
    }
    
    // Lấy vị trí Row, Col của Sushi của Sushi nguồn
    int row = m_srcSushi->getRow();
    int col = m_srcSushi->getCol();
    
    auto location = touch->getLocation();
    
    // 1/2 Chiều rộng và 1/2 chiều cao
    auto halfSushiWidth = m_srcSushi->getContentSize().width / 2;
    auto halfSushiHeight = m_srcSushi->getContentSize().height / 2;
    
    
    // Hướng di chuyển
    
    // Khung chữ nhật "phía trên Sushi nguồn"
    auto  upRect = Rect(m_srcSushi->getPositionX() - halfSushiWidth,
                        m_srcSushi->getPositionY() + halfSushiHeight,
                        m_srcSushi->getContentSize().width,
                        m_srcSushi->getContentSize().height);
    
    // Nếu khung này chứa điểm Touch, nghĩa là ta sẽ di chuyển 1 Sushi đi lên trên
    if (upRect.containsPoint(location)) {
        row++; // Hàng trên của Sushi Nguồn
        if (row < m_height) {
            m_destSushi = m_matrix[row * m_width + col]; // Lấy Sushi đích
        }
        m_movingVertical = true; // Di chuyển dọc = true
        swapSushi(); // Đảo 2 Sushi nguồn và đích cho ngau
        return; // Kết thúc hàm
    }
    
    // Khung chữ nhật "phía dưới Sushi nguồn", vì sao có halfSushiHeight * 3, bạn hãy vẽ hình ra cho dễ hình dung là nhớ là tọa độ gốc của hình Rectang là điểm Left - Bottom nhé, chiều cao + rộng sẽ dựng lên theo trục X ( sang phải ), và trục Y ( lên trên ). OK??
    auto  downRect = Rect(m_srcSushi->getPositionX() - halfSushiWidth,
                          m_srcSushi->getPositionY() - (halfSushiHeight * 3),
                          m_srcSushi->getContentSize().width,
                          m_srcSushi->getContentSize().height);
    
    // Chứa Touch
    if (downRect.containsPoint(location)) {
        row--; // Hàng dưới
        if (row >= 0) {
            m_destSushi = m_matrix[row * m_width + col];
        }
        m_movingVertical = true;
        swapSushi();
        return;
    }
    
    // Các bước di chuyển sang trái, sang phải, ở đoạn code bên dưới cũng giải thích như trên các bạn nhé
    auto  leftRect = Rect(m_srcSushi->getPositionX() - (halfSushiWidth * 3),
                          m_srcSushi->getPositionY() - halfSushiHeight,
                          m_srcSushi->getContentSize().width,
                          m_srcSushi->getContentSize().height);
    
    if (leftRect.containsPoint(location)) {
        col--;
        if (col >= 0) {
            m_destSushi = m_matrix[row * m_width + col];
        }
        m_movingVertical = false;
        swapSushi();
        return;
    }
    
    auto  rightRect = Rect(m_srcSushi->getPositionX() + halfSushiWidth,
                           m_srcSushi->getPositionY() - halfSushiHeight,
                           m_srcSushi->getContentSize().width,
                           m_srcSushi->getContentSize().height);
    
    if (rightRect.containsPoint(location)) {
        col++;
        if (col < m_width) {
            m_destSushi = m_matrix[row * m_width + col];
        }
        m_movingVertical = false;
        swapSushi();
        return;
    }
    
}