Small things often cause pain – So why can’t I create a variable inside a switch statement?

Sometimes the smallest things can cause you pain. I had a nice example of this today. I’m still new to Objective C and find something new everyday. Was using a switch statement and had what at the time seemed like an odd little issue. I couldn’t seem to create variables and call methods. The error message I was getting back really wasn’t that helpful. Turns out it was as simple as surrounding the case statement calls in curly brackets. See below:

Fails:

case 2: 
    Alert *alert = [allAlerts objectAtIndex:indexPath.row];

    alertCell.textLabel.text = alert.date;//@"Date";
    alertCell.detailTextLabel.text = @"Alert Message";
    return alertCell;
 break;

Works:

case 2: {
    Alert *alert = [allAlerts objectAtIndex:indexPath.row];

    alertCell.textLabel.text = alert.date;//@"Date";
    alertCell.detailTextLabel.text = @"Alert Message";
    return alertCell;
} break;
http://stackoverflow.com/questions/4902568/how-do-i-create-a-variable-inside-a-switch-statement-for-a-uitableview

Yup all I need was those two curly brackets 😉 Now if only Xcode could have told me that!  Yeah I know this isn’t a big thing, just think it’s funny how the small things can be such a pain at times and how knowing the syntax of a language really can be an advantage at times. I guess the moral of the story is that small things really do matter.

 

Adding formatted text to iOS applications – UIWebView, HTML from a string.

You would think that this would be easy as can be? Surly there must be a text box with formatting built in?? Apparently that is not the case. You can obviously use core graphics and render some text any way you like, but that just didn’t fit into what I was doing. The best method I could find was to just use UIWebView.

This is really rather simple. Below are the steps you need to take to use this with text:

  1. In UI builder add a UIWebView object to your view
  2. Add an Outlet to your header file:
    @property (nonatomic, retain) IBOutlet UIWebView *formattedTextView;
  3. Add a property or variable to store the HTMl:
    @property (nonatomic, copy) NSString *theHTML;
  4. Synthesize the properties and of course release them in the dealloc method:
    @synthesize theHTML = _theHTML;
    @synthesize formattedTextView = _formattedTextView;
    
    [_theHTML release];
    [_FormattedTextView release];
  5. In the implementation of the view add the html and assign it to the control, in my case I did this in didFinishLaunchingWithOptions you simply use loadHTMLString with a nil base URL:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.theHTML = @"<html> \
    <head> \
    </head> \
    <body> \
    <p>Hello World<p> \
    </body>
    </html>";
    [self.formattedTextView loadHTMLString:self.theHTML baseURL:nil];
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;
    }

 

Not a hell of a lot to it really.

Would love to hear from anyone who knows of a better way to do this 😉

 

cocos2d Transition Issues – ” undeclared (first use in this function)

Just a short post. I was trying to get a simple transition working using some code similar to below this:

[[Director sharedDirector] replaceScene:[RotoZoomTransition transitionWithDuration:1.0 scene:[PlayScene node]]];

Oddly enough I was getting an error:

'RotoZoomTransition' undeclared (first use in this function)

Although it was really quite obvious, I didn’t see the issue and on searching the net I didn’t find much. Eventually I clicked that I was just not reading the error very carefully. Looking at transitions.h I quickly realized the names of the classes were now different.

The transition classes were renamed:
Old <–> New
CCXXXTransition <–> CCTransitionXXX
Example
CCFadeTransition <–> CCTransitionFade

Just figured I would put it out there for anyone else who is missing the obvious 😉 For more information have a look here:
http://www.cocos2d-iphone.org/wiki/doku.php/release_notes:0_99_5

cocos2d – Basic Menu

I figure I will start off with something nice and simple. All apps need a menu and games are of course no different. With cocos2d is is fairly simple to create a menu.

I’m going to presume you already have cocos2d installed and the templates installed. You are going to need at least two images, a button-up state image and a button-down state image. You could also create “-hd” versions of these for the iPhone 4.

1. Start off by creating a new cocos2d application.

2. Add your button-up and button-down images to the resources by dragging them into it. Copy them to your project.

3, Find the HelloWorldScene.m file and the remove the code between the if statements

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		// create and initialize a Label
		CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

		// ask director the the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
	
		// position the label on the center of the screen
		label.position =  ccp( size.width /2 , size.height/2 );
		
		// add the label as a child to this Layer
		[self addChild: label];
	}
	return self;
}

Becomes

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
	}
	return self;
}

4. Add you menu items. These will be CCMenuItemImage items.

CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"item1.png" selectedImage:@"item1Down.png" target:self selector:@selector(menuAction1:)];	
CMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"item2.png" selectedImage:@"item2Down.png" target:self selector:@selector(menuAction2:)];	

The code is here is fairly self explanatory. Substituting your image names for me add this code between the if statement in the init method. “menuAction1” and “menuAction2” are methods that are called one touching the menu item

5. Create the menu
Add the after the code you added above.

CCMenu *menu = [CCMenu menuWithItems:item1, item2, nil];
[menu alignItemsVertically];

6. Add the menu to the scene

[self addChild: menu];

7. FInally you need to add the event methods to .h and .m files
.m

-(void) menuAction1: (id) sender
{
// do something
}
-(void) menuAction2: (id) sender
{
// do something
}
.h
-(void) menuAction1:(id) sender;
-(void) menuAction2:(id) sender;

And that’s about all there is to it.

I know this is a little breif, so feel free to ask if you have any questions. I will try make my future posts more detailed if time permits.

cocos2d for iPhone

I’ve spent quite a bit of time looking into different 2d and 3d engines. I came across Cocos2d a while back but have only recently started playing around with it. It’s very nice and well supported. The forum is active and the developers are active. All good things IMO.

“cocos2d for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same concepts, but instead of using python it uses objective-c.”

So far it seems very well thought out and well documented. Well worth a look if your doing 2d games on iOS.

http://www.cocos2d-iphone.org/about

I will post some tutorials on using it soon 😉