ios - Spritekit物理: One ball hit another ball

标签 ios sprite-kit game-physics

我是 SpriteKit 的新手。

我正在尝试解决这个问题: 我的场景中有两个球,当我拖动一个球并“击中”另一个球时,这个球应该使用正确的物理原理滚开。

在我的测试代码中我只能“移动”第二个球,他没有使用击球的“力”...

这是我的场景代码:

#import "HittingScene.h"

@interface HittingScene()

@property (nonatomic, strong) SKShapeNode *targetBall;
@property (nonatomic, strong) SKShapeNode *mainBall;
@property (nonatomic, weak) SKShapeNode *draggedNode;

@end

@implementation HittingScene

- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size]) {
        [self addChild:[self createMainBall]];
        [self addChild:[self createTargetBall]];

        [self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]];
    }
    return self;
}

- (SKShapeNode *)createTargetBall
{
    self.targetBall = [SKShapeNode node];

    CGPathRef path = CGPathCreateWithEllipseInRect((CGRect){{-20, -20}, {40, 40}}, NULL);
    [self.targetBall setPath:path];
    CGPathRelease(path);

    [self.targetBall setPosition:CGPointMake(200, 200)];
    [self.targetBall setName:@"targetBall"];

    [self.targetBall setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:20.0]];
    self.targetBall.physicsBody.dynamic = YES;
    self.targetBall.physicsBody.affectedByGravity = NO;
    self.targetBall.physicsBody.restitution = 0.7;

    return self.targetBall;
}

- (SKShapeNode *)createMainBall
{
    self.mainBall = [SKShapeNode node];

    CGPathRef path = CGPathCreateWithEllipseInRect((CGRect){{-20, -20}, {40, 40}}, NULL);
    [self.mainBall setPath:path];
    CGPathRelease(path);

    [self.mainBall setPosition:CGPointMake(100, 100)];
    [self.mainBall setName:@"mainBall"];

    [self.mainBall setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:20.0]];
    self.mainBall.physicsBody.dynamic = YES;
    self.mainBall.physicsBody.affectedByGravity = NO;
    self.mainBall.physicsBody.restitution = 0.7;

    return self.mainBall;
}

- (void)touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
{
    self.draggedNode = (SKShapeNode *)[self nodeAtPoint:[[touches anyObject]     locationInNode:self]];
}

- (void)touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event
{
    self.draggedNode.position = [[touches anyObject] locationInNode:self];
}

- (void)touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
{
    self.draggedNode = nil;
}
@end

有人知道如何解决这个问题吗?

谢谢, 乌尔克曼

最佳答案

我认为问题在于您只是在触球时改变球的位置。这样做只是将节点放置在一个新点上,实际上并不涉及任何力。您需要设置一个碰撞系统,当主球与目标球碰撞时,该系统会向目标球施加一定的力。

applyForce:atPoint:SKPhysicsBody 中看起来是一个不错的起点。

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/Reference/Reference.html

关于ios - Spritekit物理: One ball hit another ball,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23559364/

相关文章:

ios - SpriteKit 中的 "nonuniformly scaled texture"是什么意思?

unity-game-engine - 防止物体侧翻;确保它始终面朝上或面朝下落地

ios - 找不到 Cordova iOS 插件

ios - inputaccessoryview 未显示( Storyboard)

objective-c - 如何在 parse.com 过滤 ios 中的 boolean 字段上使用 pfquery?

swift - 使用 Sprite Kit 在 Swift 中隐藏或显示 Google Ad View

swift - 在调用 super.init() 之前实例化调用自身的属性

graphics - 2D 平台游戏 : why make the physics dependent on the framerate?

ios - 如何在SimCity 5中实现建筑物移动时摇摆的物理效果?

ios - Alamofire 后台服务,全局经理?全局授权 header ?