首页 文章

更改spritekit中的物理主体框架

提问于
浏览
2

我使用SpriteKit开发了一款iOS游戏(这种有用的框架可以快速制作游戏) . 我添加纹理并为主角配置物理身体作为图像

The first state: Stand Up

绿色矩形是物理身体的框架 . 我正在使用以下代码来创建它

@interface MainCharacter : SKSpriteNode

@end

@implementation MainCharacter

+ (instancetype)mainCharacterAtPosition:(CGPoint)pos {
    MainCharacter* mainChar = [[MainCharacter alloc] initWithTexture:[SKTexture textureWithImageNamed:@"stand_up"]];
    mainChar.position = pos;
    mainChar.xScale = 0.5f;
    mainChar.yScale = 0.5f;

    return mainChar;
}

- (instancetype)initWithTexture:(SKTexture *)texture {
    if (self = [super initWithTexture:texture]) {
        self.name = kCharacterName;

        self.anchorPoint = CGPointMake(0.5f, 0.0f);

        [self standup];

        CGSize spriteSize = self.size;
        CGPoint center = CGPointMake(spriteSize.width*(self.anchorPoint.x-0.5f), spriteSize.height*(0.5f-self.anchorPoint.y));

        self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteSize center:center];
        self.physicsBody.dynamic = NO;
        self.physicsBody.categoryBitMask = kCharacterCategory;
        self.physicsBody.contactTestBitMask = 0x0;
        self.physicsBody.collisionBitMask = 0x0;
    }
    return self;
}

- (void)standup {
    SKAction* standupAction = [SKAction setTexture:self.standupTexture resize:YES];
    [self runAction:standupAction];
}

- (void)standdown {
    SKAction* standownAction = [SKAction setTexture:self.standdownTexture resize:YES];
    [self runAction:standownAction completion:^{

    }];

    [self performSelector:@selector(standup) withObject:nil afterDelay:1.0f];
}

MainCharacter是一个继承自SKSPriteNode的类,只是一个管理主角的方便类 . 站起来是角色的第一个状态 . 我有另一个州,暂时叫做倒立(如下图所示)

Stand down state

我添加了向下滑动手势以使角色站立不动 .

The green rectangle also the physical body but it's too large for the character. I want to make a physical body frame as the red rectangle.

任何人都可以帮助我如何在我的角色站立时使身体变小,并在身体站立后扩大身体

2 回答

  • 0

    您可以销毁当前物理主体 self.physicsBody = nil; ,然后只需创建一个具有新大小要求的新物理主体 .

  • 0

    我通过为2个状态使用2个节点来解决这个问题(作为建议):站立状态和站立状态 . 我把它命名了

    standupNode和standdownNode

    首先,将standupNode添加到游戏场景中 . 如果滑动donw手势识别,我从游戏场景中删除standupNode并添加standdownNode . 相反,从游戏场景中删除standdownNode然后添加standupNode,如果角色站起来

相关问题