CCMenuItemImages positioning?
I am new to cocos2d and I'm trying to build a simple word game. I am stuck with some doubts.
I have arranged the letter's images at bottom of the screen. I've used CCMenuItemImage as buttons and arranged them开发者_运维百科. No way when I click the images individually, the letters should move to first position and second, and so on. For example:
- if there is some letters like b, u, t, x, y, z and if I click on any letters then it should move to some location like (200, 300) (first position) and then second position. Then third... so on.
How should I do it?? I'm a noob so explain properly. Please help!!
Thanks in advance!!
You would want to make your CCMenuItemImage have a selector to target a helper function to help move your CCMenuItemImage. Within the helper function you'd have your CCMenuItemImage moving code.
To move your CCMenuItemImage you can study/copy the cocos2d-iphone project example's ActionsTest.
You can use CCMoveTo or CCMoveBy to move your CCMenuItemImage.
The actions are defined with a duration and a target position. There are some differences between CCMoveTo and CCMoveBy. The significant one is CCMoveBy can be 'reversed' to get a reversed action for your action. Example below moves grossini to ccp(80,80) and the reverse actionByBack moves it back to it's original position. The following code can be found in the ActionsTest.m file of your cocos2d-iphone project. It defines the movement of 3 sprites, tamara, grossini and kathia.
CGSize s = [[CCDirector sharedDirector] winSize];
id actionTo = [CCMoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];
id actionBy = [CCMoveBy actionWithDuration:2 position: ccp(80,80)];
id actionByBack = [actionBy reverse];
[tamara runAction: actionTo];
[grossini runAction: [CCSequence actions:actionBy, actionByBack, nil]];
[kathia runAction:[ CCMoveTo actionWithDuration:1 position:ccp(40,40)]];
So in your case, if your CCMenuItem is called _alphabet1 you can use the actionBy action definition example to do the following
[_alphabet1 runAction: [CCSequence actions:actionBy, actionByBack, nil]];
That will send your _alphabet1 CCMenuItemImage to ccp(80,80) within 2 seconds and reverse back to the origin in 2 seconds.
If you just want to move your CCMenuItem to a position and not reverse, just use CCMoveTo.
精彩评论