How to implement an 'menu' that seems like stuck to it's position? Here with stuck I meant to say either scene
is moving or still it remain on it's on position. Which gives feel like it's on it's place. I am implementing it by moving it with scrolling scene, it works but behave absurdly, 'sometimes'.
Here is the code how am I creating menu
:
resetPosition =[CCMenuItemImage itemFromNormalImage:@"position.png"
selectedImage:@"position_over.png"
disabledImage:@"disabled.png"
target:self
selector:@selector(reset)];
resetPosition.position =ccp(400, 300);
myresetMenu = [CCMenu menuWithItems:resetPosition, nil];
myresetMenu.position = ccp(0,0);
[[self parent] addChild:myresetMenu z:10];
[resetPosition setIsEnabled:NO];
And now code for moving the scene(when user trying to scroll the scene). Also I have moved the menu with scene
, so it gives the effect that the menu is still with the scene
:
if([touchArray count]==1)//scroll
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
moveLocation1 = [[CCDirector sharedDirector] conver开发者_运维知识库tToGL:location];
float diffX = beginLocation1.x - moveLocation1.x;
float diffY = beginLocation1.y - moveLocation1.y;
//NAVIGATION TOWARDS X AND Y WhenEver and how ever you want
if (abs(diffX) > abs(diffY))
{
CCLOG(@"yScrlFlag=%d",yScrlFlag);
if(diffX > 0)
{
xScrlFlag=1;
[self.parent runAction:[CCMoveTo actionWithDuration:round(-(-3112-self.parent.position.x)/650)
position:ccp((-3112-self.position.x),self.parent.position.y)]];
[resetPosition setIsEnabled:YES];
[resetPosition runAction:[CCMoveTo actionWithDuration:round(-(-3112-self.parent.position.x)/650)
position:ccp((3112+self.position.x+400),resetPosition.position.y)]];
}
else
{
xScrlFlag=0;
[self.parent runAction:[CCMoveTo actionWithDuration:(-self.parent.position.x/650)
position:ccp(0,self.parent.position.y)]];
//[resetPosition setIsEnabled:YES];
[resetPosition runAction:[CCMoveTo actionWithDuration:(-self.parent.position.x/650)
position:ccp(400,resetPosition.position.y)]];
}
}
else
{
if(diffY < 0)
{
yScrlFlag=1;
CCLOG(@"\n nodePosition.x=%f \n nodePosition.y=%f",nodePosition.x,nodePosition.y);
[self.parent runAction:[CCMoveTo actionWithDuration:(-(-300-self.parent.position.y)/650)
position:ccp(self.parent.position.x,(-self.position.y))]];
//[self.parent runAction:[CCMoveBy actionWithDuration:(-(-300-self.parent.position.y)/650)
// position:ccp(self.parent.position.x, -diffY)]];
[resetPosition setIsEnabled:YES];
//[resetPosition runAction:[CCMoveBy actionWithDuration:round(-(-300-self.parent.position.x)/650)
// position:ccp(resetPosition.position.x, (self.parent.position.y))]];
}
else
{
yScrlFlag=0;
[self.parent runAction:[CCMoveTo actionWithDuration:(-(-300-self.parent.position.y)/650)
position:ccp(self.parent.position.x,0)]];
//[resetPosition runAction:[CCMoveTo actionWithDuration:round(-(-300-self.parent.position.x)/650)
// position:ccp(resetPosition.position.x,300)]];
}
}
}
You don't specify how you create your menus and what are you doing with them, so my answer is pretty general...
The right approach to this, for me is having a UI layer where you put all your UI stuff (menus, buttons, etc.) and that you treat differently from your other, game layers. In other words, you give your UI layer a fixed position and then don't apply any actions or any other kind of transformations to it. It should stay pretty static this way.
Cocos2d offers its own classes to manage CCMenu
, CCMenuItem
, and CCLabel
that allow you to build you UI easily. But, if you prefer, you could also try and integrate UIKit
objects. Have a look at CCUIViewWrapper
, if you really need it.
EDIT:
The idea about having a UI layer is the following in more detail:
you have a root layer acting as a container of all your layers;
one of those layers is the "game layer", which is pretty much what you have now;
another is the "ui layer";
touches and any kind of transformations are only applied to the "game layer", which move within the root layer without affecting the "ui layer".
There is a nice tutorial about this in "Learn Game Development with Cocos2D" by Steffen Itterheim. This book is fantastic if you want to learn several nifty approaches to your cocos2d game. The tutorial I am talking about is available in code form on Steffen's site. You can dowload it from this page and look into chapter 5 "Scene and Layers" example, specifically starting from ScenesAndLayers04, where you will find the 'MultiLayerScene.h/m` classes.
I had a bit of trouble with this a while back. I had been trying to have UI elements that did not get lost when the camera moved. What I ended up doing was adding a CCParallaxNode as a child and setting the parallax ratio to ccp(0.0, 0.0) on everything I put on it. That way the scene can move around but my UI layer stays in place. I'm not sure if that's the best way to do it, but I'm a dirty hack kind of programmer :)
@sergio has the right idea.
With your code though. Have it in a layer all to itself and then on the touch action move it like you are already and just reset its position when you take the action to bring the menu back up again.
精彩评论