Adding AwesomeMenu in appdelegate - cocos2d-iphone

I want to add awesomeMenu in my project but there is a problem in awesomemenu's appdelegate file in
Appdelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate,AwesomeMenuDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
is there
and in our normal appdelegate.h file there is
#interface AppController : NSObject <UIApplicationDelegate, CCDirectorDelegate>
{
UIWindow *window_;
UINavigationController *navController_;
CCDirectorIOS *director_; // weak ref
}
#property (nonatomic, retain) UIWindow *window;
#property (readonly) UINavigationController *navController;
#property (readonly) CCDirectorIOS *director;
#end
now if i change CCdirectordelegate to AwesomeMenudelegate there will be error
how to implement it?
Is there possibility to use it in helloworld layer
or we have to operate awesomemenu from appdelegate?
or is there possible to implement two interface?

If you want to compine AwesomeMenu with Cocos2d project you don't have to remove from your AppDelegate.h the CCDirectorDelegate. This is what I have done and works:
create a new project based on default cocos2d template
(if you want to be up to date, create a local copy of AwesomeMenu's github project)
add to the project 2 subdirectory of AwesomeMenu: AwesomeMenu/AwesomeMenu (4 files inside) and Images
only modify 2 files: HelloWorldLayer.h and HelloWorldLayer.m
HelloWorldLayer.h (added an import AwesomeMenu.h, and a AwesomeMenuDelegate)
#import <GameKit/GameKit.h>
#import "AwesomeMenu.h"
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
#interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate,
GKLeaderboardViewControllerDelegate,
AwesomeMenuDelegate>
{
}
HelloWorldLayer.m (modify only the init method by deleting GameCenter code and add example AwesomeMenu menu with slight modification):
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
UIImage *storyMenuItemImage = [UIImage imageNamed:#"bg-menuitem.png"];
UIImage *storyMenuItemImagePressed = [UIImage imageNamed:#"bg-menuitem-highlighted.png"];
UIImage *starImage = [UIImage imageNamed:#"icon-star.png"];
AwesomeMenuItem *starMenuItem1 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem2 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem3 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem4 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem5 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem6 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem7 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem8 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem9 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, starMenuItem5, starMenuItem6, starMenuItem7,starMenuItem8,starMenuItem9, nil];
AwesomeMenu *menu = [[AwesomeMenu alloc] initWithFrame:[[[CCDirector sharedDirector] view] window].bounds menus:menus]; // PLS NOTE: referencing window
// customize menu
/*
menu.rotateAngle = M_PI/3;
menu.menuWholeAngle = M_PI;
menu.timeOffset = 0.2f;
menu.farRadius = 180.0f;
menu.endRadius = 100.0f;
menu.nearRadius = 50.0f;
*/
//menu.startPoint = CGPointMake(120.0, 240.0);
menu.delegate = self;
[[[CCDirector sharedDirector] view] addSubview:menu]; // PLS NOTE: referencing view
}
return self;
}
Hope you can manage in your own app!

Related

custom object with CCSprite that conforms to NSCoding not displaying

In my effort to save a ScrollingBackground object I've subclassed the CCSprites to conform to NSCoding. The ScrollingBackground doesn't display. Please see the relevant code below. I'm not really sure whats wrong. Please help.
ScrollingBackground.h:
(CCBackgroundSprite's interface)
#interface CCBackgroundSprite: NSObject <NSCoding>
#property (nonatomic, assign) float xValue;
#property (nonatomic, assign) float yValue;
#property (nonatomic, retain) NSString* backgroundStringName;
#end
ScrollingBackground.m:
(CCBackgroundSprite's implementation)
#implementation CCBackgroundSprite
-(id)init
{
if((self = [super init])){
}
return self;
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
self.xValue = [aDecoder decodeFloatForKey:#"xValue"];
self.yValue = [aDecoder decodeFloatForKey:#"yValue"];
self.backgroundStringName = [aDecoder decodeObjectForKey:#"backgroundStringName"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeFloat:self.xValue forKey:#"xValue"];
[aCoder encodeFloat:self.yValue forKey:#"yValue"];
[aCoder encodeObject:self.backgroundStringName forKey:#"backgroundStringName"];
}
#end
Setting CCBackgroundSprite's instances for the CCSprite properties:
-(void)spriteProperties {
background1 = [[CCBackgroundSprite alloc] init];
[background1 setXValue:bg.position.x];
[background1 setYValue:bg.position.y];
[background1 setBackgroundStringName:#"bg"];
background2 = [[CCBackgroundSprite alloc] init];
[background2 setXValue:bgSwap.position.x];
[background2 setYValue:bgSwap.position.y];
[background2 setBackgroundStringName:#"bgSwap"];
background3 = [[CCBackgroundSprite alloc] init];
[background3 setXValue:bgSwap2.position.x];
[background3 setYValue:bgSwap2.position.y];
[background3 setBackgroundStringName:#"bgSwap2"];
}
encoding/decoding of other non-Sprite related properties of the ScrollingBackground:
-(void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:self.backgroundCount forKey:#"backgroundCount"];
[aCoder encodeInt:self.backgroundRepeatCount forKey:#"backgroundRepeatCount"];
[aCoder encodeFloat:self.scrollSpeed forKey:#"scrollSpeed"];
[aCoder encodeObject:self.backgroundArray forKey:#"backgroundArray"];
[aCoder encodeObject:self.changeArray forKey:#"changeArray"];
.
.
.
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
self.backgroundCount = [aDecoder decodeIntForKey:#"backgroundCount"];
self.backgroundRepeatCount = [aDecoder decodeIntForKey:#"backgroundRepeatCount"];
self.scrollSpeed = [aDecoder decodeFloatForKey:#"scrollSpeed"];
self.backgroundArray = [aDecoder decodeObjectForKey:#"backgroundArray"];
self.changeArray = [aDecoder decodeObjectForKey:#"changeArray"];
.
.
.
}
}
Saving and loading of ScrollingBackground object:
- (void)saveBackgroundObject:(ScrollingBackground *)object key:(NSString *)key {
[self spriteProperties];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
NSString *dataToString = [NSString stringWithFormat:#"%#", encodedObject];
CCLOG(#"encodedObject = %# \n", dataToString);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:key];
[defaults synchronize];
}
-(ScrollingBackground *)loadBackgroundWithKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [defaults objectForKey:key];
NSString *dataToString = [NSString stringWithFormat:#"%#", encodedObject];
CCLOG(#"encodedObject = %# \n", dataToString);
ScrollingBackground *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
return object;
}
UPDATED:
I have made the following changes the spriteProperties method:
-(void)spriteProperties {
background1 = [[CCBackgroundSprite alloc] init];
[background1 setXValue:bg.position.x];
[background1 setYValue:bg.position.y];
[background1 setBackgroundImageName:bg.displayFrame.textureFilename];
[self addChild:background1];
background2 = [[CCBackgroundSprite alloc] init];
[background2 setXValue:bgSwap.position.x];
[background2 setYValue:bgSwap.position.y];
[background2 setBackgroundImageName:bgSwap.displayFrame.textureFilename];
[self addChild:background2];
background3 = [[CCBackgroundSprite alloc] init];
[background3 setXValue:bgSwap2.position.x];
[background3 setYValue:bgSwap2.position.y];
[background3 setBackgroundImageName:bgSwap2.displayFrame.textureFilename];
[self addChild:background3];
}
The main reason I am using displayFrame.textureFilename above is because I'm reusing the sprites along the way.
Also to setup of the background images I did:
-(void)startingSprites //change later to setupInitialBackground
{
CGSize s = [[CCDirector sharedDirector] winSize];
bg = [CCSprite spriteWithSpriteFrameName:#"bgImage1.png"];
bg.position = ccp(s.width/2, s.height/2);
[currentBackgroundBatchNode addChild:bg];
swapbg = [CCSprite spriteWithSpriteFrameName:#"bgImage2.png"];
swapbg.position = ccp(s.width/2, 3*s.height/2 -1.0);
[currentBackgroundBatchNode addChild: swapbg];
swapbg2 = [CCSprite spriteWithSpriteFrameName:#"bgImage3.png"];
swapbg2.position = ccp(s.width/2, 5*s.height/2 - 2.0);
[currentBackgroundBatchNode addChild: swapbg2];
CCLOG(#"bg background is %#", bg.displayFrame.textureFilename);
CCLOG(#"bgSwap background is %#", swapbg.displayFrame.textureFilename);
CCLOG(#"bgSwap2 background is %#", swapbg2.displayFrame.textureFilename);
}
I've just realized a few things:
the CCLOG's in startingSprites are null
I reuse the currentBackgroundBatchNode (which is a CCSpriteBatchNode) along the way, meaning that I have to encode/decode it. How do I subclass it and with what properties? Not too sure how it'll work out.
I have read a number of your posts, also related to this. I would recommend that instead of trying to subclass several cocos2d classes to conform to NSCoding you should use a simpler work around. I believe your background has it's own layer, so why don't you rather save various background parameters and create another init for your background to handle cases for reloading the background state.
You say you've subclassed CCSprite, but you actually subclassed NSObject. Try:
#interface CCBackgroundSprite: CCSprite <NSCoding>
...

Newbie Error Apple Mach-O Linker Error

I'm fairly new here, but I'm having a Apple Mach-O Linker Error:Error
This is my app delegate!
//
// AppDelegate.m
// VCContainmentTut
//
// Created by A Khan on 01/05/2013.
// Copyright (c) 2013 AK. All rights reserved.
//
#import "AppDelegate.h"
#import "RootController.h"
#import "ViewController.h"
#import "VidViewController.h"
#import "SelectVideo1.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
VidViewController *vidViewController = [[VidViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vidViewController];
[self.window setRootViewController:navController1];
SelectVideo1 *selectVideo1 = [[SelectVideo1 alloc] initWith:#"Movie Trailers"];
//[self.navController pushViewController:selectVideo1 animated:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *tabStoryBoard = [UIStoryboard storyboardWithName:#"TabStoryboard" bundle:nil];
UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:#"NavStoryboard" bundle:nil];
UINavigationController *navController = [navStoryBoard instantiateViewControllerWithIdentifier:#"Nav Controller"];
UITabBarController *tabController = [tabStoryBoard instantiateViewControllerWithIdentifier:#"Tab Controller"];
ViewController *redVC, *greenVC;
redVC = [[ViewController alloc] init];
greenVC = [[ViewController alloc] init];
redVC.view.backgroundColor = [UIColor redColor];
greenVC.view.backgroundColor = [UIColor greenColor];
RootController *menuController = [[RootController alloc]
initWithViewControllers:#[tabController, redVC, greenVC, navController, selectVideo1]
andMenuTitles:#[#"Cheats", #"Videos", #"WalkThroughs", #"Nav"]];
self.window.rootViewController = menuController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
Your errors are caused by you trying to use code that you haven't told the project (so the linker) exists. You need to add the dependencies to the project. In this case, the frameworks defining the features you're trying to use.

how to use UISwipeGestureRecognizer in the project created by cocos2d?

i created a project by using cocos2d,and now i want to use UISwipeGestureRecognizer for get the up/down/left/right, so how can i do?
thanks a lot
In the .h file add this :
// Add inside #interface
UISwipeGestureRecognizer * _swipeLeftRecognizer;
UISwipeGestureRecognizer * _swipeRightRecognizer;
// Add after #interface
#property (retain) UISwipeGestureRecognizer * swipeLeftRecognizer;
#property (retain) UISwipeGestureRecognizer * swipeRightRecognizer;
In .m file add this :
// Add after #implementation
#synthesize swipeLeftRecognizer = _swipeLeftRecognizer;
#synthesize swipeRightRecognizer = _swipeRightRecognizer;
// Then add these new methods
- (void)onEnter {
self.swipeLeftRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe:)] autorelease];
_swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeLeftRecognizer];
self.swipeRightRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe:)] autorelease];
_swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeRightRecognizer];
}
- (void)onExit {
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeLeftRecognizer];
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeRightRecognizer];
}
// Add to dealloc
_swipeLeftRecognizer = nil;
[_swipeRightRecognizer release];
_swipeRightRecognizer = nil;
Hope it'll help

IOS TabBarController Questions

TabBar will be about 2 question. I am using a combination of a TabBar and NavigationController. As the following link.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CombiningViewControllers/CombiningViewControllers.html
Question 1:
I would like to appear before a TabBar to another ViewController. Several checks are here to do. (For example, Facebook login) If the prerequisites are met, tabbar will be visible. How do I do?
Question 2:
TabBar screen icon that appears in the middle of the first TabBar want it to be. The following code sequence also affects the order of TabBarItem.
self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, nil];
Thank you.
Okan Sahin
For those who have the same problem:
I'm using Xcode 4.2. I have created Tabbed App.
Answer 1:
For loading screen,
I created a new ViewController.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewControllerLoading = [[LoadingViewController alloc] initWithNibName:#"LoadingViewController" bundle:nil];
self.window.rootViewController = viewControllerLoading;
[self.window makeKeyAndVisible];
return YES;
}
LoadingViewController.h
#interface LoadingViewController : UIViewController <UITabBarControllerDelegate>
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
LoadingViewController.m
UIViewController *viewControllerFriends = [[FriendsViewController alloc] initWithNibName:#"FriendsViewController" bundle:nil];
UINavigationController* navController1 = [[UINavigationController alloc]
initWithRootViewController:viewControllerFriends];
UIViewController *viewConrollerMessages = [[MessagesViewController alloc] initWithNibName:#"MessagesViewController" bundle:nil];
UINavigationController* navController2 = [[UINavigationController alloc]
initWithRootViewController:viewConrollerMessages];
UIViewController *viewControllerWorld = [[WorldViewController alloc] initWithNibName:#"WorldViewController" bundle:nil];
UINavigationController* navController3 = [[UINavigationController alloc]
initWithRootViewController:viewControllerWorld];
UIViewController *viewControllerCheckIn = [[CheckInViewController alloc] initWithNibName:#"CheckinViewController" bundle:nil];
UINavigationController* navController4 = [[UINavigationController alloc]
initWithRootViewController:viewControllerCheckIn];
UIViewController *viewControllerProfile = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
UINavigationController* navController5 = [[UINavigationController alloc]
initWithRootViewController:viewControllerProfile];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, navController3, navController4, navController5, nil];
[self.view addSubview:self.tabBarController.view];
Answer 2:
self.tabBarController.selectedIndex = 2;
Best regards
Okan Sahin

UIButton in a UITabBarController crashing when clicked!

I tried to include a custom button programmatically inside a UIView which is a subview of UITabBarController.
The button is displays fine but when I click it, it crashes without an error message. Its strange that sometimes it does inconsistently:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString playButton]: unrecognized selector sent to instance 0x632cd10'
I even tried removing any code in my play button method, i tried changing the name playButton to playAction, I also tried add the button directly to "self" not to aboutView but the result is still the same.
My guess is it got something to do with the tabBar having a UIView as a subview with a button on it. I don't know.
Here's a code snippet of how i built the tabBar in my appDelegate method
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutNavC = [[[UIViewController alloc] init] autorelease];
aboutNavC.title = #"About";
aboutNavC.view = aboutViewC.view;
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];
and heres the code for implementing the AboutViewController class
AboutViewController.h
#import
#interface AboutViewController : UIViewController {
UIButton *playSoundButton;
UIView *aboutView;
}
- (void)playButton;
#end
AboutViewController.m
#import "AboutViewController.h"
#implementation AboutViewController
- (void)dealloc {
[playSoundButton release];
[aboutView release];
[super dealloc];
}
- (void)viewDidLoad {
aboutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain];
image = [UIImage imageNamed:#"bt_play.png"];
[playSoundButton setImage:image forState:UIControlStateNormal];
[playSoundButton addTarget:self action:#selector(playButton) forControlEvents:UIControlEventTouchUpInside];
playSoundButton.frame = CGRectMake(0, 350, 40, 40);
[aboutView addSubview:playSoundButton];
stopSoundButton.hidden = YES;
playSoundButton.hidden = NO;
[self.view addSubview:aboutView];
[super viewDidLoad];
}
- (void)playButton
{
NSLog(#"playAction method");
}
#end
Thanks in advance!
[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain]; should read playSoundButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
(i.e. move the first [ further right to UIButton)
Thanks tob for the feedback, i actually know how to implement retain statement, it was a typo i didn't notice. surprisingly, that is not the problem and the code run's fine now leaving the typo unchanged.
The real problem was in my appDelegate method.
Instead of
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutNavC = [[[UIViewController alloc] init] autorelease];
aboutNavC.title = #"About";
aboutNavC.view = aboutViewC.view;
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];
should be
// About Tab
aboutViewC = [[[AboutViewController alloc] init] autorelease];
aboutViewC.title = #"About";
// Lessons Tab
lessonsViewC = [[[LevelViewController alloc] init] autorelease];
lessonsViewC.title = #"Levels";
lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
lessonsNavC.title = #"Lessons";
lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:aboutViewC, lessonsNavC, nil];