spriteKit中常规冲突的bug

时间:2014-06-02 15:26:26

标签: ios objective-c xcode sprite-kit

我做了一个非常简单的跳跃游戏。当我的“英雄”与地面相撞时,它会调用didBeginContact并跳跃。非常基本,但它不能很好地工作,因为过了一段时间(有时更早,有时候更晚......)它会停止工作,你可以看到。我想知道我是做错了什么,或者是否有更好的方法,或者如果spriteKit Id中有一个已知的错误,从未听说过...... 图像是一个gif。如果看不到动作,请在新标签中打开它 jumping secuence

@interface XYZWorld1()
@property (nonatomic)  SKSpriteNode *hero;
@property (nonatomic) SKSpriteNode *groundPhysics;
@end


@implementation XYZWorld1
static const uint32_t ground = 0x2 << 2;
static const uint32_t bee = 0x3 << 3;

创建基础的方法:

    -(SKSpriteNode*)createGroundxPos:(float)x yPos:(float)y width:(CGFloat)width height:(CGFloat)height
{
    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:1 saturation:1 brightness:1 alpha:1] size:CGSizeMake(width, height)];
    SKPhysicsBody *groundFisica =[SKPhysicsBody bodyWithRectangleOfSize:ground.size];
    [ground setName:@"ground"];
    [groundFisica setDynamic:NO];
    [groundFisica setAffectedByGravity:NO];
    [groundFisica setAllowsRotation:NO];
    [groundFisica setRestitution:0];
    [groundFisica setUsesPreciseCollisionDetection:YES];
    [groundFisica setCategoryBitMask:ground];
    [groundFisica setContactTestBitMask:bee];
    [groundFisica setCollisionBitMask:bee];
    ground.physicsBody = groundFisica;
    [ground setPosition:CGPointMake(x, y)];
    return ground;

}

创建英雄的方法

-(SKSpriteNode*)crearHero
{
    SKTexture *temp = _heroWalkingFrames[0];
    SKSpriteNode *hero = [SKSpriteNode spriteNodeWithTexture:temp];

    hero.name = @"hero";
    hero.zPosition = 1;
    if(IS_IPHONE_5){
        [hero setPosition:CGPointMake(100, 280)];
    }
    else {
        [hero setPosition:CGPointMake(100, 220)];
    }

    [hero setSize:CGSizeMake(25, 25)];
    hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(23, 24)];

    [hero.physicsBody setDensity:0.95];
    [hero.physicsBody setDynamic:YES];
    [hero.physicsBody setAffectedByGravity:YES];
    [hero.physicsBody setAllowsRotation:NO];
    [hero.physicsBody setUsesPreciseCollisionDetection:YES];
    [hero.physicsBody setRestitution:0];
    [hero.physicsBody setVelocity:CGVectorMake(0, 0)];
    return hero;
}

在游戏开始前预加载所有内容的方法:

-(void)preloadAssets
{
    //hero
    _heroWalkingFrames = [self cargarArrayAtlas:@"astroJump.atlas" imageName:@"astronautaJump_000"];
    _hero = [self crearHero];

    _hero.physicsBody.categoryBitMask = bee;
    _hero.physicsBody.collisionBitMask = bee | cubico | ground | bicho ;
    _hero.physicsBody.contactTestBitMask = bee | cubico | ground | bicho  ;


//Ground
_groundPhysics = [self createGroundxPos:100 yPos:142 width:42 height:23.25];
}

当两个物体首次相互接触时调用的方法:

-(void) didBeginContact:(SKPhysicsContact *)contact 
{
    //if hero collides with ground

    if((contact.bodyA.categoryBitMask == bee && contact.bodyB.categoryBitMask == ground) ||
       (contact.bodyA.categoryBitMask == ground && contact.bodyB.categoryBitMask == bee) )
    {

        if(_jumping == NO && _groundPhysics.position.y+_groundPhysics.size.height/2<=_hero.position.y-11.5)
        {

            [self jumping];
        }
    }
}

调用方法进行跳转:

-(void)jumping{
    //Play sound file
    [self runAction:self.jumpSound withKey:@"jumpSound"];


    if(IS_IPHONE_5){
        _diferencia=(164 * 9)/_hero.position.y;

    }else{
        _diferencia=(164 * 7)/_hero.position.y;
    }


    [_hero.physicsBody applyImpulse:CGVectorMake(0,_diferencia)];
    _jumping = YES;
    [_hero removeActionForKey:@"astroJump"];
    [self astroJump];
}

-(void)astroJump
{
    //runAction animation method
    [_hero runAction:[SKAction animateWithTextures:_heroWalkingFrames
                                       timePerFrame:0.05f
                                             resize:NO
                                            restore:NO] withKey:@"astroJump"];
    return;
}

updateinitGameContent中来自if句子的布尔(self.jumping):

-(void)update:(CFTimeInterval)currentTime
{
if(_hero.physicsBody.velocity.dy < 0 )_jumping = NO;
}

-(void)initGameContent
{

    _jumping = NO;

2 个答案:

答案 0 :(得分:1)

您可能希望确保在检测到与地面接触时将玩家位置重置为高于地面。否则,当[self jumping];被调用时,玩家仍然可以与地面接触,这有时会导致玩家被卡住#34;在进行碰撞检测时,我在游戏中看到了类似的问题。下面的代码显示了解决此问题的方法。

if (_jumping == NO && _groundPhysics.position.y+_groundPhysics.size.height/2<=_hero.position.y-11.5)
{
    _hero.position.y = _groundPhysics.position.y + _groundPhysics.size.height/2 + 1;
    [self jumping];
}

答案 1 :(得分:0)

我终于找到了解决问题的方法。我已将physicbody.size.y的大小更改为与他的spriteNode.y相同,并且错误已经消失。