如何制作几个精灵节点,每个节点都有一个特殊的路径可供遵循

时间:2015-05-30 15:05:50

标签: ios objective-c cocoa-touch sprite-kit

我正在尝试制作像飞行控制这样的游戏,其中有数量的汽车,每个都有自己的路径。我的想法是制作一个汽车类的子类型SKNode,并为每个对象都有一个CGMutablePathRef实例变量。我可以使用touchsBegins,touchsMoved和touchesEnded创建CGMutablePathRef,然后我可以让每个对象跟随SKAction followPath的特定路径。这是一个好方法吗?如果不是,你会怎么做?这就是我目前为Vehicle类所做的:

Vehicle.h

@interface Vehicle: SKNode

@property (nonatomic, assign) CGMutablePathRef pathToFollow;
@property (nonatomic) CGFloat speed;
@property (nonatomic) int direction;

- (instancetype)initWith:(CGMutablePathRef)pathPassed;

@end

Vehicle.m

@implementation Vehicle : SKNode

@synthesize pathToFollow;
@synthesize speed;
@synthesize direction;


- (id)init
{
    self = [super init];
    if (self) {
        SKSpriteNode *vehicle = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        [self addChild:vehicle];
        [self setDirection:4];
        [vehicle setName:@"car"];
        [self setScale:.5];
    }
    return self;
}
- (id)initWith:(CGMutablePathRef)pathPassed {

    self = [super init];
    if (self) {
        SKSpriteNode *vehicle = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        [self addChild:vehicle];
        [self setPathToFollow:pathPassed];
        [self setDirection:4];
        [vehicle setName:@"car"];
        [self setScale:.1];
    }

    return self;
}

@end

编辑:添加GameScene希望它会有所帮助。我现在的实现有一个问题,其中Vehicle对象跟随路径,但只有在它跳到第一个点的坐标+场景上的节点位置之后(例如:position =(200,200),第一个点) =(123,456)它将转到(323,656)然后按照路径)。它绘制了我想要它的行,但随后它从这个坐标开始。再次感谢您的帮助!

GameScene.m

-(void)didMoveToView:(SKView *)view {
    _car = [[Vehicle new] init]; // init the Vehicle object then set position
    [_car setPosition:CGPointMake(self.scene.size.width/2, self.scene.size.height/2)];
    [self addChild_car]; // add the child to the scene
}

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

    // get the node that was touched
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    _nodeTouchedFirst = [self nodeAtPoint:positionInScene];

    if ([_nodeTouchedFirst.name isEqualToString:@"car"]) {
        if (lineNode != NULL) { // remove the previous path
            [lineNode removeFromParent];
            [CGPathRelease(pathToDraw);
        }

        //Start a new path and display it on the screen
        pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
        lineNode = [SKShapeNode];
        lineNode.path = pathToDraw;
        lineNode.strokeColor = [SKColor redColor];
        [self addChild:lineNode];


    }
} // touchesBegan

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    if ([_nodeTouchedFirst.name isEqualToString:@"car"]) {
        UITouch* touch = [touches anyObject];
        CGPoint positionInScene = [touch locationInNode:self];

        CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
        lineNode.path = pathToDraw;
    }
} // touchesMoved

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([_nodeTouchedFirst.name isEqualToString:@"car"]) {
        SKAction *followPath = [SKAction followPath:pathToDraw asOffset:YES orientToPath:NO duration:2];
        [_nodeTouchedFirst runAction:followPath];
        lineNode.strokeColor = [SKColor grayColor];
    }

}

1 个答案:

答案 0 :(得分:0)

一些可能有帮助或可能没有帮助的观察结果:

  • 您正在设置_car的位置并将其添加到场景中,然后再为其指定路径。
  • 您引用了_nodeTouchedFirstnodeTouchedFirst"car""car1"
  • 您正在使用asOffset:YES, orientToPath:NO, duration:2(而不是asOffset:NO, orientToPath:YES, speed:2)跟踪路径。
  • directionSKNode zRotation属性不对应。