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 Build a Game with Sprite Kit Actions and Animations Shooting Projectiles using SKAction: Part 2

SpaceCats could fail

In video "Shooting Projectiles using SKAction: Part 2 14:37" the slope of a line is calculated

float slope = (position.y - self.position.y) / (position.x - self.position.x);

However, if the user taps on a point directly above the projectile node (i.e. position.x == self.position.x), the slope is undefined.

I haven't tested this yet, but it seems like you'll get a runtime error when the program tries to divide by 0 to calculate the slope.

In this case, it might be easiest to change

if ( position.x <= self.position.x)

into an if and an else if

if ( position.x == self.position.x) //vertical line case
  { 
  // calc distance using something like distanceC = self.parent.frame.size.height + 10 - self.position.y;
  }
else if (position.x < self.position.x )
{
 // calculate like in video
}

I know this isn't exactly correct, but it gives the idea.

I've learned to be careful when working with lines because we've been taught these simple equations that are often so easy that we forget to think about how they work.

1 Answer

I searched "Spacecats" and your post came up...and I thought it was a good point! If you click directly where the fireballs shoot from - no fireball gets fired. Great observation!