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 trialJason Meinzer
3,679 PointsWhy are my projectile sprites moving so ridiculously slow, at a snails pace?
I've been troubleshooting for about two hours, and have watched this video and the one prior 3x each to triple check all my work. When I run the app on the simulator for a 4s, 5, 5s or on my actual iPhone 5s, I touch the screen all over and projectiles are created, but they literally move at a snails pace and take literally about 40 seconds to get to the edge of the screen. What's even weirder, is sometimes out of nowhere one will actually move at what seems to be normal speed (as the course video shows it should).
So, basically, imagine clicking the screen like 20 times - what I get is about 20 projectiles just like floating in the air like bubbles, each taking about 40 seconds to get to the edge of the screen. So odd. They do seem to be following the correct path, though, based off where I touched the screen.
Does anybody have any suggestions on how to fix this? There aren't any code errors. And yes, I've played around with the speed in the THProjectileNode.m file by altering the "float time = distanceC / 100;" to "float time = distanceC / 400;", etc. and the only way the speed of the projectile gets anywhere close to what it should be (based off the results in the course video) is if I alter the code to read ""float time = distanceC / 50000;". However, again - it's not consistent - only most of the projectiles fly at what seems to be a normal speed and like 1/5 of them will zip across the screen almost too fast for the eye to even see.
Please help. Thanks!
If it helps, here's the copy/paste from my THProjectNode.m file:
....
-
(void) moveTowardsPosition:(CGPoint)position { //passing the position of where the user touches the screen through this method //slope = (Y3 - Y1) / (X3 - X1) float slope = (position.y - self.position.y)/(position.x - self.position.x); //just FYI, self.position is the position of our projectile //slope = (Y2 - Y1) / (X2 - X1) //Y2 - Y1 = slope (X2 - X1) //Y2 = slope * X2 - slope * X1 + Y1
float offscreenX; //point where we want the projectile to end up off the screen if (position.x <= self.position.x) { //means touchpoint is on left side of screen because X1 is where the projectile originates from which is the astrocat's projectile launcher, but if it's greater than X1 that means it's on the right side of the screen at which point we'll have to just add 10 pixels or w/e to the width of the screen, but the left side is easy, we can just say -10 offscreenX = -10; } else { offscreenX = self.parent.frame.size.width + 10; //basically we're getting the frame size ie.. the width of the parent node, which is in this case is the scene }
float offscreenY = slope * offscreenX - slope * self.position.x + self.position.y; CGPoint pointOffscreen = CGPointMake(offscreenX, offscreenY); //we're making a point offsceen
float distanceB = pointOffscreen.x - self.position.x; float distanceA = pointOffscreen.y - self.position.y;
//so now that we have these two we can now we can calculate distanceC....
float distanceC = sqrtf(powf(distanceA, 2) * powf(distanceB, 2));
//distance = speed * time //time = distance / speed //so now we need to figure out how long projectile would take to travel from that point to the point off screen, given that we have the distance
float time = distanceC / GameProjectileSpeed; //in the course this was set to 400, but that wasn't working for me because the projectiles were moving at a snails pace, so I just updated it to an arbitrary, very large number.... likely will have to figure out the root of this problem and fix it down the road....
SKAction *moveProjectile = [SKAction moveTo:pointOffscreen duration:time]; //projectile has to move at a constant speed to that position [self runAction:moveProjectile];
}
@end
1 Answer
Jason Meinzer
3,679 PointsDebugging and stepping through each line to see the values of each variable got me my answer after another hour or so.
Turns out, super simple fix. Changed "float distanceC = sqrtf(powf(distanceA, 2) * powf(distanceB, 2));" to "float distanceC = sqrtf(powf(distanceA, 2) + powf(distanceB, 2));" and the distances were being calculated correctly, thus the time and speed were accurate as a result.
Not sure why I had a * instead of a +.
All fixed!