Tag Archives: Code

iOS8 Core Location Updates

So I recently had a job that required me to set up Core Location Services for iOS, which I haven’t tinkered with since Apple released iOS 8, and as it turns out they made a few pretty important changes in their implementation.

Unlike previous version, iOS 8 will now require an authentication before start location services. Although this is a pretty simple task, and only necessitates a few extra lines of code, it can slow down online casino your coding quite a bit, particularly if your expecting the Core Locations service to work the same way it did in iOS 7.

There are two steps that need to be done to get Core Location working in iOS 8:

  1. The first thing that you’ll need to do is to add two lines of code to the instantiation of your Core Location Manager.We show the implementation below:
if (nil == self.localLocationManager){// iOS 7 Core Locationself.localLocationManager = [[CLLocationManageralloc] init];self.localLocationManager.delegate = self;// We only need to authorize for iOS 8if ([self.localLocationManagerrespondsToSelector:@selector(requestAlwaysAuthorization)])[self.localLocationManagerrequestWhenInUseAuthorization];

}

 

There are two kinds of authorization that you can request, WhenInUse and Always. WhenInUse authentication will allow the app to receive location updates only when the app is in the foreground. Always authorization will allow the app to receive location updates both when the app is in the foreground and in the background.

You will need Always authorization to use the following location types :

  • Significant Location Change
  • Boundary Crossing (Geofences)
  • Background Location Updates (e.g. Fitness, Navigation apps)
  • iBeacons
  • Visited Locations (iOS 8+)
  • Deferred Location Updates

All these location types have the power to wake the app from suspended or terminated when a location event occurs.

  1. The second thing that you’ll need is add the plist keys for the authorization that you need to add to your app.
  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

Screen Shot 2015-01-07 at 10.34.49 PM

Both of these keys take a string which is a description of why you need location services, which will be displayed by the UIAlertView that asks the user to allow your app permission to use the Core Location Services, so phrase it carefully.

You’ll need to include the key for the corresponding services that you’ll choose to implement, but I tend to include both (it can’t hurt after all).

Apportable

It’s taken me quite a bit of work to get as proficient with iOS and Objective C as I am. I’m not going to downplay the amount of time and energy I’ve spent on my skills, while simultaneously working full-time and going to graduate school full-time. It’s hard.

But iOS captures only about 45% of the Mobile Platform market. Over half of Mobile Devices are running Android now, and every developer needs to be at least familiar with the language.

But that means another 8 months of sleepless nights while crawling through the introductory exercises of a new library. Doesn’t it? Nope.

There are plenty of tools to compile Objective C code to the Android Platform. I started working with the Apportable Engine.

I’m a little skeptical, but I’m optimistic that this will really cut-down on the Android learning curve, especially for Video Game Development.

I’ll be posting my progress and some of the more, memorable, bugs I run into while using the Apportable Engine in the hopes that it might make some people’s lives a bit easier.

Positioning the Platforms – Y Now!?

Last time we nailed down the X-Coordinate of our Platform Nodes, and this time we’ll dig into the much more difficult task of picking a Y-Coordinate. In our last post, we just left the Coordinate as a value of 4, which will make a single unbroken platform. (Pretty boring right?)

But now it’s time to add a little challenge!

The primary aspect of platform games is the ability of the player to jump from one platform to another, essentially making an obstacles course. Sometimes there can be monsters, or treasures, or traps, but at their heart, platform games really come down to that simple premise.

So to procedurally generate this feeling, our first step is to start programmatically carrying the height of our platforms as they scroll across our screen.

To do this, we’re going to take some time to do a bit of design effort that will save us some time down the road.

What we’re going to do is add a new class to our project that encapsulates our height calculation algorithm. This design principle is called the Strategy Pattern, and is the best pattern to encapsulate complicated algorithm that might change over time. For now, we’ll be keeping our algorithm simple, but we may want to change it over time, and this design pattern will help make that easier.

So we’ll make a new class in our project and call it  EEPlatformGenerator.

Now that we have our strategy class, we can get to work.

But, there’s no sense getting ourselves wound around the axel immediately, so we’ll implement the simplest possible algorithm that we can. The simplest way to vary the height of our platforms is to just make it random. To do this, we’ll spin a quick helper function to generate a random number between two values, a maximum and minimum value:

Method - Random Number

Now all we have to decide what those maximum and minimum values are going to be. So we’re going add some data to our new class:

Class - EP Platform Method

What this data does is:

  1. currentBlockHeight – This datum will track the Block Height of the last platform.
  2. jumpHeight – This datum is the height our player can achieve when he jumps.
  3. dropHeight – This datum is the height form which a player can fall without taking damage.
  4. platformCielingHeight – This datum will provide the maximum height of a platform. We’ll use this to prevent our platforms from climbing to ridiculous heights, we wouldn’t want a platform with a height of 145,057.

Now we’ll use our new data to build a method to determine the height of our next platform:

Method - heightOfPlatform_01

 

Ta Da!!!

Platform Generation

Now that we have our Grid Laid out, we can move on with our Platform Generation Methods.

Our platform generation algorithm will break down into 5 Steps:
1. We will create a new sprite node for our Platform.
2. We will use the methods we developed last time to determine the position of our Platform.
3. We will set the Physics attributes of our Platform (in our case we will treat our Platforms like SKPhysicsBody).
4. We will add the Platform to our PlatformNode.
5. We will add the Platform to our Platform Buffer of our PlatformNode.

IMG_0671

Here’s our code for this algorithm:

– (void) generatePlatformAtPosition:(CGPoint) mapPosition
{
// 1. Create a new SKSpriteNode
SKSpriteNode *newPlatform = [SKSpriteNode spriteNodeWithTexture:self.platformBrickTexture];

// 2. Set the Position by converting the grid coordinate system to the coordinate of the Scene
newPlatform.position = [self convertMapCoordinateToWorldCoordinate:mapPosition];

// 3. Set Up the Physics Attributes
newPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.platformBrickTexture.size.width,
self.platformBrickTexture.size.width)];
newPlatform.physicsBody.dynamic = NO;
newPlatform.physicsBody.restitution = 0.0f;
newPlatform.physicsBody.friction = 1.0f;

// 4. Add the SKSpriteNode to our Platform Node
[self addChild:newPlatform];

// 5. Add the SKSpriteNode to our Platform Buffer
[platformBlockBuffer addObject:newPlatform];
}

Now we can add a platform at whatever position we want.

Next time we’ll start adding and removing the platforms dynamically.

Hooray!

Positioning the Platforms – Dos Equis

Last time we built a function to add Platforms to our Scene at a specified point, so we can place our platforms wherever we want! All we need is an X-Y Coordinate in our tile grid that we’ve already developed here:

But just getting the platforms scrolling along our scene is really the easy part; the tricky part is developing a procedure to place them in order to make our game looked like a pre-designed platform level.

There are two parts to this issue: getting an X-coordinate, and getting a Y-coordinate. The first part will be fairly easy, and the second part will take a bit more design work.

To select our Platform’s X-Coordinate, we’ll use the platform buffer we developed previously.

If you recall, the entire point of our platform buffer was to hold all of our Platform Nodes as they scroll across the screen. This means that if  we can visualize our platform buffer as a line of Platform Nodes positioned across the screen, with the first Platform Node in our Platform Buffer as being the first Platform Node in a line of across the screen and the last Platform Node in our Platform Buffer as being the last Platform Node in a line of across the screen.

This is a pretty key concept, here! What this concept allows us to do is simply use a Platform’s position in the Platform Buffer as it’s X-Coordinate.

We’ll implement this in a new method below:

AddPlatform_01

This method will add a new platform, using the generate platform method we developed previously. We place the platform at the end position of our platform buffer.

For now we’ve got the platform’s X-coordinate sorted out, and we just place at a height of 4 four now, but we’ll be changing that later.


Coordinate Conversions

Last time, I held off on going over our method to add Platforms to our SKScene. The reason I did this is because I wanted to take some time to go over a bit of background before diving headfirst into the code.

This time I’m going to go over the coordinate system that we’ll use to place our Platforms.

The strategy that we’ll use is to break our SKScene into a grid. Every column of our grid will have exactly one Platform, so the width of our grid will be the size of our  Platform Buffer and the size of each square of our grid will be the size of our Platforms.

The trick is going to be finding a way to change the coordinates of our grid to the coordinates of our SKScene.

To do this, we’ll add two new methods to our PlatformNode  class:

- (CGPoint) convertGridCoordinate ToSceneCoorindate:(CGPoint)gridCoordinate {return CGPointMake(gridCoordinate .x * self.platformBrickTexture.size.width,
gridCoordinate .y * self.platformBrickTexture.size.height + self.platformBrickTexture.size.height / 2);}
– (CGPoint) convertSceneCoorindateToGridCoordinate :(CGPoint)sceneCoorindate
{return CGPointMake(roundf(sceneCoorindate.x / self.platformBrickTexture.size.width),
roundf((sceneCoorindate.y – self.platformBrickTexture.size.height/2)/self.platformBrickTexture.size.height) );}

 

These two methods will convert a CGPoint between the coordinate system of our grid and the coordinate system of our SKScene.

Buffing Up Our Platforms

Last time we built a quick class to get our scene moving, but that’s not particularly helpful until we have something to move. In this post, we’ll add a set of platforms to our scene.

This first step is to create a new class that sub-classes our ScrollingNode that we built in the last post.

#import “ScrollingNode.h”
@interface PlatformNode : ScrollingNode
@end

This will ensure that whatever we add to this whatever we add to our PlatformNode will be able to scroll at whatever speed we decide, which was the point of our work in the last post.

Now that we have a class to get things started (or scrolled), we will build a way to keep track of a set of platforms.

One of the advantages of using a procedural level generator, is that it can lower our overhead when it comes to keeping a number of  large map or level in system memory. So, as we build our platform side-scroller we have to keep in mind that we want to limit the total number of platforms that our program will need to keep track of at any given time.

To do this, we’ll build a platform buffer to limit the number of platforms we track. Change our PlatformNode.h file as shown below:

@interface PlatformNode : SKScrollingNode
{
NSMutableArray *platformBlockBuffer;
}
@property (nonatomic) NSUInteger platformBufferSize;
@end

 

What we’ve done is to add:

  1. A Mutable Array that can store any number of objects. We’ll use this to store our Platform Nodes.
  2. An Integer to track how many Platform Nodes we’ll keep track of.

The next step is to make sure these two variables get set up correctly; in our PlatformNode.m file, change our init method as shown below:

- (id) init
{
if (( self = [super init] ))
{
platformBlockBuffer = [[NSMutableArray alloc] init];
self.platformBufferSize = 10;// Generate the first set of platform tiles on the Ground
for (int i = 0; i < self.platformBufferSize; i++)
{
// Generate a New Platform
}
}
return self;
}

 

All this code does is to:

  1. Instantiate our Platform Buffer array.
  2. Set our Platform Buffer size to a default value (I’ve just used 10 here because it’s easy.
  3. Added a loop that will fill up our Platform Buffer.

Now all we’ll need to do is to come up with a way to Generate our Platforms, which we’ll do in our next post.

Side-Scrolling

If we want to build a procedurally generated level for a side-scrolling platform game, then the first step is to get it to scroll to the side.

Crazy notion, right?

This is a pretty easy task when it comes right down to it, and takes only a few lines of code.

To accomplish our task, we’re going to take full advantage of the way Sprite Kit handles it’s scenes. Every object in Spite Kit is basically a sub-class of the SKNode class, and every SKNode has a parent and a bunch of children, forming a hierarchy or tree.

So what we’ll do in order to get our platforms to scroll is to create a new class: an ScrollingNode that is a subclass of an SKNode.

@interface ScrollingNode : SKNode

 

We’ll have to manage some way of determining how fast we want our platforms to scroll right? So we’ll have to add a property to track that.

@property (nonatomic) CGFloat scrollingSpeed;

 

Since the only functionality that we really want from our ScrollingNode is to scroll (it is in the name after all), we’ll only have a single method, which over writes the update method that’s already present.

- (void) update:(NSTimeInterval)currentTime
{
[self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop)
{
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
}];
}

This method is what takes advantage of the SKNode’s design.

The only thing this method does is to enumerate through every child of our ScrollingNode, and move them by the scrollingSpeed that we’ve already added.

And that’s it.

…I told you it would be simple…