这里是如何获得如何实现这样一个游戏的想法。
让我们创建一个我们要扔的球。首先,我们需要一个属性:
@property(nonatomic, strong) SKShapeNode *ball;
然后我们必须为我们的球创建一个SKShapeNode 并设置它的物理体:
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
_ball = [[SKShapeNode alloc] init];
// Create a circle.
CGMutablePathRef circle = CGPathCreateMutable();
CGPathAddArc(circle, NULL, 0,0, 60, 0, M_PI*2, YES);
// Set the shape of our ball and its color.
_ball.path = circle;
_ball.fillColor = [SKColor blueColor];
_ball.position = CGPointMake(200, 200);
// Create a circular physics body.
_ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
[self addChild:_ball];
// Create a physics body that borders the screen.
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// Set physicsBody of scene to borderBody.
self.physicsBody = borderBody;
}
return self;
}
然后让我们对具有所需angle 和magnitude 的球施加脉冲:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat angle = M_PI_4;
CGFloat magnitude = 1000;
[_ball.physicsBody applyImpulse:CGVectorMake(magnitude*cos(angle),
magnitude*sin(angle))];
}
在您的实现中,您需要在 touchesBegan 和 touchesMoved 方法中计算角度和幅度值,并在 touchesEnded 中应用脉冲。
这应该给你一个启动。希望对您有所帮助。