mercredi 1 avril 2015

Objective-C Sprite Kit – Continue a line made to bounce off the walls and create a sort of reflection

Well . . . I guess I'm just bad at putting trigonometry and code together ;)


So, I had another discussion here on StackOverflow that helped me out with how to code a dotted line appearing between a certain point on the screen, and where my finger is. So when I moved my finger, the dotted line will have one end at the designated spot, but the other end will follow my finger.


What I'm having problems with is this: I want the dotted line to continue past my finger and stay on the same path. When it runs into the Simulator's (or device's) edges, it'll reflect off of it. When I move my finger, the reflection will follow my finger to create the path it would if it were to reflect in that different – new – path.


Anyone have any ideas? I have this wonderful Ray Wenderlich link, but I can't seem to wrap my mind around it. If this is what I want, could someone please try to explain it a little differently than how this guy does? Thanks a ton!


http://ift.tt/18OKJuU


EDIT:


Here's the code for the project I already have:



#import "GameScene.h"



@implementation GameScene {
SKShapeNode *line;
}

-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */

line = [SKShapeNode node];
[self addChild:line];
[line setStrokeColor:[UIColor redColor]];

}

-(void)drawLine:(CGPoint)endingPoint {

CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
CGPathAddLineToPoint(pathToDraw, NULL, endingPoint.x,endingPoint.y);

CGFloat pattern[2];
pattern[0] = 20.0;
pattern[1] = 20.0;
CGPathRef dashed =
CGPathCreateCopyByDashingPath(pathToDraw,NULL,0,pattern,2);

line.path = dashed;

CGPathRelease(dashed);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];

[self drawLine:location];

}
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];

[self drawLine:location];

}
}

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

line.path = nil;

}


-(void)update:(NSTimeInterval)currentTime {

}


@end





Aucun commentaire:

Enregistrer un commentaire