gpt4 book ai didi

ios - SpriteKit SKPhysicsBody bodyWithTexture 颠倒了

转载 作者:可可西里 更新时间:2023-11-01 03:34:22 29 4
gpt4 key购买 nike

您好,我正在尝试修复这个 spritekit 的物理形状倒置的错误。

[SKPhysicsBody bodyWithTexture:monsterTexture size:monsterTexture.size]

怪物第一次出现时,物理 body 方向是正确的。但是第二次和之后的每一次怪物出现,它的物理 body 都沿着 Y 轴倒转。查看图片,其中 skView.showsPhysics = true; 显示物理形状。它第一次正常工作的事实让我觉得也许我不知道的一些属性正在被修改或什么的。

我想将 2 个物理 block 放在 Boot 的粗略形状中,但这并不理想,因为我想在其他一些更复杂的形状上使用 bodyWithTexture,它们也遇到了同样的错误。

我还尝试了 bodyWithTexture:monsterTexture,原始 SKTexture 对象而不是 bodyWithTexture:monster.texture,Monster 对象的纹理。

physics body upside down

这是我添加怪物的代码。如果您需要更多,请告诉我。

- (Monster *)monster:(NSDictionary *)settings
{
NSDictionary * monsterDefaults = [self monsterDefaults];
NSDictionary * monsterConfig = [self monsterConfig:settings[TYPE]];
SKTexture * monsterTexture = monsterConfig[TEXTURE] ? monsterConfig[TEXTURE] : monsterDefaults[TEXTURE];
Monster * monster = [Monster spriteNodeWithTexture:monsterTexture];

// Animation
if (monsterConfig[ANIMATION]) {
[monster runAction:monsterConfig[ANIMATION]];
}

// Moster Stats
monster.name = MONSTER_SPRITE;
monster.type = settings[TYPE];
monster.points = monsterConfig[POINTS] ? [monsterConfig[POINTS] intValue] : [monsterDefaults[POINTS] intValue];
monster.damage = monsterConfig[DAMAGE] ? [monsterConfig[DAMAGE] intValue] : [monsterDefaults[DAMAGE] intValue];
monster.hp = monsterConfig[HP] ? [monsterConfig[HP] intValue] : [monsterDefaults[HP] intValue];
monster.lethal = monsterConfig[LETHAL] ? [monsterConfig[LETHAL] boolValue] : [monsterDefaults[LETHAL] boolValue];

// Monster Physics
float physicsResize = monsterConfig[RESIZE] ? [monsterConfig[RESIZE] floatValue] : [monsterDefaults[RESIZE] floatValue];
switch ([monsterConfig[SHAPE] intValue]) {
case COMPLEX:
NSLog(@"%@", monster.texture);
NSLog(@"rotation: %f", monster.zRotation);
NSLog(@"x scale: %f", monster.xScale);
NSLog(@"y scale: %f", monster.yScale);
monster.physicsBody = [SKPhysicsBody bodyWithTexture:monster.texture size:monster.texture.size];
break;
case RECTANGLE:
monster.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(monster.size.width * physicsResize, monster.size.height * physicsResize)];
break;
default:
monster.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(monster.size.height * physicsResize) / 2];
break;
}

monster.physicsBody.dynamic = false;
monster.physicsBody.affectedByGravity = false;
monster.physicsBody.categoryBitMask = monsterCategory;
monster.physicsBody.contactTestBitMask = weaponCategory | heroCategory;
monster.physicsBody.collisionBitMask = defaultCategory;

// Monster Flight Pattern
SKAction * flightPattern = [self monsterFlightPattern:monster settings:settings];

// Monster Rotation
// Rotation disabled for physics text
// [self monsterRotation:monster rotationConfig:monsterConfig[ROTATION]];

// Move & Remove
SKAction * remove = [Config removeAction:monster];
[monster runAction:[SKAction sequence:@[flightPattern, remove]]];

return monster;
}

我在类加载时缓存纹理

@property (nonatomic) SKTexture * monsterBootTexture;
...

- (id)initWithFrameSize:(CGSize)frameSize
{
...
SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:monsterAtlas];
self.monsterBootTexture = [atlas textureNamed:MONSTER_BOOT];
...
}

NSLog是这样写的:

2015-01-02 12:03:20.619 Gadget Blaster[3301:665394] <SKTexture> 'boot.png' (97 x 100)
2015-01-02 12:03:20.623 Gadget Blaster[3301:665394] <SKTexture> 'boot.png' (97 x 100)

我根据 LearnCocos2D 的评论添加了以下日志:

2015-01-03 12:00:06.131 Gadget Blaster[3987:772046] rotation: 0.000000
2015-01-03 12:00:06.133 Gadget Blaster[3987:772046] x scale: 1.000000
2015-01-03 12:00:06.134 Gadget Blaster[3987:772046] y scale: 1.000000
2015-01-03 12:00:08.131 Gadget Blaster[3987:772046] rotation: 0.000000
2015-01-03 12:00:08.131 Gadget Blaster[3987:772046] x scale: 1.000000
2015-01-03 12:00:08.132 Gadget Blaster[3987:772046] y scale: 1.000000
2015-01-03 12:00:10.156 Gadget Blaster[3987:772046] rotation: 0.000000
2015-01-03 12:00:10.156 Gadget Blaster[3987:772046] x scale: 1.000000
2015-01-03 12:00:10.159 Gadget Blaster[3987:772046] y scale: 1.000000

此外,在使用复杂物理体时,我遇到了一些意想不到的碰撞问题。 SKPhysicsBody bodyWithCircleOfRadius 似乎表现得更好,我正在考虑让所有的怪物都成为圆形物理形状。

最佳答案

我认为这是 iOS8 上 Spritekit 的一个错误。我怀疑当物理体的纹理从内部缓存中检索时会出现错误。它可能会错误地翻转图像以尝试更正来自 CoreGraphics 的坐标系。

改用 bodyWithBodies。糟糕,我真的很喜欢这个功能。

关于ios - SpriteKit SKPhysicsBody bodyWithTexture 颠倒了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27748034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com