【问题标题】:Draw the Path of a Moving SKSpritenode绘制移动 SKSpritenode 的路径
【发布时间】:2013-12-24 15:44:23
【问题描述】:

我正在尝试使用新的 SKSpritenode ,我设法创建了一个 Spritenode 在屏幕上移动它,尽管我希望 Spritenode 在它移动的地方留下痕迹(颜色)。

创建 Sprite 节点的代码和我尝试创建一个 shapenode 作为 spritenode 的子节点(这不起作用)。

-(void)movetherayoflight{

    ray1=[[lightray1 alloc]initWithColor:[SKColor redColor] size:CGSizeMake(6,6)];
    [ray1 setPosition:CGPointMake(50, 50)];
    ray1.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:ray1.size];
    ray1.physicsBody.restitution=1;
    ray1.physicsBody.linearDamping=0;
    ray1.physicsBody.friction=0;
    ray1.physicsBody.allowsRotation=NO;
    ray1.physicsBody.velocity=CGVectorMake(0.0f, 300.0f);
    [self addChild:ray1];

    SKShapeNode *raayyy=[[SKShapeNode alloc]init];
    CGMutablePathRef rayPath = CGPathCreateMutable();

    CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
    CGPoint startpoint=CGPointMake(50, 100);
    //CGPathAddLines(rayPath, NULL, &fff, 2);
    CGPathAddLines(rayPath, NULL, &startpoint, 5);
    //CGPathAddLineToPoint(rayPath, NULL, ray1.position.x, ray1.position.y);
    raayyy.path=rayPath;
    raayyy.lineWidth = 1.0;
    //raayyy.fillColor = [SKColor whiteColor];
    raayyy.strokeColor = [SKColor greenColor];
    raayyy.glowWidth = 0.5;
    [ray1 addChild:raayyy];


}

如果您有更好的解决方案,请告诉我!

【问题讨论】:

    标签: sprite-kit cgpath skspritenode skshapenode


    【解决方案1】:

    不要将 SKShapeNode 作为 SKSpriteNode 的子节点,而是将其声明为 SKScene 中的兄弟节点。

    首先,将 SKShapeNode 和 CGPath 声明为实例变量。

    在场景的-initWithSize: 方法中,

    raayyy=[[SKShapeNode alloc]init];
    //Additional initialization here.
    
    rayPath = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, ray1.position.x, ray1.position.y);
    rayyy.path = rayPath;
    [self addChild:rayyy];
    

    然后在您的-update: 方法中,

    CGPathAddLineToPoint(rayPath, NULL, yar1.position.x, ray1.position.y);
    rayyy.path = rayPath;
    

    这只是一个建议,我自己没有尝试过类似的东西。

    【讨论】:

      【解决方案2】:

      好吧,我已经用一个解决方案来管理它,但到目前为止我真的不喜欢它

          -(SKShapeNode*)gravityline{
          SKShapeNode *lolo = [[SKShapeNode alloc] init];
          CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
          CGMutablePathRef path = CGPathCreateMutable();
          CGPathMoveToPoint(path, NULL, fff.x, fff.y);
          CGPathAddLineToPoint(path, 0,rayoriginpoint.x,rayoriginpoint.y );
          CGPathCloseSubpath(path);
          lolo.path = path;
          lolo.name=@"gravityline";
          lolo.strokeColor=[SKColor greenColor];
          lolo.glowWidth=.1;
          lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];
          lolo.physicsBody.categoryBitMask=raylightCategory;
          lolo.physicsBody.collisionBitMask=batCategory;
          lolo.physicsBody.contactTestBitMask=batCategory;
          lolo.physicsBody.dynamic=NO;
          CGPathRelease(path);
      
          return lolo;
      }
      

      一旦射线点击中某物,rayoriginpoint就会改变它的值

      if (firstBody.categoryBitMask == rayCategory && secondBody.categoryBitMask==mirrorCategory )
      {
          [self addChild:[self gravityline]];
          CGPoint p = contact.contactPoint;
          rayoriginpoint=p;
      
          NSLog(@"Contact have been made");
      }
      

      我想做的是非常基本的: 1-改变SKSpritenode经过的每个CGPOINT的颜色 2-要么创建在某个方向上放大的 Sprite 节点,直到它撞到某物然后改变方向

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多