Gad*_*ter 7 objective-c game-physics ios sprite-kit skphysicsbody
嗨,我正在尝试修复这个错误,spritekit的物理形状出现颠倒.
[SKPhysicsBody bodyWithTexture:monsterTexture size:monsterTexture.size]
Run Code Online (Sandbox Code Playgroud)
怪物第一次出现时,身体方向是正确的.但是第二次和每次之后怪物出现它的物理体沿着Y轴反转.请参见图片,skView.showsPhysics = true;以便显示物理形状.它第一次正常工作的事实让我觉得可能有一些我不知道的属性正在修改或者其他什么.
我想把2个物理块放在靴子的粗糙形状中,但这并不理想,因为还有一些其他更复杂的形状,我想使用bodyWithTexture来体验相同的bug.
我也试过bodyWithTexture:monsterTexture,原来的SKTexture对象代替bodyWithTexture:monster.texture了Monster对象的纹理.

这是我的添加怪物代码.如果您需要更多,请告诉我.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我在加载类时缓存纹理
@property (nonatomic) SKTexture * monsterBootTexture;
...
- (id)initWithFrameSize:(CGSize)frameSize
{
...
SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:monsterAtlas];
self.monsterBootTexture = [atlas textureNamed:MONSTER_BOOT];
...
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我根据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
Run Code Online (Sandbox Code Playgroud)
另外,在使用复杂的物理实体时,我遇到了一些意外的碰撞问题.SKPhysicsBody bodyWithCircleOfRadius似乎表现得更好,我正在考虑让所有怪物圈出物理形状.
解决方案是对图集而不是纹理本身进行强有力的引用。[SKTextureAtlas preloadTextureAtlases:textureAtlases withCompletionHandler:^{ ... }];这也简化了我的代码,因为我已经在场景开始时预加载了所有地图集。这似乎使用相同数量的内存(如果不是更少的话),并且它不再产生颠倒的物理体错误。关于这个问题的评论(我应该在精灵套件的属性中缓存纹理吗?)帮助我在重构代码时发现了这一点。
// In Game Scene
@property (nonatomic, strong) SKTextureAtlas * monsterAtlas;
...
- (id)initWithSize:(CGSize)size
{
self.monsterAtlas = [SKTextureAtlas atlasNamed:monsterAtlasName];
NSArray * textureAtlases = @[self.monsterAtlas];
[SKTextureAtlas preloadTextureAtlases:textureAtlases withCompletionHandler:^{ ... }];
}
// In Monster Class
- (Monster *)monster:(NSDictionary *)settings
{
...
SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:monsterAtlasName];
NSString * textureName = monsterConfig[TEXTURE] ? monsterConfig[TEXTURE] : monsterDefaults[TEXTURE];
Monster * monster = [Monster spriteNodeWithTexture:[atlas textureNamed:textureName]];
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1329 次 |
| 最近记录: |