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] convertToGL: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)]];
}
}
}
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 🙂