0

接触点で衝突したときにボールをスピナーに貼り付けようとしています。ただし、連絡先が開始される前に didBeginContact が呼び出されているようです。下の画像は、両方が一緒に回転していることを示していますが、大きなスペースがあります.

ここに画像の説明を入力

コードは以下のとおりです。

#import "GameScene.h"

@implementation GameScene
@synthesize _flowIsON;

NSString *const kFlowTypeRed = @"RED_FLOW_PARTICLE";
const float kRED_DELAY_BETWEEN_PARTICLE_DROP = 0.01; //delay for particle drop in seconds

static const uint32_t kRedParticleCategory         =  0x1 << 0;
static const uint32_t kSpinnnerCategory            =  0x1 << 1;

NSString *const kStartBtn = @"START_BTN";
NSString *const kLever = @"Lever";

NSString *const START_BTN_TEXT = @"Start Game";

CFTimeInterval lastTime;


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

    _bkgNode = (SKSpriteNode *)[self.scene childNodeWithName:@"Background"];

    [self initializeScene];

}

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


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

        SKNode *node = [self nodeAtPoint:location];

        if ([node.name isEqualToString:kStartBtn]) {
            [node removeFromParent];

            //initalize to ON
            _flowIsON = YES;

            //[self initializeScene];
        }
    }
}

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

}

-(void)update:(CFTimeInterval)currentTime {

    float deltaTimeInSeconds = currentTime - lastTime;

    //NSLog(@"Time is %f and flow is %d",deltaTimeInSeconds, _flowIsON);

    if ((deltaTimeInSeconds > kRED_DELAY_BETWEEN_PARTICLE_DROP)) {


        //TBD
        SKAction *rotation = [SKAction rotateByAngle: M_PI/8.0 duration:0];
        [_spinner runAction:rotation];


        //only if its been past 1 second do we set the lasttime to the current time
        lastTime = currentTime;

    }



}


- (void) initializeScene {

    self.physicsWorld.contactDelegate = self;


    //create ball
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
    ball.size = CGSizeMake(50, 50);
    ball.position = CGPointMake(320, 1050);
    ball.zPosition = 1;
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
    ball.physicsBody.restitution = 0.0;
    ball.physicsBody.categoryBitMask = kRedParticleCategory;
    ball.physicsBody.contactTestBitMask = kSpinnnerCategory;
    ball.physicsBody.collisionBitMask = kSpinnnerCategory;
    ball.name = @"Ball";

    NSLog(@"Ball size is %f",ball.size.width);

    [self addChild:ball];



    //Create spinner
    _spinner = [SKSpriteNode spriteNodeWithImageNamed:@"Spinner"];
    _spinner.size = CGSizeMake(300, 300);
    _spinner.position = CGPointMake(320, 500);
    _spinner.zPosition = 1;
    _spinner.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:150];
    _spinner.physicsBody.affectedByGravity = NO;
    _spinner.physicsBody.allowsRotation = YES;
    _spinner.physicsBody.dynamic = NO;
    _spinner.physicsBody.restitution = 0.0;
    _spinner.physicsBody.categoryBitMask = kSpinnnerCategory;
    _spinner.physicsBody.contactTestBitMask = kRedParticleCategory;
    _spinner.physicsBody.collisionBitMask = kRedParticleCategory;
    _spinner.name = @"Spinner";

    [self addChild:_spinner];


    //create pipe

//    CGPoint center = CGPointMake(400, 600) ;
//    
//    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
//    [bezierPath addArcWithCenter:center radius:400   startAngle:1.825777 endAngle:2.011118 clockwise:YES];
//    [bezierPath addLineToPoint:center];
//    [bezierPath closePath];
//    
//    SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithPath:bezierPath.CGPath];
//    shapeNode.strokeColor = [UIColor whiteColor];
//    shapeNode.fillColor = [UIColor whiteColor];
//    [self addChild:shapeNode];

}






# pragma mark -- SKPhysicsContactDelegate Methods

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

    if ([contact.bodyA.node.name isEqualToString:@"Ball"] && [contact.bodyB.node.name isEqualToString:@"Spinner"]) {

        [self connectNode1:(SKSpriteNode *)contact.bodyA.node toNode2:(SKSpriteNode *)contact.bodyB.node withContact:contact];

    }

}


- (void)didEndContact:(SKPhysicsContact *) contact {
    //NSLog(@"didEndContact called");

}

- (void) connectNode1:(SKSpriteNode *)node1 toNode2:(SKSpriteNode *)node2 withContact: (SKPhysicsContact *)contact
{

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
                                                               bodyB:node2.physicsBody
                                                              anchor:node2.position];
    [self.physicsWorld addJoint:joint];
}


@end

did begin contact メソッドをコメントアウトすると、画像が衝突したときに画像が完全に重なり合っているため、画像のサイズが正しいことがわかります。

ここに画像の説明を入力

didEnterContact メソッドをコメントアウトしたときに、contact.contactPoint が両方のボディが衝突するポイントと同じではないのはなぜですか? 修正方法はありますか?

4

1 に答える 1