适用于相对球尺寸的力

时间:2014-12-24 10:39:13

标签: objective-c iphone vector sprite-kit

在我的代码中,我有一个开始下降的球,每次点击它都会应用下面的代码。这完全适用于iPhone,但我也在开发iPad。现在在iPad上,球必须明显更大,我也希望同样的效果也适用于那些球。我试着用数学来得到球的大小的相对力量,我知道我的数学运算是正确的,但必须有一个更简单的方法来实现这一点。任何帮助将不胜感激!

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

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

// if ball is tapped apply impulse
if ([node.name isEqualToString:@"ball"]) {
    node.physicsBody.velocity = CGVectorMake(0, 15);
    [node.physicsBody applyImpulse:
     CGVectorMake(0, 50)];

    [GameState sharedInstance].easyScore += 1;
    [_lblEasyScore setText:[NSString stringWithFormat:@"%d", [GameState sharedInstance].easyScore]];

    NSLog(@"ball pressed");
}
}

1 个答案:

答案 0 :(得分:1)

//Create private CGVector variable.
CGVector ballApplyImpulse;

//In your start method.(initWithSize, didMoveToView etc)
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )//iPad Detected
{
    ballApplyImpulse = CGVectorMake(0, 100);//I haven't tested values. You can test yourself for true values.
}
else//iPhone Detected
{
    ballApplyImpulse = CGVectorMake(0, 50);
}

//In your on touch method
if ([node.name isEqualToString:@"ball"]) {
    node.physicsBody.velocity = CGVectorMake(0, 15);
    [node.physicsBody applyImpulse:ballApplyImpulse;

    [GameState sharedInstance].easyScore += 1;
    [_lblEasyScore setText:[NSString stringWithFormat:@"%d", [GameState sharedInstance].easyScore]];

    NSLog(@"ball pressed");
}
}