How to switch Scene in Cocos2d-x - c++

I have 2 Scenes, "one scene is just an empty scene with a button" and "another scene has one TableView". I would like to switch back and forth, my code that is using to switch the scenes is
shown below:
scence1 to scene2
auto scene2 = Scene2::createScene();
Director::getInstance()->replaceScene(scene2);
scene2 to scene1
auto scene1 = Scene1::createScene();
Director::getInstance()->replaceScene(scene1);
I suspect that TableViewCell make the program crashes.
Here is my code to create TableView in scene2:
Size tvSize = Size(640.0, 640.0);
_tableview = TableView::create(this, tvSize);
_tableview->retain();
_tableview->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
_tableview->setPosition(Point(0.0 , 0.0 + ((640.0 - tvSize.height)/ 2.0)));
_tableview->setDataSource(this);
Remarkable: cocos2d-x v3.0 beta
UPDTE:
Thank you everyone for helping me, right now I be able to fix it,
The caused of the error is I add this line:
_eventDispatcher->addEventListenerWithFixedPriority(listener, 1);
and forgot to add this line when we finish using _eventDispatcher:
_eventDispatcher->removeEventListener(_touchListener);

Related

Do a Command After an Action Has Been Completed - SpriteKit

In SpriteKit, I have a simple action for my sprite:
sprite.size = CGSizeMake(40, 10);
sprite.physicsBody =[SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite.position = CGPointMake(65, 230);
SKAction *changeImage = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniSlide"]];
SKAction *wait = [SKAction waitForDuration:.1];
SKAction *changeImage2 = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniStick"]];
SKAction *sequence=[SKAction sequence:#[changeImage, wait, changeImage2]];
[sprite runAction:sequence];
It changes textures, waits, then changes textures again. This is so it ducks for a certain time period. After ducking I want it to change back to its original size. When I put the code for changing size after the runAction command, it doesn't perform the first sprite.size = CGSizeMake(40, 10), and doesn't duck at all, as it stays the same size as it was in the beginning. So I was wondering how to make an if statement that when if an action has been completed, it then changes the size of the sprite.
You can't do it with an if statement, but take a look at runAction:completion:. You use it like:
[sprite runAction:sequence completion:^{
// the action is complete, change size back
}];

How to do Android COCOS-2d Menuitem Animation?

Cocos2d-android - I have an animation which has 5 Frames. which will rolling in position. how to make the button rolling like a globe.
I don't know if it can be done like menuitem, but you can make a sprite with forever action(animation of yours 5 frames)and in ccTouchesBegan/ccTouchesEnded you add code to go to another scene or for do an another function after touching on your sprite.
`private CCSprite animatedButton; // Make your sprite visible at begin of your class/layer.`
`animatedButton = CCSprite.sprite("yourImage.png");
CCAnimation buttonAnimation = CCAnimation.animation("", 1f);
buttonAnimation.addFrame("1.png");
buttonAnimation.addFrame("2.png");
addChild(animatedButton, 1);
CCIntervalAction buttonAction = CCAnimate.action(duration, buttonAnimation, false);
animatedButton.runAction(CCRepeatForever.action(buttonAction));
`
Now should be your button(CCSprite) animating. I didnt tried code. And now you just find out in ccTouchesBegan or ccTouchesEnded if your button was touched. If yes, you could do what you want. :)
`if(animatedButton.getBoundingBox.contains(x,y)){
CCScene scene = GameLayer.scene();
CCDirector.sharedDirector().replaceScene(scene);
}`
X and y are coordinates of touch;
You can add animations to menuitem. Like this
CCMenuItem button=CCMenuItemImage.item(image, image, this, "label");
button.setScaleX((winSize.width/8.0f)/image.getTexture().getWidth());
button.setScaleY((winSize.height/8.0f)/image.getTexture().getHeight());
button.setPosition(winSize.width/2,winSize.height+50);
CCAction actionMove42=CCSequence.actions(CCDelayTime.action(0.3f), CCEaseBackOut.action(CCMoveTo.action(1.0f, CGPoint.ccp(0,0))));
button.runAction(actionMove42);

QSpacerItem changeSize attracts bug

I'm creating a calendar app and just finished the appointment view and edit. Basicly it's a QVBoxLayout with the viewlayout and editlayout in it and I hide and show either the view or edit using signals.
That works just fine, there is 1 issue: in the viewlayout I have a QSpacerItem for some extra spacing and in order to remove the space when viewing the edit-part I change its size.
Like so
if( show_view_hide_edit )
spacer->changeSize(1, 60, QSizePolicy::Expanding, QSizePolicy::Fixed );
else
spacer->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed );
This code works when the widget shows, and when I swap it to "edit". When I switch it back to view and then go back to edit the spacing is at the top of my screen. It shouldnt really be there. Image shows what happens. (link to image for size)
Without the construction code of your widget it is a little hard to guess, what is going on. Have you tried to insert / remove the SpacerItems with QBoxLayout::insertSpacing ( int index, int size ) and QLayout::removeItem ( QLayoutItem * item )?
And did you call QLayout::invalidate () after changing the size?

CCRepeatForever Error

I've got a problem in my current game.
I'm trying to move a sprite based on the movement of a other physic body, for a map. This is my code:
...
NSMutableArray *mapObjetcs = [[[NSMutableArray alloc]init]autorelease];
[mapObjetcs addObject:swordman];
[mapObjetcs addObject:icon];
CCCallFuncND* iconMap = [CCCallFuncND actionWithTarget:self selector:#selector(mapLoc:mapObj:) data:mapObjetcs];
CCSequence* iconMapSequence = [CCSequence actions:[CCDelayTime actionWithDuration:1.0f/60.0f], iconMap, nil];;
CCRepeatForever* iconRef = [CCRepeatForever actionWithAction:iconMapSequence];
[self runAction:iconRef];
}
-(void) mapLoc:(ccTime)delta mapObj:(NSMutableArray*)mapObj
{
GB2Sprite *swordmanTemp = (GB2Sprite*)[mapObj objectAtIndex:0];
CCSprite *iconTemp = (CCSprite*)[mapObj objectAtIndex:1];
CGPoint swordmanPos = [swordmanTemp ccPosition];
float pos = (swordmanPos.x/convFactor)+65;
iconTemp.position = ccp(pos, 290);
}
Every time i run the code with the CCRepeatForever the games freezes, if i run the code without the CCRepeatForever the game run grat but dont refresh the icon in map.
Can anybody help me??? Thanks
Its a problem with running CCRepeatForever on layer itself.. Ofcourse it will freeze the game.. You can try for alternate solution I guess.. Instead of using a separate CCRepeatForever loop, use the update method of your layer.. As its already doing same thing that you want to do with your own action..
Another solution is make a same CCRepeatForever for your icon sprite.. and in its CCCallFuncND take the position of other object....
Hope this helps.. Try yourself.. If it doesn't work.. I'll try 2 give you code... Don't run CCRepeatForever Loop on your layer itself.. :)
To avoud such actions you can simply schedule some method with needed interval. smth like
[self schedule: #selector(methodToBeCalled) interval: intervalInSeconds];
just don't forget to unschedule it later

How to enable setAliasTexParameters for CCMenuItem with Cocos2D

I'm having really hard time with my pixelated 2D game. I'm using Cocos2D framework and the menu is driving me insane. I can change the setAliasTexParameters parameter with Sprites, but not for CCMenuItemImage.
I believe it is possible with CCMenuItemSprite, but I red you can't set it straight with that either and I can't seem to even get the menu work with CCMenuItem Sprites. It works nicely with CCMenuItemImages thought.
Here's the blurry buttons:
http://cl.ly/0i1V3Y0e3u2k1t102H2a
For some unknown reason the left arrow button is sharp. I tried to change achor points, positions and paddings by 1 and 0.5 pixels but it didn't help. All the button sizes are even numbers.
This is my code for one of the MenuItems:
CCMenuItemImage *buttonMoveL = [CCMenuItemImage itemFromNormalImage:#"buttonMoveLeft.png" selectedImage:#"buttonMoveLeft2.png" block:^(id sender) {
// Do something
}
}];
And this is the Menu itself:
ActionsMenu = [CCMenu menuWithItems: buttonAttack, buttonMoveL, buttonMoveR, buttonDefend, nil];
ActionsMenu.position = ccp(240, 38);
[ActionsMenu alignItemsHorizontallyWithPadding:8];
All help is really much appreciated!
I got it working by using sprites in the menus. Here's my solution:
CCSprite *spriteDefend1 = [CCSprite spriteWithSpriteFrameName:#"buttonDefend1.png"];
CCSprite *spriteDefend2 = [CCSprite spriteWithSpriteFrameName:#"buttonDefend2.png"];
CCMenuItemImage *buttonDefend = [CCMenuItemSprite itemFromNormalSprite:spriteDefend1 selectedSprite:spriteDefend2 block:^(id sender) {
// Do something cool
}
}];