0

我被这个碰撞问题吓坏了:快速平移时,平移的球会从边缘循环体中脱落。有人有类似的问题吗?这是说明问题的场景代码。(替换 xcode 示例中的 MyScene - spritekitgame)

#import "SAMyScene.h"

/* Bitmask for the different entities with physics bodies. */
typedef enum : uint32_t {
    SAColliderTypeBall             = 0x1 << 1
    ,SAColliderTypeEdge            = 0x1 << 3
} SAColliderType;

NSString * const SABallName = @"ballNode";

@interface SAMyScene()
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, strong) SKShapeNode *ball;
@end
@implementation SAMyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        NSLog(@"Ball categorie bitmask =%d", SAColliderTypeBall);
        NSLog(@"Edges categorie bitmask =%d", SAColliderTypeEdge);

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsBody.categoryBitMask = SAColliderTypeEdge;
        self.physicsBody.usesPreciseCollisionDetection = YES;
        self.ball = [self newBallNode];
        [self addChild:self.ball];
    }
    return self;
}

- (SKShapeNode *)newBallNode {

    SKShapeNode *ball = [[SKShapeNode alloc] init];
    CGMutablePathRef myPath = CGPathCreateMutable();
    CGFloat radius = 60.0;
    CGPathAddArc(myPath, NULL, 0,0, radius, 0, M_PI*2, YES);
    ball.path = myPath;
    ball.fillColor = [SKColor yellowColor];
    ball.name = SABallName;
    ball.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
    ball.physicsBody.affectedByGravity = NO;
    ball.physicsBody.dynamic = YES;
    ball.physicsBody.categoryBitMask = SAColliderTypeBall;
    ball.physicsBody.contactTestBitMask = SAColliderTypeEdge;
    ball.physicsBody.collisionBitMask = SAColliderTypeEdge;
    ball.physicsBody.usesPreciseCollisionDetection = YES;
    return ball;
}

-(void)update:(CFTimeInterval)currentTime {
    //Called before each frame is rendered
}

- (UIPanGestureRecognizer *)panGestureRecognizer {
    if (!_panGestureRecognizer) {
        _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                        action:@selector(handlePanFrom:)];
    }
    return _panGestureRecognizer;
}

- (void)didMoveToView:(SKView *)view {

    if (![[self view].gestureRecognizers containsObject:self.panGestureRecognizer]) {
        [[self view] addGestureRecognizer:self.panGestureRecognizer];
    }
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

    if (recognizer.state == UIGestureRecognizerStateBegan) {
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(translation.x, - translation.y);
        [self panForTranslation:translation];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
    }
}

- (void)panForTranslation:(CGPoint)translation {
    CGPoint position = self.ball.position;
    CGPoint newPos = CGPointMake(position.x + translation.x, position.y);
    self.ball.position = newPos;
}

@end
4

0 回答 0