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 trialGhaz Sabh
5,005 Pointsshooting projectiles
Please help, I have spent 4 hours trying to find the issue, I'm at Shooting Projectiles using SKAction - Part 2 when I run my app work fine but I notes the projectiles they coming from the left bottom of the screen, not from the machine with cat.
7 Answers
Jonathan McCottry
6,606 PointsAHHHHHH is your Machine node named "machine" or Machine"?
Jonathan McCottry
6,606 Pointsin your game scene in your shootptojectiletowardsposition function, when you call your projectileatposition function from your projectile node class make sure you are using "machine.position.x" which gives you the x value of the center of the machine. Remember, the atposition functions in your game scene allow you to set a staring position for your nodes. I will let you figure out the y now.
Let me know if you need some more help
JMc
Ghaz Sabh
5,005 PointsHi Jonathan,
Thank you so much for your reply, still not working, not sure what i have done.
here are the code what i have done.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint position = [touch locationInNode:self]; [self ShootProjectileTowardsPosition:position]; }
}
-
(void) ShootProjectileTowardsPosition:(CGPoint)position { THSpaceCatNode spaceCat = (THSpaceCatNode)[self childNodeWithName:@"SpaceCat"]; [spaceCat performTap];
THMachineNode *machine = (THMachineNode *)[self childNodeWithName:@"machine"];
THProjectileNode *projectile = [THProjectileNode projectileAtPosition:CGPointMake (machine.position.x, machine.position.y+machine.frame.size.height-15)]; [self addChild:projectile]; [projectile MoveTowardsPosition:position];
Jonathan McCottry
6,606 Pointslets see your projectile node code, you may be overriding the position when you create the skspritenode
Ghaz Sabh
5,005 Pointswould you like me to email my project file?
import "THProjectileNode.h"
@implementation THProjectileNode
-
(instancetype) projectileAtPosition: (CGPoint)position { THProjectileNode *Projectile = [self spriteNodeWithImageNamed:@"projectile_1"]; Projectile.position = position; Projectile.name = @"Projectile";
[Projectile setupAnimation];
return Projectile;
}
- (void) setupAnimation { NSArray *textures = @[[SKTexture textureWithImageNamed:@"projectile_1"], [SKTexture textureWithImageNamed:@"projectile_2"], [SKTexture textureWithImageNamed:@"projectile_3"]]; SKAction *animation = [SKAction animateWithTextures:textures timePerFrame:0.1]; SKAction *repeatAction = [SKAction repeatActionForever:animation]; [self runAction:repeatAction];
}
-
(void) MoveTowardsPosition:(CGPoint)position { // slope = (Y3 - Y1) / (X3 - X1) float slope = (position.y - self.position.y) / (position.x - self.position.x);
// slope = (Y2 - Y1) / (X2 - X1) // Y2 - Y1 = slope ( X2 - X1 ) // Y2 = slope * X2 - slope * X1 + Y1
float offscreenX;
if ( position.x <= self.position.x ) { offscreenX = -10; } else { offscreenX = self.parent.frame.size.width + 10; }
float offscreenY = slope * offscreenX - slope * self.position.x + self.position.y;
CGPoint pointOffscreen = CGPointMake(offscreenX, offscreenY);
float distanceA = pointOffscreen.y - self.position.y; float distanceB = pointOffscreen.x - self.position.x;
float distanceC = sqrtf(powf(distanceA, 2) + powf(distanceB, 2));
// distance = speed * time // time = distance / speed
float time = distanceC / 400;
SKAction *moveProjectile = [SKAction moveTo:pointOffscreen duration:time]; [self runAction:moveProjectile];
}
Ghaz Sabh
5,005 PointsMate thank you so much, this never come to my mind, even in the video has lower m* it's working fine now.
Jonathan McCottry
6,606 PointsNo worries, if it cannot find your child node it stuffs it in the 0,0 value. Another thing you can do when you have this trouble is to NSLog your nodes to see where they exist on the screen.
Glad I could be of help. I actually think i had this problem too but I blocked it out....lol
Cheers to your continued success in IOS development!