精灵套装球弹跳物理目标c

时间:2015-12-21 16:33:12

标签: ios objective-c game-physics skphysicsbody

我想将重力加到球上,所以它会上下弹跳,并且稍微向右或向左反弹,角球反弹,我不想失去球的能量,所以它总是在反弹同样的高度。现在球在各个方向都反弹,并在几分钟后停止,即使我将摩擦设置为0.这里有一点我的代码:

    ball.position = CGPointMake(self.frame.size.width/4 +arc4random() % ((int)self.frame.size.width/2),
                                self.frame.size.height/2 +arc4random() % ((int)self.frame.size.height/2));
    [self addChild:ball];

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/3];
    ball.physicsBody.friction = 0.0f;
    ball.physicsBody.restitution = 1.0f;
    ball.physicsBody.linearDamping = 0.0f;
    ball.physicsBody.allowsRotation = YES;
    [ball.physicsBody applyImpulse:CGVectorMake(50.0f, -50.0f)]; 

1 个答案:

答案 0 :(得分:0)

如果你不希望球向各个方向反弹,那么你应该直接击球。

如果你想在游戏中保持恒定的速度,那么你需要确保你保持速度:

  if(myNode.physicsBody.velocity.dx < 30)
myNode.physicsBody.velocity = CGVectorMake(30, myNode.physicsBody.velocity.dy);

当节点速度小于30

时,这将应用速度30

您可以使用此方法:

-(void)addBallOfType:(NSString *)imageName ofSize:(CGSize) size addSpriteName:(NSString *)name atPoint:(CGPoint)point withBounce:(BOOL) bounce withVelocity:(CGVector)velocity andHitCategory:(int) category addEmitter:(BOOL) addEmitter{


    SKSpriteNode * ball = [SKSpriteNode spriteNodeWithImageNamed:imageName];
    ball.name=name;
    ball.size=size;
    ball.anchorPoint=CGPointMake(0, 0);

    ball.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
    ball.physicsBody.categoryBitMask=category;
    ball.physicsBody.collisionBitMask=youCollisionBitMask;
    ball.physicsBody.contactTestBitMask= youCollisionBitMask;

    ball.physicsBody.dynamic=YES;
    ball.physicsBody.affectedByGravity=NO;
    ball.physicsBody.restitution=1.0;
    ball.physicsBody.friction=0.0;
    ball.physicsBody.linearDamping=0.0;
    ball.physicsBody.angularDamping=0.0;

    ball.position=point;
    [self addChild:ball];

     if (addEmitter) {
       _emitterNodePath =
        [[NSBundle mainBundle] pathForResource:@"RParticle" ofType:@"sks"];
       SKEmitterNode *burstEmitter =
    [NSKeyedUnarchiver unarchiveObjectWithFile:_emitterNodePath];
    [ball addChild:burstEmitter];
    }



    if (bounce) {
        CGVector impulse = velocity;
        [ball.physicsBody applyImpulse:impulse];

    }


}

向不同方向打击:

-(CGVector)hitToLeftWithSpeed:(float)speed{

    return CGVectorMake(-speed, 0);
}

-(CGVector)hitToRightWithSpeed:(float)speed{

    return CGVectorMake(speed, 0);
}

-(CGVector)hitTotopWithSpeed:(float)speed{

    return CGVectorMake(0, speed);
}

-(CGVector)hitToBottomWithSpeed:(float)speed{

    return CGVectorMake(0, -speed);
}

-(CGVector)forwardCrossHitWithSpeed:(float)speed{

    return CGVectorMake(speed, speed);
}

-(CGVector)backwardCrossHitWithSpeed:(float)speed{

    return CGVectorMake(-speed, -speed);
}

定位球:

  -(CGPoint)addBallAtCenterWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 100+y);
}
-(CGPoint)addBallAtTopLeftWithOffsetX:(int) x withOffsetY:(int)y{

    return CGPointMake(200+x, 100+y);
}

-(CGPoint)addBallAtTopRightWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 100+y);
}

-(CGPoint)addBallAtBottomRightWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 2100+y);
}

-(CGPoint)addBallAtBottomLeftWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 200+y);
}

你可以通过以下方式调用它:

 [self addBallOfType:hBlueBall ofSize:CGSizeMake(75, 75) addSpriteName:@"greenball" atPoint:CGPointMake(100, 100) withBounce:YES withVelocity:[self hitTotopWithSpeed:50] andHitCategory:blueBallHitCategory addEmitter:YES];