2016-01-05 20 views
-2

我正在做滑动益智游戏,我需要帮助如何切片图像,并使更多的精灵图像这个图像安排降序玩家将安排到正确的位置。如何分割图像的高度和宽度均匀4X4 = 16件?

知道如何在cocos2d-x 3.2中切片图像有人请帮我解决这个问题。

谢谢老兄。

回答

1

可以使用setTextureRect()切片在cocos2d-x的精灵。例如,下面是16段的代码,你可以换你的电话号码:

for (int i = 15; i >= 0; i--) 
{ 
    vecSprites[i]->setPosition((i%4) * visibleSize.width/4, (((int)i/4) + 1) * visibleSize.height/4); 
    vecSprites[i]->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); 
    this->addChild(vecSprites[i]); 
} 

结果:

Size visibleSize = Director::getInstance()->getVisibleSize(); 
vector<Sprite*> vecSprites; 
for (int i = 0; i <= 15; i++) 
{ 
    Sprite* segment = Sprite::create("example.png"); 
    float segmentWidth = segment->getBoundingBox().size.width/4; 
    float segmentHeight = segment->getBoundingBox().size.height/4; 
    float originX = (i % 4) * segmentWidth; 
    float originY = ((int)i/4) * segmentHeight; 
    segment->setTextureRect(Rect(originX, originY, segmentWidth, segmentHeight)); 
    vecSprites.push_back(segment); 
} 

以相反的顺序在屏幕上精灵的显示段enter image description here
当然,您可以随机显示它们。我希望这能帮到您。

+0

感谢您的代码工作。 – Guru

+0

比谢谢更好接受答案) – alc77

+0

是的,这是更好的多一个问题,如何触摸移动此段? – Guru