Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Space Cat "Varying Size and Velocity" Crash Issue

Hey guys,

So I'm up to the Varying Size and Velocity stage of the Space Cat game and I have come across a rather concerning problem that results in the game crashing.

The Problem

I successfully build the app and it runs perfectly until it comes time to introduce the enemies to the game and that's when all hell breaks loose and the game crashes and I am presented with the following error:

Thread 1:EXC_ARTITHMETIC(code=EXC_I386_DIV, subcode=0x0)

This is within apparently the THUtil.m implementation file.

I have had no problems up until this stage.

The Code

Here is the code that I have for both the header (.h) and the implementation (.m) files.

THUtil.m

#import "THUtil.h"

@implementation Util

+ (NSInteger) randomWithMin:(NSInteger)min max:(NSInteger)max {
    return arc4random()%(max - min) + min;
}

@end

THUtil.h

#import <Foundation/Foundation.h>

static const int ProjectileSpeed = 400;
static const int SpaceDogMinSpeed = -100;
static const int SpaceDogMaxSpeed = -50;

typedef NS_OPTIONS(uint32_t, collisionCategory) {
    collisionCategoryEnemy      = 1 << 0,       // 0000
    collisionCategoryProjectile = 1 << 1,       // 0010
    collisionCategoryDebris     = 1 << 2,       // 0100
    collisionCategoryGround     = 1 << 3        // 1000
};

@interface Util : NSObject

+ (NSInteger) randomWithMin:(NSInteger)min max:(NSInteger)max;

@end

I'm not 100% as to what is going on here but I would really appreciate some advice on how I can rectify this problem.

Maybe Amit Bijlani could also give some advice on how this could be rectified!

Thanking you in advance

Stu :)

2 Answers

That looks like a division by zero error. Check the place where you're calling the +randomWithMin:max: and see if you're passing min that's equal to max.

Hey Dino,

I'm assuming you mean:

+ (NSInteger) randomWithMin:(NSInteger)min max:(NSInteger)max;

In the THUtil.h?

I'm not super confident with Objective-C, I really should do the Deep-Dive for the Objective-C Course.

Cheers,

Stu :)

What you have in THUtil.h is the method declaration. THUtil.m holds its definition.

What I'm talking is the call site of that method. I don't remember exactly anymore, but I know it's called in the game scene implementation and quite possibly other places.

The call to the method (or sending the message) looks like this:

[THUtil randomWithMin: someNumber max: someOtherNumber];

I encourage you to go through Objective-C basics course to fully understand what is going on here.