Showing posts with label Objective-C. Show all posts
Showing posts with label Objective-C. Show all posts

Friday, July 31, 2009

Always initialize your variables... never assume default value

I had a problem which was going to drive me insane. The app on the simulator worked fine, but not on the device. I introduced depth in the prototype I'm working on, where buttons need to rotate and look 3d-ish. So when I added the z value, initially this was going to be zero. So I assumed that when I'm creating a variable it was going to be zero by default. I was wrong. It is only zero by default on the simulator. On the device it won't be, at least for floats. The result was a psychedelic trippy 3d-effect, which were ugly to say the least.

Now I know why Java pesters the programmer to not allow him to use variables which have not been initialized.

Friday, July 24, 2009

Keyframe animation

I had already done some keyframe animation code in java for Swing 'n Strike. I decided to migrate some of the code. The keyframe animation classes consisted mainly of two classes, a Keyframe class and a KeframesCollection class. Each instance of a Keyframe contains information about the position, transparency, frame number/time of the keyframe, what kind of interpolation to use with the next keyframe (linear,easing in/out, etc). The interpolation method I had in java, used to give back an instance of a Keyframe containing the interpolated values. But with Objective-C (actually, because of C), now I could keep a reference to a primitive value, e.g. any int, any unsigned char, any float, or any point... and the interpolate method would directly manipulate that reference. Awesome!

It's quite powerful and easy to use. Just create a keyframes collection. Give an address to what it will be modifying, give it some keyframes, and in the update method, we play the keyframe animation.

Saturday, May 02, 2009

Be careful of constructor helper methods

I forgot that constructor helper methods (e.g. +arrayWithCapacity) would make the array autoreleased leading to an EXC_BAD_ACCESS. Still getting used to the reference counting mechanism and conventions :).

I was using such a method for an instance variable. The solution was either to retain it or to use the alloc method, which gives you an object with a retain count of 1.

_replayLines = [[NSMutableArray alloc] initWithCapacity:5];

Friday, April 24, 2009

The pains of converting from Java to Objective-C

I'm still compiling a list of the annoyances I encounter when converting from Java to Objective-C, which I will publish soon. But so far the worst one was to be careful when using the math functions like abs instead of fabs! The abs function works with ints and if you try using it with a float, you are going to get some bogus result. Why why aren't such methods overloaded!?!

Wasted a good two hours trying to hunt down such a bug in some code which worked fine in actionscript.

Mental note: remember as well sinf, cosf, asinf, acosf

Migrating current actionscript code to Xcode... check :D
Next task...