Thursday, December 13, 2012

Alpha Channel Table


Opacity %   255 Step        2 digit HEX prefix
0%          0.00            00
5%          12.75           0C
10%         25.50           19
15%         38.25           26
20%         51.00           33
25%         63.75           3F
30%         76.50           4C
35%         89.25           59
40%         102.00          66
45%         114.75          72
50%         127.50          7F
55%         140.25          8C
60%         153.00          99
65%         165.75          A5
70%         178.50          B2
75%         191.25          BF
80%         204.00          CC
85%         216.75          D8
90%         229.50          E5
95%         242.25          F2
100%        255.00          FF

Wednesday, November 28, 2012

Cocos2D and applicationWillEnterForeground


When waking up i.e. relaunching an app (either through springboard, app switching or URL)applicationWillEnterForeground: is called. It is only executed once when the app becomes ready for use, after being put into the background, while applicationDidBecomeActive: may be called multiple times after launch. This makes applicationWillEnterForeground: ideal for setup that needs to occur just once after relaunch.
applicationWillEnterForeground: is called:
  • when app is relaunched
  • before applicationDidBecomeActive:
applicationDidBecomeActive: is called:
  • when app is first launched after application:didFinishLaunchingWithOptions:
  • after applicationWillEnterForeground: if there's no URL to handle.
  • after application:handleOpenURL: is called.
  • after applicationWillResignActive: if user ignores interruption like a phone call or SMS.
applicationWillResignActive: is called:
  • when there is an interruption like a phone call.
    • if user takes call applicationDidEnterBackground: is called.
    • if user ignores call applicationDidBecomeActive: is called.
  • when the home button is pressed or user switches apps.
  • docs say you should
    • pause ongoing tasks
    • disable timers
    • pause a game
    • reduce OpenGL frame rates
applicationDidEnterBackground: is called:
  • after applicationWillResignActive:
  • docs say you should:
    • release shared resources
    • save user data
    • invalidate timers
    • save app state so you can restore it if app is terminated.
    • disable UI updates
  • you have 5 seconds to do what you need to and return the method
    • if you dont return within ~5 seconds the app is terminated.
    • you can ask for more time with beginBackgroundTaskWithExpirationHandler:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
 [[CCDirector sharedDirector] resume];
 [[CCDirector sharedDirector] startAnimation];
}

- (void) applicationDidEnterBackground:(UIApplication *)application
{
 [[CCDirector sharedDirector] stopAnimation];
 [[CCDirector sharedDirector] pause];
}

- (void)applicationWillResignActive:(UIApplication *)application {
 [[CCDirector sharedDirector] stopAnimation];
 [[CCDirector sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
 [[CCDirector sharedDirector] stopAnimation]; // Avoid 2 Display Link Crash ***
 [[CCDirector sharedDirector] resume];
 [[CCDirector sharedDirector] startAnimation];
}


Sources:

Saturday, November 24, 2012

Removing Flickers from Cocos2d

There are so many useful posts on StackOverflow. This one is about removing various types of flickers you can remove by configuring your EAGLView correctly:



Friday, November 23, 2012

Objective-C Categories to Extend Collections

My first use for Objective-C categories was to extend the NSArray class to act like a Stack and then later, like a Queue.

Here is a post about how to use categories:
http://macdevelopertips.com/objective-c/objective-c-categories.html

Here is a post about how to use them to make Stacks and Queues:
http://stackoverflow.com/questions/817469/how-do-i-make-and-use-a-queue-in-objective-c

One thing that initially threw me for a loop is the naming. They do expect you to use a '+' in the name and that is OK in Mac OS X and XCode.

Another thing that threw me for a loop is that unlike a Generic or a sub-class, they're simply injected (available) for every instance of the object to whom the category belongs.

That is to say, you do not need to create a Stack -- if you have the category, all your NSArray can now act like a Stack if you send that message to the instance.


1024x1024 Icons Look Funny (Gloss) in iTunes Connect

Do not be alarmed, my friend, your icon will always look funny after you upload it into iTunes Connect.

Our loyal partners over at Apple App Store will make some adjustments to your icon during the application submission process and it appears that everything just works out in the end if you don't worry and learn to love the App Submission Process.

http://stackoverflow.com/questions/8804236/ios-app-icon-has-glossy-effect-on-app-store-and-itunes-connect

Bundle Version and Bundle Build in XCode 4

The SO question that helped me most yesterday was:

http://stackoverflow.com/questions/7281085/whats-the-difference-between-version-number-in-itunes-connect-bundle-versio


Apple has a lot of information too, of course:



They all refer to the version of your application.
  • iTunes Connect
    This is the version number shown in the App Store; This must be a pure version number like1.2.3
  • Bundle Version (CFBundleVersion)
    This doesn't need to be a pure version number. This can be something like 12345 or 1.2.3 (Build 12345AB). This is shown in the About window for Mac OS X apps for example and is often more a "Build Number" than a "Version Number".
  • Bundle Version String (CFBundleShortVersionString) This value is used as the "real" version number. This must be the same string as used for the version in iTunes Connect.



Thursday, November 22, 2012

iPhone and iPad Debugging for Star Traders RPG

I was working on device compatibility between various iPhone devices and was having trouble clearing out a particular configuration so I could test. I was using the iPhone Simulator and was just about to pull out my hair until I found this simple solution:


I was having this weird MP3 playing error on my iPod that was inconsistent and eventually just stopped appearing:

The error was "The operation couldn’t be completed. (OSStatus error -50.)"


I found out if you pop a modal dialog over an OpenGL view in Cocos2d without saying YES to animated you will get crazy errors:

The error was "OpenGL error 0x0506 in -[EAGLView swapBuffers]" and the screen just wouldn't update correctly
http://www.cocos2d-iphone.org/forum/topic/20318


I also found some issues with different iPhone devices related to the Cocos2d "Animation Started" property and had to work around some odd issues with the Cocos2d template project...

The error message was "displayLink must be nil. Calling startAnimation twice?"
http://www.cocos2d-iphone.org/forum/topic/7326

Tuesday, October 23, 2012

iPhone Development Learning Continues

The nexus of iOS 6 and XCode compatibility settings can sometimes generate a real mess.

This exception is caused can be created by a string of seemingly benign events:

http://stackoverflow.com/questions/11085859/how-can-i-fix-nsinvalidunarchiveoperationexception


Everyone once in a while it can be very advantageous for an iPhone developer or coco2d programmer to go back over the basic concepts as a refresher and reminder:

http://www.indiedb.com/tutorials/cocos2d-working-with-sprites


There will always be times in your code that you need to detect if the current device is an iPad, and iPhone or what. This quick answer on StackOverflow does the trick for me:

http://stackoverflow.com/questions/11097362/detect-if-app-is-running-in-ipad-simulator


If you are like me, and new to Objective-C, you will probably spend a lot of time struggling with 'release' and 'autorelease' and ARC and everything else. Sometimes I get into trouble with multiple releases of memory (error message '-[CALayer retain]: message sent to deallocated instance')

http://stackoverflow.com/questions/3235949/calayer-retain-message-sent-to-deallocated-instance


A sore point for me right now is I still haven't figured out how to handle variable length generated text as gracefully as I can on Android.

http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content


Back to CCSprite, you will eventually need to switch the image in use by that object:

http://www.coderzheaven.com/2011/03/17/changing-the-image-of-the-sprite-in-cocos2d/


I was sad to lear that C-style string formats are fraught with bugs and ugliness:

http://stackoverflow.com/questions/1063843/is-there-a-way-to-specify-argument-position-index-in-nsstring-stringwithformat


Switching View XIB between iPhone and iPad can't be this hard ... can it? can it?

http://stackoverflow.com/questions/10459498/xcode-4-2-change-the-iphone-xib-to-ipad-xib

Thursday, September 06, 2012

Transparent PNG Animation


This is the code I am using to draw animations of explosions onto some UIImageViews.

I struggled a lot with "Animated PNG with Transparent Region" in this code, but in the end discovered that iPhone animation do not support PNG-8 and you must use PNG-24.


 NSMutableArray *frames = [NSMutableArray arrayWithCapacity:25];
    
    for (int i = 0; i < 26; i++) {
        UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"exp%d.png", i]];
        
        [frames addObject:frame];
    }
    
    self.playerShipImageAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exp0.png"]];
    self.hostileShipImageAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exp0.png"]];
    
    [playerShipImage addSubview:playerShipImageAnim];
    [hostileShipImage addSubview:hostileShipImageAnim];
    
    playerShipImageAnim.animationImages = frames;
    hostileShipImageAnim.animationImages = frames;
    playerShipImageAnim.animationRepeatCount = 1;
    hostileShipImageAnim.animationRepeatCount = 1;
    playerShipImageAnim.animationDuration = 0.5;
    hostileShipImageAnim.animationDuration = 0.5;

Tuesday, July 31, 2012

iPhone Tutorials

My research says that if you are going to go all in on the iOS and Apple App Store you need to support iPhone, iPod and iPad all. You cannot skip tablets as easily on iOS as you can on Android, so this tutorial is pretty clutch:

How to Port an Application to iPad
http://www.raywenderlich.com/1111/how-to-port-an-iphone-application-to-the-ipad

This post about things you wish you knew about cocos2d before you started coding
http://www.cocos2d-iphone.org/forum/topic/737

Another one of Ray Wenderlich's wonderful tutorials (thanks Ray!) about GCD and multiple threads:
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial


Friday, July 20, 2012

Cory vs. UITableView

As I continue my adventure into iPhone development I have struggled a bit with the UITableView concepts.

Tables and lists in Android seem to be much more straight forward and in the end I had to do a lot of reading to figure out how to make a basic scrolling list of data in Objective-C.

First I started reading tutorials:
http://iosdeveloperzone.com/2011/05/09/using-uitableview/

But that will only get you so far. The problem with tutorials is the confusion of IB changes and various patterns of clicking that must be accomplished to link the correct IBOutlets to the cells and other details.

So I did more Googling and found some additional tutorials:
http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-add-uiimage-and-uilabel-in-the-uitableview/

The tutorials gets you close enough to have some code running but for the most part it just crashed a lot.

This second source of details was really useful and was more of a guide than a tutorial:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

Eventually I had to go back to the basics and try to understand how UITableView and UIView get along. As usual, when you go back to the basics you end up on Apple's documentation site:

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW1

I am still trying to wrap my mind around the translation of concepts between Android and iOS and the resulting map is a mass of confusion. As I build a better understanding of the two operating systems and SDK I will continue to post mapping blogs.

Wednesday, July 04, 2012

Some iPhone Development Stuff

I was working on coding some Water-Fuel display systems that needed floating point numbers converted to a 2 decimal place display for the UI.


NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloat];
This came from a great Stackoverflow question here:
http://stackoverflow.com/questions/560517/make-a-float-only-show-two-decimal-places

When I'm making lots and lots of model objects from existing applications, I often want to generate Objective-C from JSON or GSON. I use http://tigerbears.com/objectify/ for that.

Trying to figure out some stuff about Apple's in-app purchasing system I learned a lot from reading http://www.raywenderlich.com/2797/introduction-to-in-app-purchases which is an excellent tutorial on an excellent site.

Wednesday, April 25, 2012

Controller Events in Android vs. iPhone

Android


  1. onCreate
  2. onStart
  3. onResume
  4. onPause
  5. onStop
  6. onDestroy
  7. onLowMemory

iPhone


  1. loadView
  2. viewDidLoad
  3. viewWillAppear
  4. viewDidAppear
  5. viewWillDisappear
  6. viewDidDisappear
  7. didReceiveMemoryWarning

Monday, April 23, 2012

Retrieve a File from iPhone Developer Device Sandbox

Here is a post discussing extracting files from an iPhone development device.

You basically use Organizer, browse to the Sandbox Application and click 'Export'

http://stackoverflow.com/questions/1548029/copy-a-file-from-iphone-sandbox-to-desktop

Remove the Frames Per Second FPS in Cocos2d Project

Sooner or later you will need to remove the FPS image in your Cocos2d project.

[[CCDirector sharedDirector] setDisplayFPS:NO];

I do this directly after i setOpenGLView.

Saturday, April 14, 2012

iPhone UIScrollView Seemed Really Hard Until

At least until I read this tutorial:

http://redartisan.com/2010/5/23/scrolling-with-uiscrollview

A lot of other tutorials make the process very unclear and skip over the realistic requirements of building "larger than window UIView" and this page on Red Artisan hits it perfectly.

Tuesday, April 10, 2012

Inserting and Deleting Data using FMDB


I have been using FMDB to manage the Star Traders RPG database files in iPhone.

There are two ways to run the -(void) executeUpdate; method, as outlined here:

http://www.icodeblog.com/2011/11/04/simple-sqlite-database-interaction-using-fmdb/

The page that got me started was this -- the key is the "open" method

Inserting And Deleting Data

Inserting data using sqlite is very straight forward. You can either build your strings and pass them in directly OR use the sqlite format using “?’s” and letting fmdb do the work. Below is an example of each:
// Building the string ourself
NSString *query = [NSString stringWithFormat:@"insert into user values ('%@', %d)",
@"brandontreb", 25];
[database executeUpdate:query];
 
// Let fmdb do the work
[database executeUpdate:@"insert into user(name, age) values(?,?)",
@"cruffenach",[NSNumber numberWithInt:25],nil];
Generally, the second route is preferred as fmdb will do some of the sanitizing for your (such as add slashes to single quotes).
Now that we have some data in our database, let’s delete it. The following code will delete all users with an age of 25:
[database executeUpdate:@"delete from user where age = 25"];
And that should remove both of the records that we inserted.

iPhone or iPad to Start Out?

I have been trying to decide -- should I start with making and testing an iPhone or an iPad application first?

I used this tutorial to convert from an iPhone application to one that ran on iPad and iPhone but the results were not very good.

http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html

Where Are the Best iPhone Tutorials?

Here are the ones I am finding most useful:

http://www.raywenderlich.com/tutorials

Which iPhone tutorials have you found most useful for development -- games or in general?

Android to iPhone -- Passing Data Between Controllers

In Android we use INTENT and in iPhone we use a few different techniques.

This link has two methods, passing into and passing out of. I'm using a database for most of my return values but I found the snippets below to be the most valuable.

http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers

Passing Data INTO a UIViewController
Of course we could skip right to the code:


For this example we will have ViewControllerA and ViewControllerB
To pass a BOOL value from ViewControllerA to ViewControllerB we would do the following.
1) in ViewControllerB.h create a property for the BOOL
@property(nonatomic) BOOL *isSomethingEnabled;
2) in ViewControllerA you need to tell it about ViewControllerB so use an
#import "ViewControllerB.h"
Then where you want to load the view eg. didSelectRowAtIndex or some IBAction you need to set the property in ViewControllerB before you push it onto nav stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib=@"ViewControllerB" bundle=nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];