free4machine
Member
PM this user
Hi,
I want to make a sample animation:
there is a button at the bottom of the screen, a small menu will appear when this button is pressed.
And there is an icon at the bottom of the small menu, the small menu will disappear when this icon is pressed.
The button, small menu, icon, all are instance of CCMenuItemImage .
Question: the small menu does not disappear when the icon is pressed. it seems that the icon does not respond for touch events.
Any suggestions or help will be appreciated
Sorry for my poor english.
here is the code:
CCMenuItemImage *bg = [CCMenuItemImage itemFromNormalImage:#"b1.png" selectedImage:#"b2.png" ];
CCMenuItemImage *button = [CCMenuItemImage itemFromNormalImage:#"button.png" selectedImage:#"button2.png" target: self selector:#selector(showSideMenu:)];
CCMenuItemImage *sideMenu = [CCMenuItemImage itemFromNormalImage:#"s1.png" selectedImage:#"s2.png" ];
CCMenuItemImage *backbutton = [CCMenuItemImage itemFromNormalImage:#"backbutton.png" selectedImage:#"backbutton2. png" target:self selector:#selector(hideSideMenu:)];
[ sideMenu addChild: backbutton ];
CCMenu *menu = [ CCMenu menuWithItems: bg,button, sideMenu, nil ];
[ self addChild: menu];
What did showSideMenu and hideSideMenu do is making sideMenu appear and disappear by using runAction.
thanks.
Based on the code, the only problem I see is that backButton isn't added as a child of the menu. Only bg, button, and sideMenu are in the list of arguments. By adding it as a child of bg, it won't trigger it's own selector, but bg's. And since bg doesn't have any defined, nothing happens. The simplest fix is to just add backbutton to the list. Your code will look like this:
CCMenuItemImage *bg = [CCMenuItemImage itemFromNormalImage:#"b1.png" selectedImage:#"b2.png" ];
CCMenuItemImage *button = [CCMenuItemImage itemFromNormalImage:#"button.png" selectedImage:#"button2.png" target: self selector:#selector(showSideMenu:)];
CCMenuItemImage *sideMenu = [CCMenuItemImage itemFromNormalImage:#"s1.png" selectedImage:#"s2.png" ];
CCMenuItemImage *backbutton = [CCMenuItemImage itemFromNormalImage:#"backbutton.png" selectedImage:#"backbutton2. png" target:self selector:#selector(hideSideMenu:)];
CCMenu *menu = [ CCMenu menuWithItems: bg,button, sideMenu, backbutton, nil ];
[ self addChild: menu];
If you want each of the buttons to be positioned relative to the menu, just set their position property.
Related
I'm quite new to Qt Quick (and Qt in general), and i'd like to have an advice on "good way" to do this.
In an application, if I have a menubar and toolbar that have common actions, is there a way to link the buttons from menubar and buttons from toolbar ?
For instance, if I have a "save" function. This action is avaible through menubar and toolbar. How can I mutualise this action ?
At the moment, the best way i've found is to create a function "save" that is called by both buttons.
I've actually found a "good practice" for this problem on QML example : use Action items.
For instance :
FileDialog {
id: openDialog
onAccepted: myData.source= fileUrl
}
Action {
id: openFile
iconSource: "images/fileopen.png"
text: "Open"
onTriggered: openDialog.open()
}
menuBar: MenuBar {
Menu {
MenuItem { action : openFile }
// ....
toolBar : ToolBar {
ToolButton { action:openFile}
i have a UIKit menu based on UIButtons, when i click on one of them i'm firing a cocos2D scene. i'm setting up the CCDirector in the AppDelegate :
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
and in the rootViewController (the viewController that will holds my EAGLView) i'm doing this in the viewDidLoad method :
glView = [EAGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:glView atIndex:0];
[[CCDirector sharedDirector] setOpenGLView:glView];
CCScene *scene = [GameScene sceneWithParent:self];
[[CCDirector sharedDirector] runWithScene:scene];
i'm setting up my menu in the MainMenuViewController, from where i push the RootViewController in the stack of a navigationController. This works perfectly and my game starts without problems. I can also go back with a UIButton from the RootViewController (which holds the cocos2d scene) : - (IBAction)goBack {
[self.navigationController popViewControllerAnimated:YES];
[[CCDirector sharedDirector] end];}
but when i try to reload my game scene the application quit. i've tried this method :
restart CCDirector but in vain!! i've tried to pause the CCDirector and restart animation but also in vain, the error that i'm getting is in relation with the draw method of cocos2d!!
please help
i've just solved my problem. so if you are trying to use a UINavigationController you don't have to use [[CCDirector sharedDirector] end], because when the rootController is poped from the stack, all the scenes of cocos2d world will be released. But if you are trying to removing only the subview (not the hole controller) you have to end the CCDirector!! my problem was in a singleton class that i added as a child of GameScene, when i ended the CCDirector it still exists in the memory so i had to release it before recreating the game scene..hope this was clear.
I have a CCLayer with a UIViewController.view added to the layer via CCUIViewWrapper.
UIViewController *vc = [[UIViewController alloc] initWithNibName:#"Book" bundle:nil];
vc.view.backgroundColor = [UIColor whiteColor];
wrapper = [CCUIViewWrapper wrapperForUIView:vc.view];
wrapper.contentSize = CGSizeMake(320, 480);
[self addChild:wrapper];
The view has a UIButton, which ideally responds to Touch event, and call an IBAction.
But I can't figure out how to make the UIButton call the IBAction in CCLayer. The reason that the button calls an IBAction in CCLayer is that the action planned to do is to interact with the elements of CCLayer.
Can anyone give me some lights?
how to create menu on any other View it mean's i m created a UIView and i want to add MenuButton on it. This is my code but it is not working properly.. The UIView is hides the MuneButton..
UIView *aview;
aview = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300,250)];
aview.backgroundColor= [UIColor redColor];
[[[CCDirector sharedDirector] openGLView] addSubview:aview];
// Standard method to create a button
CCMenuItem *menuItem1 = [CCMenuItemImage
itemFromNormalImage:#"Icon.png" selectedImage:#"Icon.png"
target:self selector:#selector(NextButton:)];
menuItem1.position = ccp(100, 60);
CCMenu *starMenu = [CCMenu menuWithItems:menuItem1, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
and
(void) NextButton: (CCMenuItem *) menuItem
{
NSLog(#"Button1");
}
how to add Menu on UIView. I Try with AddSubview removing addChild. And i try aview removing self..
Hope it help to you..
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index
When you add a subview to a UIView, you are essentially adding them on top of the view. The cocos layer is always at the bottom. You can add the view to the root view controller, but you'll take a performance hit.
You're going about this in the wrong way anyway. Put your button in a CCLayer subclass with a CCColorLayer background and add that to your running scene. In your case: self.
My Learn Cocos2D book (2nd Edition) has a chapter about UIKit integration that explains how you can place UIKit views in front of and behind the Cocos2D GL view while allowing all views (front views, cocos2d view, back views) to respond to touch events.
i have menu with buttons
when touch the Play button
game go to another scene
but Touch don't work there
i'm write
self.isToucheEnabled=YES;
in init method
and add in onEnter method
[[CCTouchDispatcher sharedDispatcher] setDispatchEvents:YES];
but that don't work
pleas help why i can enable touch
You must put the following code:
-(void) onEnter {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
inside the scene you want to enable the TouchDispatcher on, then in the same scene make sure you enter this:
-(void) onExit {
[[CCTouchDispatcher sharedDispatcher] removeDelegate: self];
}
and the touch should register in every scene that you have the above code in.
This is an example of how you define a menu in cocos2d (source):
CCMenuItem *starMenuItem = [CCMenuItemImage temFromNormalImage:#"ButtonStar.jpg" selectedImage:#"ButtonStarSel.jpg" target:self selector:#selector(starButtonTapped:)];
starMenuItem.position = ccp(60, 60);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
If you need more help, please provide the code that you are using to create the menu.
You also have to add UIGestureRecognizerDelegate interface in your CCLayer in your header!
e.g.:
#interface YourScene : CCLayer <UIGestureRecognizerDelegate> {
}
In the layer use:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Not:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event