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

Shaun Kelly
Shaun Kelly
5,648 Points

Projectile not working ?

Projectile.m

#import "Projectile.h"

@implementation Projectile




+(instancetype)projectileAtPostion:(CGPoint)position {

    Projectile *projectile = [Projectile spriteNodeWithImageNamed:@"projectile_1"];
    projectile.position = position;
    return projectile;






}

-(void) moveTowardsPosition:(CGPoint)position {

    //GUIDLINES
    // slope = (Y2 - Y1) / (X2 - X1)
    // Y2 - Y1 = slope (X2 - X1)
    // Y2 = slope * X2 - slope * X3 + Y3

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

    float offscreenX;

    if (position.x <= self.position.x) {
        offscreenX = -10;
    }
    else {
        offscreenX = self.parent.frame.size.width + 10;
        //spfsw
    }




    float offscreenY = slope * position.x - slope * self.position.x + self.position.y;


    CGPoint offScreenPosition = CGPointMake(offscreenX, offscreenY);



    //DISTANCE
    //A + B = c
    float distanceA = offScreenPosition.y - self.position.y;
    float distanceB = offScreenPosition.x - self.position.x;
    float distanceC = sqrtf(powf(distanceA, 2) + powf(distanceB, 2));


    //SPEED
    //distance = speed * time

    //time = distance / speed
    float time =  distanceC / 400;

    SKAction *moveProjectile = [SKAction moveTo:offScreenPosition duration:time];
    [self runAction:moveProjectile];





}


@end

GamePlayScene.m

#import "GamePlayScene.h"
#import "Machine.h"
#import "SpaceCat.h"
#import "Projectile.h"
@implementation GamePlayScene

-(void)didMoveToView:(SKView *)view {

    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"background_1"];
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:background];



    Machine *machine = [Machine machineAtPosition:CGPointMake(CGRectGetMidX(self.frame), 13)];
    [self addChild:machine];


    SpaceCat *spaceCat = [SpaceCat spaceCatAtPosition:CGPointMake(CGRectGetMidX(self.frame), machine.position.y-3)];
    [self addChild:spaceCat];


}



-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        [self shootProjectileTowardsPosition:location];

    }


    SpaceCat *spaceCat = (SpaceCat*)[self childNodeWithName:@"SpaceCat"];
    [spaceCat runAction];


}


-(void) shootProjectileTowardsPosition:(CGPoint)position {

    Machine *machine = (Machine*) [self childNodeWithName:@"Machine"];



    Projectile *projectile = [Projectile projectileAtPostion:CGPointMake(machine.position.x, machine.position.y+machine.frame.size.width+7)];


    [self  addChild:projectile];
    [projectile moveTowardsPosition:position];


}




@end