Make Your Own iPhone Game, Day 7

Conan gets real, real sexy.

Look how smooth his flow is flowing!

I spent most of day 7 researching Cocos2D iPhone’s sprites, sprite caches, animations and atlases. The thorn in my mind was trying to figure out a way to erase the ugly “transparent” brown color from the bitmaps using purely code. I was searching in earnest for this way because I don’t want to have to update thousands of bitmaps and erase ugly brown colors.

At the end of the day I gave up and just decided to pack the textures with Zwoptex and then remove the ugly brown color in Photoshop after it packed a nice PNG for me. Before Zwoptex would accept the bitmaps I had to convert them to PNG first. After an hour of frustratingly trying to do this with the horribly stupid Automator, I found the following command line utility which worked like a charm:

sips -s format png *.bmp --out pngs

I also had a bit of fun re-learning how circles work. Yay. Trigonometry is fun. Woooo. Here’s how the joystick button position is converted into player movement:

joybtnRadians = atan2f(newpos.x - joypad.position.x, newpos.y - joypad.position.y);
if(joybtnRadians < -(M_PI * 0.875) || joybtnRadians > (M_PI * 0.875))
	{move.y -= step;}
else if(joybtnRadians < -(M_PI * 0.625))
	{move.x -= step; move.y -= step;}
else if(joybtnRadians < -(M_PI * 0.375))
	{move.x -= step;}
else if(joybtnRadians < -(M_PI * 0.125))
	{move.x -= step; move.y += step;}
else if(joybtnRadians > (M_PI * 0.625))
	{move.x += step; move.y -= step;}
else if(joybtnRadians > (M_PI * 0.375))
	{move.x += step;}
else if(joybtnRadians > (M_PI * 0.125))
	{move.x += step; move.y += step;}
else
	{move.y += step;}

And the joystick button position is “clamped” to the edge of the joypad with the following code. Don’t ask me why we have to subtract half a PI for x and add half a PI for y… It just works that way.

if( joybtnDist > kMaxJoybtnDist)
{
	newpos.x = joypad.position.x + (23.45f * cosf(joybtnRadians - (M_PI / 2)));
	newpos.y = joypad.position.y + (23.45f * sinf(joybtnRadians + (M_PI / 2)));
	joybtnDist = kMaxJoybtnDist;
}
[joybtn setPosition:newpos];

Leave a Reply

Spam protection by WP Captcha-Free

Storyteller   .:.   Musician   .:.   Vagabond   .:.   Wizard