【问题标题】:Sling shot using SpriteKit? [closed]使用 SpriteKit 进行弹弓射击? [关闭]
【发布时间】:2014-02-25 19:59:26
【问题描述】:

我正在开发使用SpriteKit 开发的基于弹弓的游戏。 但是,我不知道如何实现物理。

任何人都可以提供有关如何开始实施它的任何提示吗?

【问题讨论】:

  • 嗨,Almo,我觉得你对我太苛刻了。这应该是一个支持社区,不仅对像你这样的专业人士,而且对像我这样的傻瓜也是如此。如果像我这样的初学者害怕提问,这个网站将会关闭,你将独自留在这里庆祝你的伟大......是的,我试图寻找其他解决方案,但我发现不合适到 SpriteKit。
  • 这里应该问什么问题的定义很窄。 stackoverflow.com/help 阅读。我们非常支持遵守规则的人。您的问题并未表明您对此进行过任何研究。我们期望的是“我在我的游戏中实现物理时遇到了问题。这是我尝试过的:-小代码示例-但镜头没有移动。

标签: ios sprite-kit


【解决方案1】:

这里是如何获得如何实现这样一个游戏的想法。

让我们创建一个我们要扔的球。首先,我们需要一个属性:

@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;
}

然后让我们对具有所需anglemagnitude 的球施加脉冲:

-(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))];
}

在您的实现中,您需要在 touchesBegantouchesMoved 方法中计算角度和幅度值,并在 touchesEnded 中应用脉冲。

这应该给你一个启动。希望对您有所帮助。

【讨论】:

  • 谢谢,这确实是一个很好的开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多