level selection menu with different levels similar to cut the rope game - cocos2d-iphone

I want a level selection screen in cocos2d as in the cut the rope game ie. without any sliding effects.
The site I found most helpful so far is "level selection screen with scrolling"
At the moment I am using the following for getting the menu items but not able to position them
for (int i=0; i<levelCount; i++) {
item1 = [CCMenuItemImage itemFromNormalImage:#"levButton.png" selectedImage:#"levButton.png" target:self selector:#selector(playnow)];
item1.tag = i;
[levelMenu addChild:item1];
}

If you need to position your items a grid manner, you could do it like this:
int realIndex = 0;
for (int x = 0; x < numberOfHorizontalItems; x++) {
for (int y = 0; y < numberOfVerticalItems; y++) {
item1 = [CCMenuItemImage itemFromNormalImage:#"levButton.png" selectedImage:#"levButton.png" target:self selector:#selector(playnow)];
item1.tag = realIndex;
item1.position = ccp(x * (item1.contentSize.width + 6),-(y * (item1.contentSize.height + 6)));
[levelMenu addChild:item1];
realIndex++;
}
}
levelMenu.position = ccp(0,0) // <-- Adjust coordinates.
The trick is to use 2 For loops, one for X and another Y positioning.

You can create a vertical line of buttons, as you are doing with
item1 = [CCMenuItemImage itemFromNormalImage:#"levButton.png"
selectedImage:#"levButton.png" target:self
selector:#selector(playnow)]
add all items in a CCMenu, add a vertical padding and position the menu
CCMenu *menu = [CCMenu menuWithItems:item1, item2, nil];
[menu alignItemsVerticallyWithPadding:20.f];
menu.position = ccp(x,y);
You can create 1 menu for each column, and add all button in that column in that menu.
You only need to position the CCMenu, positioning of the item inside the menu follow your rules (verticallyWithPadding for example)

Related

QTableWidget Checkbox get state and location

How to get state of all checkbox and get row and column of checked?
Onclick PushButton function.
QTableWidget *t = ui->tableWidget;
t->setRowCount(2);
t->setColumnCount(2);
QStringList tableHeader;
tableHeader<<"item01"<<"item02";
t->setHorizontalHeaderLabels(tableHeader);
for (int i = 0; i < t->rowCount(); i++) {
for (int j = 0; j < t->columnCount(); j++) {
QWidget *pWidget = new QWidget();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
QCheckBox *pCheckBox = new QCheckBox();
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pLayout->addWidget(pCheckBox);
pWidget->setLayout(pLayout);
t->setCellWidget(i, j, pWidget);
}
}
And when I clicked the button, I need get all selected elements with rows, columns of each.
void Widget::on_pushButton_clicked()
{
// Code here
// For example: Selected ["item01", 2]
}
I simply iterate over all cell widgets:
for (int i = 0; i < t->rowCount(); i++) {
for (int j = 0; j < t->columnCount(); j++) {
QWidget *pWidget = t->cellWidget(i, j);
QCheckBox *checkbox = pWidget->findChild<QCheckBox *>();
if (checkbox && checkbox->isChecked())
qDebug() << t->horizontalHeaderItem(j)->text() << i;
}
}
It's been awhile since I've programmed with Qt, but I believe there is not really good way of doing this. I've done all of these solutions with success.
1) Iterate through all the cell widgets like svlasov's answer says. This has some scalability issues.
2) Create hash maps where the pointer to the button is the key and the indices you want are the values. You can get which button was clicked with QObject::sender().
3) Store the indices you want as properties on the button when you create the buttons (see setProperty() in QObject's documentation). For example,
button->setProperty("x index", x);
In your slot, use QObject::sender() to get the pointer to the button and then call
button->property("x");
I've generally found the third option to be the cleanest and best performing.
Note that these answers also work for QTreeWidgets and QListWidgets.

how to create buttons with a for loop in cocos2d-iphone

Ok so I've been trying to get a level select scene working for Custom levels (levels that the user created), so i need to have a button for each created level, and i have been trying to do it with a for loop, but only one button shows up. here is my code:
-(void)AddLevelButtons{
NSArray *levels = [[NSArray alloc]init];
levels = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:CustomPath error:nil];
LevelCount = (int)[levels count];
for (int counter= 0;counter<LevelCount;counter++) {
CCButton *tempButton;
tempButton=[CCButton buttonWithTitle:#"" spriteFrame:[CCSpriteFrame frameWithImageNamed:#"othercategory.png"]];
tempButton.block= ^(id sender) {
GameScene *GameNode = [[GameScene alloc]initWithPlistName:[NSString stringWithFormat:#"CustomLevel_%d",counter]withObjIndex:counter];
[[CCDirector sharedDirector] replaceScene:LevelEditorNode.scene withTransition:[CCTransition transitionFadeWithDuration:.5f]];
};
tempButton.positionType=CCPositionTypeNormalized;
tempButton.position = ccp(counter+2/5,.5f);
[ScrollingNode addChild:tempButton];
}
}
I thought that maybe both buttons were created, and they just had the same position, but i set a breakpoint, and only one button is created.

Moving items up and down in a QListWidget in different view

I am trying to move the listwidget item programmatically. I am able to move the listwidget successfully if the move is in current view. If i try to move the list widget item across view ( i.e Using scrollbar ), move is not working as expected. i.e List widget item is not reflecting
code snip:
void func(int fromPage, int toPage)
{
QListWidget* expListWidget =i.next();
QListWidgetItem* widgetItem = expListWidget->takeItem(fromPage);
expListWidget->insertItem(toPage,widgetItem);
}
Here is an example how to move items up and down independently of where they are located:
QListWidget* lw1 = new QListWidget;
for (int i = 0; i < 500 ; i++)
{
QListWidgetItem* item = new QListWidgetItem(QString::number(i));
lw1->addItem(item);
}
//move from lower part to the top
QListWidgetItem* i = lw1->takeItem(400);
lw1->insertItem(0, i);
//move from the top to the lower part of the list
i = lw1->takeItem(1);
lw1->insertItem(400, i);

Can not scroll when add 2 CCScrollLayer in a layer

I'm using CCScrollLayer files at here CCScrollLayer for my cocos2d-x project (version 2.2.2).
I adapted them to make it can scroll vertically. And I got a problem: when I add two CCScrollLayer in a layer, I just can only scroll the last CCScrollLayer that are added.
Here is my code:
I add a layer that contains the two CCScrollLayer to a Scene
void ChooseMapScene::addSlidingLayers()
{
mChooseCharacterLayer = createChooseCharaterLayer();
mChooseCharacterLayer->setPosition(CCPointZero);
mChooseCharacterLayer->setTouchEnabled(true);
this->addChild(mChooseCharacterLayer, GR_FOREGROUND);
}
I add SlideCharacter1 and SlideCharacter2 in a layer (chooseCharacterLayer) but just the SlideCharacter2 can scroll
CCLayer* ChooseMapScene::createChooseCharaterLayer()
{
CCLayer* chooseCharacterLayer = CCLayer::create();
CCArray* characterArr1 = createCharactersArray(CHARACTER_LEFT_LAYER_POS);
CCArray* characterArr2 = createCharactersArray(CHARACTER_RIGHT_LAYER_POS);
mSlideCharacter1 = CCScrollLayerVertical::nodeWithLayers(characterArr1, 0);
chooseCharacterLayer->addChild(mSlideCharacter1, GR_FOREGROUND);
mSlideCharacter2 = CCScrollLayerVertical::nodeWithLayers(characterArr2, 0);
chooseCharacterLayer->addChild(mSlideCharacter2, GR_FOREGROUND);
// I add SlideCharacter1 and SlideCharacter2 in a layer (chooseCharacterLayer) but just the SlideCharacter2 can scroll
return chooseCharacterLayer;
}
.
CCArray* ChooseMapScene::createCharactersArray(CCPoint pPos)
{
CCArray* characterArr = CCArray::createWithCapacity(NUMBER_CHARACTERS);
for (int i = 1; i <= NUMBER_CHARACTERS; ++i)
{
CCLayer* characterLayer = CCLayer::create();
CCSprite* character = CCSprite::create(CCString::createWithFormat("Images/Game/Object/c%i.png", i)->getCString());
character->setPosition(pPos);
characterLayer->addChild(character, GR_FOREGROUND, i);
characterArr->addObject(characterLayer);
}
return characterArr;
}
You can manually call SlideCharacter1's touch methods (began, cancelled, moved, ended) from SlideCharacter2's touch methods to simulate the touches

How to change UITabBarItem position?

I need to change UITabBarItem's image and text position when it's highlighted. Is it possible?
It's look like there is no way to change UITabBarItem's image's position. I just corrected my selected image to have about 15 pixels in button and corrected UITabBarItem's title's position on 15 in tabBar:didSelectItem: method of my custom UITabBarController:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
// If there is another selected item set default properties to it.
for (int counter = 0; counter < [self.tabBar.items count]; counter++) {
UITabBarItem *currentSelectedItem = [self.tabBar.items objectAtIndex:counter];
[currentSelectedItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
}
// Set selected item.
UITabBarItem *selectedItem = item;
[selectedItem setTitlePositionAdjustment:UIOffsetMake(0, -15)];
}