将iOS精灵从一个类传递到另一个类

时间:2014-01-17 20:44:27

标签: ios sprite-kit

我在主屏幕上的xcode中有一个Spritekit项目。但是我真的想从另一个类来管理它,以便不集中我的主要。所以我添加了一个新类来管理和处理它。可悲的是,我似乎无法让它显示在我的主要(或者我可能做错了)。我的myscene.m包含几乎相同的代码,包括太空船,这也是我移动代码的地方(我没有费心添加像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

1 个答案:

答案 0 :(得分:1)

您尚未将joystick节点添加到场景中。

将此代码添加到MyScene的{​​{1}}方法(initWithSize之前):

[self addship]