9/11/2009

[Theory] Number Base System

A hex dump of a binary executable fileImage via Wikipedia

Today we're going to look at the different Number bases and how to convert them. As allways, there are many ways of doing this and these are just my methods.

We're going to star with the easiest: Binary.
If you are a normal person (more or less) then you tend to think in a 10 base system. This means that there are 10 different signs to represent numbers in your system. Which are: 0,1,2,3,4,5,6,7,8,9. (Notice that 10 isn't a number, its a combination of two).
We call this the Decimal (DEC) system.

In binary (BIN) there are only two different symbols: 0,1. Its the easiest system to differenciate one number from another. Its either ON or OFF, its BLACK or WHITE, its 1 or 0.

Obviously, the more symbols we have to represent a number the less we have to write, but if we have very few symbols, like in the binary system, the number strings tend to become somewhat large.

Reading Binary:

Each position in a binary string represents a weight or value. Lets take this string as an example: [1101]
If there is a 1, we count that value of the position. If there is a 0 we ignor the position.
The values are the following: [1,2,4,8,16,32,64,128,512,1024...].

If we now look at our example string, starting from behind (right side), we have a 1. The 1 means we count the value. The Value for the first position (starting from behind) is 1.
Next comes the 0. Second position has the value 2 but we don't count that because of the 0.
Next number is a 1, which means again that we do count the value:4.
The last number in our string is another 1: The fourth position has a value of 8.

Now that we have gathered all the values, just add them together and you get the result of what that binary number looks in our decimal base.
1+4+8 = 25.

And thats how you can read binary.
Next we will check out some conversions from one base to another aswell as the Hexadecimal (16th) base which you might have heared from if you've done some Photoshop.
Reblog this post [with Zemanta]

7/31/2009

iPhone Game Tutorial (Part 2)

Lets add more to this App.

Our Goals today:
We're going to make a few changes to our previous project. What we're going to be able to do with this app at the end of the tutorial will be:
  • Move in 3 directions (Up, Down, Right)
  • Fire (yes!)
  • Move to different stages
So lets start by modifieing our .h:
@interface AnimateViewController : UIViewController {
IBOutlet UIImageView *background;
IBOutlet UIButton *buttonUp;
IBOutlet UIButton *buttonDown;
IBOutlet UIButton *buttonRight;
IBOutlet UIButton *buttonFire;

UIImageView *player;
UIImageView *flash;
NSArray *walkImages;
NSArray *fireImages;
NSTimer *walkingTimer;
NSTimer *fireTimer;

NSUInteger lvl;
NSUInteger topLimit;
NSUInteger bottomLimit;
}

-(void)disableMovementButtons;
-(void)enableMovementButtons;

-(IBAction)walk;
-(IBAction)stopWalking;
-(IBAction)walkUp;
-(IBAction)stopWalkingUp;
-(IBAction)walkDown;
-(IBAction)stopWalkingDown;
-(IBAction)triggerPulled;
-(IBAction)triggerReleased;

-(void)startMovingForward;
-(void)moveForward;
-(void)startMovingUp;
-(void)moveUp;
-(void)startMovingDown;
-(void)moveDown;

-(void)startFireing;
-(void)checkHits;

-(void)checklvl;

@end
As you can see we have some new methods to fill with code, but I've also modified some of the existing ones, so watch for changes.
We create IBOutlets as we will be referring to the background to switch the image in it while we move on in the game, and refer to the buttons to change some properties of them.
We also create a new UIImageView called flash witch will help us animate the gun fire. We then also create an NSTimer to check for hits on the enemies (in future tutorials).
The NSUIntegers I created are to keep track of some numbers, like the level or stage we currently are at or the maximum vertical range we can let our little soldier move on the iPhones screen.
As for the methods, I think the names speak for them self, if not, you'll see when we right the actual code.

Changing the xib:
Open the xib in Interface Builder and make the changes so it looks like on the screenshot below.


You will also need:
iPhone Game Tutorial (Part 2)


Connect all the IBOutlets to its appropriate Element (buttons and background) and set the following methods:

buttonRight Touch Down => Walk
buttonRight Touch Up Inside => stopWalking

buttonUp Touch Down => walkUp
buttonUp Touch Up Inside => stopWalkingUp

buttonDown Touch Down => walkDown
buttonDown Touch Up Inside => stopWalkingDown

buttonFire Touch Down => triggerPulled
buttonFire Touch Up Inside => triggerReleased

we can also remove the image from our background as we'll set it later in the code.
And that's it for the xib. Save and close it.

Moving on to the .m:

I've done some changes to the viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
lvl = 1;
topLimit = 140;
bottomLimit = 260;

walkImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"w1.png"], [UIImage imageNamed:@"w2.png"],
[UIImage imageNamed:@"w3.png"], [UIImage imageNamed:@"w4.png"],
[UIImage imageNamed:@"w5.png"], [UIImage imageNamed:@"w6.png"], nil];

player = [[UIImageView alloc] initWithFrame:CGRectMake(100, 220, 30, 30)];
player.image = [UIImage imageNamed:@"stand.png"];
player.animationDuration = 1.5;
player.contentMode = UIViewContentModeBottomLeft;

fireImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"flash1.png"],
[UIImage imageNamed:@"flash2.png"], nil];

flash = [[UIImageView alloc] init];
flash.animationDuration = 0.18;
flash.contentMode = UIViewContentModeBottomLeft;


[self.view addSubview:player];
[self.view addSubview:flash];
[self checklvl];
}
As you can see, we've now filled up another array with 2 images and given the new UIImageView some instructions. BUT we haven't actually placed it on the iPhone screen yet (no frame yet).
#pragma mark - #pragma mark My Methods

-(void)disableMovementButtons {
buttonUp.enabled = NO;
buttonDown.enabled = NO;
buttonRight.enabled = NO;
}

-(void)enableMovementButtons {
buttonUp.enabled = YES;
buttonDown.enabled = YES;
buttonRight.enabled = YES;
}
When the player is sawing away bullets on hordes of enemies (which don't exist yet) we don't want him to be able to run around like Rambo... and screw around with our o so simple animation. That's why we'll use these two methods to simply dis- enable the buttons.
#pragma mark - #pragma mark IB Actions

-(IBAction)walk {
if(player.frame.origin.y > topLimit &&
player.frame.origin.y < bottomLimit) {
[self startMovingForward];
player.animationImages = walkImages;
[player startAnimating];

buttonUp.enabled = NO;
buttonDown.enabled = NO;
}
}

-(IBAction)stopWalking {
[player stopAnimating];
[walkingTimer invalidate];
buttonUp.enabled = YES;
buttonDown.enabled = YES;
}

-(IBAction)walkUp {
[self startMovingUp];
player.animationImages = walkImages;
[player startAnimating];
buttonRight.enabled = NO;
buttonDown.enabled = NO;
}

-(IBAction)stopWalkingUp {
[player stopAnimating];
[walkingTimer invalidate];
buttonRight.enabled = YES;
buttonDown.enabled = YES;
}

-(IBAction)walkDown {
[self startMovingDown];
player.animationImages = walkImages;
[player startAnimating];
buttonUp.enabled = NO;
buttonRight.enabled = NO;
}

-(IBAction)stopWalkingDown {
[player stopAnimating];
[walkingTimer invalidate];
buttonUp.enabled = YES;
buttonRight.enabled = YES;
}

-(IBAction)triggerPulled {
[self startFireing];
flash.animationImages = fireImages;
[flash startAnimating];
[self disableMovementButtons];
}

-(IBAction)triggerReleased {
[fireTimer invalidate];
[flash stopAnimating];
[self enableMovementButtons];
}
All fairly simple and similar to first part of the tutorial. However, we added a little IF to move to the next stage and reset our position when we reach the end of the iPhone screen.
#pragma mark Weapon use

-(void)startFireing {
flash.frame = CGRectMake((player.frame.origin.x)+30, (player.frame.origin.y)+10, 15, 10);
fireTimer = [NSTimer scheduledTimerWithTimeInterval:0.18
target:self
selector:@selector(checkHits)
userInfo:nil
repeats:YES];
}

-(void)checkHits {

}
Here comes the part you've been waiting for! We orientate on the origin of the player frame and displace the flash so that it aligns with the gun barrel. We will not check if we hit anything yet as there isn't anything we could shoot at but we'll get to that in the next tutorial.
#pragma mark -
#pragma mark world events

-(void)checklvl {
switch (lvl) {
case 1:
background.image = [UIImage imageNamed:@"bg1.png"];
break;
case 2:
background.image = [UIImage imageNamed:@"bg2.png"];
topLimit = 170;
break;
case 3:
background.image = [UIImage imageNamed:@"bg3.png"];
break;
case 4:
background.image = [UIImage imageNamed:@"bg4.png"];
break;
case 5:
background.image = [UIImage imageNamed:@"bg5.png"];
break;
case 6:
background.image = [UIImage imageNamed:@"bg6.png"];
break;
case 7:
background.image = [UIImage imageNamed:@"bg7.png"];
break;
case 8:
background.image = [UIImage imageNamed:@"bg8.png"];
topLimit = 140;
break;
}
}
Easy enough. Later on we can also change parameters like number of enemies or type of enemies etc etc depending on the stage. Cool, huh?
You'll find the backgrounds here:

Alright, I hope I didn't leave anything out. That should do it for today. Try running around and through all stages and don't forget to shoot here and there to make you look more dangerous.

PS: I think the editor for the post here might have swollowed some code or something. I did fix one part but incase you find something that looks weird, write a coment
Reblog this post [with Zemanta]

7/30/2009

iPhone Game Tutorial (Part 1)

I thought I could share my methods on how I animate and move stuff around on the iPhone for games with you.
As always, these are just my ways of doing it. I'm sure there are better ways but I'm also very sure that there are far worse ways as well.



First comes first:
What do we want to make this app do ?
As a starter I thought it would be cool having a little man being able to run across the screen in an animated fashion.
You'll need the sprites and the background so it looks nicer.

Sprites


Once downloaded we can move on.

Create the Project:
Easy enough. Create a View Based Application. Give it a name and move to the viewController.h.

ViewController.h:
We will need to add a few things here.
Our project is going to need a UIImageView to display our little soldier.
We are also going to need an array to hold our images for the walking animation.
For the movement I like using a timer, so we'll create that too.

As for the functions, we're going to use a button to make the little guy walk across the screen of our iPhone.
So we'll create two functions for that. The name explains itself, right?
The other two void functions will be used by the timer.
@interface AnimateViewController : UIViewController {
UIImageView *player;
NSArray *walkImages;
NSTimer *walkingTimer;
}

-(IBAction)walk;
-(IBAction)stopWalking;
-(void)startMovingForward;
-(void)moveForward;

Moving on the ViewController.m:
First we're going to create our little world for the little guy to walk around on.

Make the application load in Landscape mode

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

Interface Builder:
Now open the ViewController.xib and click on the gray arrow in the top corner of the view. This will make the view rotate into landscape. Add a UIImageView, have it take the whole screen and display "wof028.png" in it and set the mode to aspect fit.
Don't forget to add a UIButton in the top right corner to make the soldier walk.
Go to the Button Connections and assign the function "Walk" to "Touch Down" and "StopWalking" to "Touch Up Inside".
Save and close the Interface Builder.

Back to .m:
Insert the following in the viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];

walkImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"w1.png"], [UIImage imageNamed:@"w2.png"],
[UIImage imageNamed:@"w3.png"], [UIImage imageNamed:@"w4.png"],
[UIImage imageNamed:@"w5.png"], [UIImage imageNamed:@"w6.png"], nil];

player = [[UIImageView alloc] initWithFrame:CGRectMake(100, 220, 30, 30)];
player.image = [UIImage imageNamed:@"stand.png"];
player.animationDuration = 1.5;
player.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:player];

}

Here we simply assigned the images to the array. We could do it with a loop too (which is recommendable if you have more than just a few files) but for this example I wanted to show it like this to make it clearer.

We also initiate our ImageView "player" with a frame (CGRect). A frame is just that. A frame that holds the position of our image. Much like a picture frame in real life. If you don't understand the numbers of CGRectMake try altering them and see what happens.
We also assign a standard "standing" pose with the "stand.png" to the player.

The animation duration number is the number of seconds it need to cycle through the whole animation. You'll need to try and see what works good for you. For this example I use 1.5.

The ContentMode sets where the images will be placed every time in the frame. This is important when the sprites are of different sizes. In our case, not so much.

Last, we add the image to our view.

Taking care of the rest:
Add the following:

#pragma mark -
#pragma mark IB Actions

-(IBAction)walk {
[self startMovingForward];
player.animationImages = walkImages;
[player startAnimating];
}

-(IBAction)stopWalking {
[player stopAnimating];
[walkingTimer invalidate];
}

#pragma mark Move it

-(void)startMovingForward {
walkingTimer = [NSTimer scheduledTimerWithTimeInterval:0.125 target:self selector:@selector(moveForward) userInfo:nil repeats:YES];
}

-(void)moveForward {
player.frame = CGRectMake(((player.frame.origin.x)+5), 220, 30, 30);
}

Pragma Mark ??
Pragma Mark can help you organize your code for when it gets very long. Copy the code above and then click on the two arrows pointing up and down at the top back above your code. This will open an index of your methods and the pragma mark text will help you find what you need.

When we tap on the button we'll start the "walk" function. The walk function starts the "moveForward" function, assigns the array walkImages to the property animationImages of our player ImageView and tells the program to start Animating "player".

startMovingForward will just start the timer. We give it an interval to work with and in the selector we tell it to activate "moveForward" on every interval. Don't forget to tell the timer to repeat itself.

moveForward will finally take care of the actual moving of the little soldier. We assign a new frame to player which is the same as the current one but we add 5 pixel to its x axis.

When we lift our finger from the Walking button, the animation and the timer will be stopped.

Build and run the application. See how it goes.
Reblog this post [with Zemanta]

7/01/2009

[iPhone] Quartz 2D

Quartz ComposerImage via Wikipedia

The last week I've been fighting to get some stuff working in Quartz. Actually I've been trying to get Quartz itself to work.
I've now managed and thought I could share:

Open your viewController.xib and add a view inside your view. Size it and move it the way you want it. This is the view that you will use like a tv screen, showing all your Quartz drawing.
(This is the way I am doing it for a specific app. I'm sure you'll find your way that fits your app the most.)

Select the view and press ⌘4 and change the class to, lets say, "QuartzView".
Select File>Write Class File and check the box to create the .h file too.

Once created, the files will be in your project.
in the QuartzView.h:
@interface QuartzView : UIView {
UIColor *color;
}
@end
Switch over to the .m file.
The drawRect method is the one where we'll do all the drawing. The drawing code has to be in this method as far as I know.

Lets start filling in some code in the drawRect method then and try to draw a line.
First, we set the color for our line and retrieve the context. The context is like your canvas where you'll be drawing. All the drawing is done in a context. As we'll only have one context, I'll call it (who would have guessed) "context" :

color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();

Then, we set some properties of the line we're going to draw. They are pretty self explanatory. You always define the context (which we cleverly called "context") and the specific attribute of the property.

CGContextSetLineWidth(context, 2.0); // We set a 2px thick line.
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetFillColorWithColor(context, color.CGColor);
//This settings want a CGColor and not a UIColor. Good for us, UIColor has a property that returns the color as a CGColor.
Now that we've set the properties of the line we're about to draw, lets prepare the actual movement required to draw a line.

We'll create 2 CGPoints which basically just group an X & Y coordinate of the screen in one variable.

CGPoint startingPoint = CGPointMake(20.0, 20.0);
CGPoint finishingPoint = CGPointMake(80.0, 80.0);

Now, we move our virtual drawing hand (with the pencil we've defined earlier) to the starting point and then make it move to the finishing point. We then just have to say to actually draw that line and to update our view. 4 lines of code.

CGContextMoveToPoint(context, startingPoint.x, startingPoint.y);
CGContextAddLineToPoint(context, finishingPoint.x, finishingPoint.y);
CGContextStrokePath(context);
[self setNeedsDisplay];
Right, so that's the basics. I'll probably go into more details once I've figured it out better myself soon.
Reblog this post [with Zemanta]

6/22/2009

[iPhone] Saving Simple Data

My hacked (for apps) iPhone & an iPod Touch at...Image by Steve Rhodes via Flickr

This tutorial will show you an easy way of saving simple bits of data so that they can be continue to be used next time the app is started on your iPhone.

Imagine you wanted to save some user settings. In this example I have a UISlider and a UISegmentedControler which I want the user to be able to set, but the iPhone should remember the settings and display them in their last state whenever the iPhone App is restarted.

In the .h file add:
float sliderValue;
float segmentValue; // Use these values to adjust your UI Elements

// You will also need the following methods:
-(NSString *)dataFilePath;
-(void)applicationWillTerminate:(NSNotification *)notification;

In the .m file add:

-(void)viewDidLoad
{
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
sliderValue = [[array objectAtIndex:0] floatValue];
segmentValue = [[array objectAtIndex:1] floatValue];
[array release];
}

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}

Above: This is where we load the date if there is any from the last save on the iPhone. First, we check to see if the data file does already exist. If it doesn't, then we don't bother about it. If it does however, we instantiate an array with the contents of the file and add the content to our variable as a float. Remember to save and load them in the same order so you don't mix things up. After having done that, we add some extra code to subscribe to the Notification so that it calls the saving process before quiting the app.
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
Above: What this method does is return the full path of the the data file (data.plist). It does that by finding the documents directory on the iPhone/iPod Touch device and appending (adding) "data.plist"

-(void)applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: [NSNumber numberWithFloat:sliderValue]];
[array addObject: [NSNumber numberWithFloat:segmentValue]];
[array writeToFile:[self dataFilePath] atomically: YES];
[array release];
}
Above: The method will be called when, as you might have guessed, the application will get terminated/closed. Here is where you should add all the data you want to keep for the next time the iPhone loads the Application.
Reblog this post [with Zemanta]

6/18/2009

[iPhone] Removing the Keyboard when done.

iphone keyboardImage by _snapp via Flickr

Let me continue with the easy stuff to fill up the Blog a little more.
Today I'll go through how to make the keyboard move out of the way once you are done editing the field.

There are two basic ways of doing it. One, is pressing the DONE key on the keyboard. The other way is tapping with your finger anywhere else on the screen.

Lets clarify:
UITextField *myTextField;
UITextField *myOtherTextField;

-(IBAction)backgroundClick:(id)sender;
-(IBAction)doneEditing:(id)sender;

Using the DONE key:

To make it return with the Done key start by adding the following code:
-(IBAction)doneEditing:(id)sender
{
[sender resignFirstResponder];
}

Any controller can be called to resign the First Responder status. With this simple function done lets set WHEN it will be called.
Open Interface Builder and single click on one of the Text Fields and click ⌘2 to open up the connections inspector.

Connect the Did End on Exit event with the doneEditing function by dragging the little circle symbol next to the event name to the File's Owner.

Repeat this for the other text field to show that you don't need a separate function for every field and try it out.

By tapping it away:

Some keyboard layouts don't have a DONE key. Or even if they have, a lot of people prefere to just tapp it away instead of tapping the small button in the corner. For these situations there is a quick and easy way of solving the problem.

Add the following code:
-(IBAction)backgroundClick:(id)sender
{
[myTextField resignFirstResponder];
[myOtherTextField resignFirstResponder];
}

Can you already guess? Lets see.

Save it again and go back to to Interface Builder.
Drag a Round Rect Button over your view and resize it so that it covers ALL of your view.
Now Select Layout > Send to Back and don't deselect the button anymore. The Button is now behind everything else you are showing.

Press
⌘1 and change the type to Custom.
You now have an invisible button in you back layer. Only thing missing now is to connect the Did touch up inside event to the action backgroundClick and you're done.

Notice that you can ask all of your fields to resign First Responder even if they don't currently have it and it won't cause any problem.
Reblog this post [with Zemanta]

6/17/2009

[iPhone] Saving an Image to the Library from UIImageView

Photo LibraryImage by ahhyeah via Flickr

In my latest app I have a UIImageView which shows an Image that has been taken from the web.
I then wanted to give the user the possibility to save that shown image to his library.
At this point I was getting ready for some longer coding for this but it turned out to be very easy.

To start from the same line let me point out where we are:
I have the following:
UIImageView *imageView;
UIImage *myImage;
-(IBAction)savePicture;


This is how its done:

-(IBAction)savePicture
{
myImage = imageView.image;
/* I assign the image that is being displayed in imageView to myImage */


UIImageWriteToSavedPhotosAlbum
(myImage, self, @selector(image:didFinishSavingWithError:contextInfo:), self);
/* This is the only code needed to save it to the library. See, its easier than you thought. */


[myImage release];
/* Release the image as we don't need to touch that anymore. Always a good thing to release images as early as possible */

}

//This is the error check to make sure we have a nice handling of our saving process.
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
// Lets create an UIAlertView to display that everything is fine.
// That way the user will know it has been actually saved.
NSString *msg = @"Saved!!!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Saved." message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
[str release];
}


And that's it.
If you have any questions, I can try helping you out.
Reblog this post [with Zemanta]

6/16/2009

What's in this Blog? Whom is it for? How often will there be new content?

Hannover Norld-LB BuildingImage by dheuer via Flickr

All very good questions in fact and there are the answers:

What's in this Blog?
I am a Software Engineer working for a middle size Internet Marketing Company. We do web pages, server hosting, custom applications etc and since currently: iPhone Applications which I am currently assigned to.

This summer I will be starting a 3 year schooling program in Software Engineering in a unique system in Germany. I will be part working in the company and part learning at a IT&Media Academy. It's not quiet likeStudying at a University as you earn practical experience just as academic.

So when I'm starting, I will be updating every bit of interesting knowledge on this blog, letting the world take part and learn as much as I am. Be prepared for a lot of info on this page.


Whom is this Blog for?
For anybody interested in Development. Not just for Mobile Applications but for everything that has to do with Software Engineering and Internet Marketing.


How often will there be new updates?
Often. I can't tell you yet exactly how often and I don't want to give you a fix amount as it depends on the course. But be assured that every time I learn something new or old I will be making a post here for you and myself too.
Reblog this post [with Zemanta]