Sleep Paralysis
Sleep Paralysis

Reputation: 469

Passing iOS sprites from one class to another

I have a Spritekit project in xcode on my main screen. However I would really like to manage it from another class so as to not cluster my main. So I added a new class to manage and handle it. Sadly I can't seem to get it to show in my main (or I might be doing it wrong). My myscene.m consists of pretty much the same code including the spaceship and this is where I moved the code too ( I didn't bother adding the methods like touchesBegan)

//  joysitck.m
//  controlpad
//


#import "joysitck.h"


@implementation joysitck
{
    BOOL controlButtonOn;
}
float controlx=200;
float controly=200;
float touchX = 0.0;
float touchY=0.0;


- (instancetype)init
    {
        if (self = [super init]) {
            self.name = @"controlPadNode";

        SKSpriteNode *controlPadNode = [SKSpriteNode spriteNodeWithImageNamed:@"game-controller-frontb"];

        controlPadNode.position = CGPointMake(controlx,controly);
        [controlPadNode setScale:0.5];
        controlPadNode.name = @"controlPadNode";
        controlPadNode.zPosition = 1.0;

        [self addChild:controlPadNode];
    }
    return self;
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        touchX = location.x;
        touchY = location.y;
        controlButtonOn= true;
    }
}

//when touch moves
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        touchX = location.x;
        touchY = location.y;
        controlButtonOn= true;
    }
}

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

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //if control pad touched
    if ([node.name isEqualToString:@"controlPadNode"]) {
        controlButtonOn= false;
    }
}

//update method
-(void)update:(NSTimeInterval)currentTime {

    if (controlButtonOn) {

        //the control pad
        SKNode *controlNode = [self childNodeWithName:@"controlPadNode"];
        SKNode *node = [self childNodeWithName:@"spaceShipNode"];     
        float angle = atan2f (touchY - controly, touchX - controlx) ;
        SKAction *moveShip=[SKAction moveByX:6*cosf(angle) y:0 duration:0.005];
        [node runAction: moveShip];

    }
}

@end

Upvotes: 0

Views: 133

Answers (1)

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

You haven't added joystick node to the scene.

Add this code to MyScene's initWithSize method (before [self addship]):

joysitck *joysitckNode = [[joysitck alloc] init];
[self addChild:joysitckNode];

Upvotes: 1

Related Questions