0

I am Trying to use the new SKSpritenode , i have managed to create a Spritenode move it around the screen , although i want the Spritenode leaves a trace( color ) where it moves.

the code to create the Sprite node & my attempts to create a shapenode as a child for the spritenode ( which did not work ) .

-(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];


}

If you have better Solution , Please let me know !

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
TALAA
  • 6,184
  • 1
  • 13
  • 21

2 Answers2

1

Instead of making the SKShapeNode as the child of the SKSpriteNode, declare it as its sibling in the SKScene.

First, declare the SKShapeNode and the CGPath as instance variables.

In your scene's -initWithSize: method,

raayyy=[[SKShapeNode alloc]init];
//Additional initialization here.

rayPath = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, ray1.position.x, ray1.position.y);
rayyy.path = rayPath;
[self addChild:rayyy];

Then in your -update: method,

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

This is just a suggestion, I haven't tried anything of the like myself.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
0

well , I have managed it with a solution but i really dont like it so far

    -(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;
}

The rayoriginpointchanges its value once the ray point hits something

if (firstBody.categoryBitMask == rayCategory && secondBody.categoryBitMask==mirrorCategory )
{
    [self addChild:[self gravityline]];
    CGPoint p = contact.contactPoint;
    rayoriginpoint=p;

    NSLog(@"Contact have been made");
}

What i want to do is very basic : 1- Either to change the Color of every CGPOINT that the SKSpritenode Passes by 2- Either to create Sprite node that scales up in certain direction till it hits something then changes direction

TALAA
  • 6,184
  • 1
  • 13
  • 21