iOS SpriteKit - 光线(线)从边界反射

时间:2015-03-28 07:56:31

标签: ios iphone sprite-kit game-physics

我们有行(光线)移动到边界并在达到绑定时反映。这些图像展示了运动和反射的动态。

enter image description here

enter image description here

enter image description here

我想在SpriteKit中实现它(obj-c更喜欢)但不明白我应该从哪个角度开始。

1 个答案:

答案 0 :(得分:0)

我找到了如何实现它。希望它对其他人有用

    #import "GameScene.h"

static const float GUIDE_MASS =  .0015;
static const int SEGMENTS_COUNT = 10;
static const int SEGMENT_LENGTH = 5;

@interface GameScene(private)
-(void) createGuidesAndShot;
@end

@implementation GameScene

SKShapeNode *shot;
NSMutableArray* shotSegments;

-(void)didMoveToView:(SKView *)view {
    [self setBackgroundColor:[SKColor whiteColor]];
}

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

}

-(void)update:(CFTimeInterval)currentTime {

    CGMutablePathRef pathToDraw = CGPathCreateMutable();

    if (shotSegments!=nil) {
        bool isFirst = YES;
        for (SKNode* segment in shotSegments) {
            if(isFirst){
                CGPathMoveToPoint(pathToDraw, NULL,
                                  segment.position.x,
                                  segment.position.y);
                isFirst =NO;
            } else {
                CGPathAddLineToPoint(pathToDraw, NULL,
                                     segment.position.x,
                                     segment.position.y);
            }
        }
        shot.path = pathToDraw;
    }

}

-(void)didBeginContact:(SKPhysicsContact *)contact {

    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (shotCategory|screenBoundsCategory)){


    }
}


-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {

        shotSegments = [NSMutableArray new];

        self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
        SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsBody = borderBody;

        self.physicsBody.friction = 0.0f;
        self.physicsBody.categoryBitMask = screenBoundsCategory;
        self.physicsBody.contactTestBitMask = shotCategory;

        self.physicsWorld.contactDelegate = self;

        [self createGuidesAndShot];

    }

    return self;

}

-(void) createGuidesAndShot{
    for (int i = 0; i<SEGMENTS_COUNT*SEGMENT_LENGTH; i+=SEGMENT_LENGTH) {

        SKShapeNode* guide = [SKShapeNode shapeNodeWithCircleOfRadius:1];

        guide.position = CGPointMake(self.frame.size.width/2+i,
                                     self.frame.size.height/2-i);

     //   guide.fillColor = [SKColor blackColor];
        guide.name = [NSString stringWithFormat:@"guide%i", i];

        [self addChild:guide];
        [shotSegments addObject:guide];

        guide.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:guide.frame.size.width/2];

        guide.physicsBody.friction = 0.0f;
        guide.physicsBody.restitution = 1.0f;
        guide.physicsBody.linearDamping = 0.0f;
        guide.physicsBody.allowsRotation = NO;

        guide.physicsBody.categoryBitMask = shotCategory;
        guide.physicsBody.contactTestBitMask = screenBoundsCategory;
        guide.physicsBody.collisionBitMask = screenBoundsCategory;

        guide.physicsBody.mass = GUIDE_MASS;
        [guide.physicsBody applyImpulse:CGVectorMake(0.1f, -0.1f)];

    }

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

@end