SpriteKit目标跳转

时间:2014-06-05 09:55:52

标签: ios sprite-kit uiswipegesturerecognizer

我的代码出了问题。我添加了滑动手势来跳跃,但我的目标根本不会跳跃。滑动手势的NSLog有效,因此我的客观代码一定有问题。

-(void)Mensch{

SKTexture * MenschTexture1 = [SKTexture textureWithImageNamed:@"Mensch1"];
MenschTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture * MenschTexture2 = [SKTexture textureWithImageNamed:@"Mensch2"];
MenschTexture2.filteringMode = SKTextureFilteringNearest;

SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschTexture1, MenschTexture2] timePerFrame:0.4]];

Mensch = [SKSpriteNode spriteNodeWithTexture:MenschTexture1];
Mensch.size = CGSizeMake(45, 45);
Mensch.position = CGPointMake(self.frame.size.width / 5, Boden.position.y + 73);
Mensch.zPosition = 2;

Mensch.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Mensch.size];
Mensch.physicsBody.dynamic = NO;
Mensch.physicsBody.allowsRotation = NO;
Mensch.physicsBody.usesPreciseCollisionDetection = YES;
Mensch.physicsBody.restitution = 0;

[Mensch runAction:Run];
[self addChild:Mensch];

}


- (void)didMoveToView:(SKView *)view
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
recognizer.direction = UISwipeGestureRecognizerDirectionUp;
[[self view] addGestureRecognizer:recognizer];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{ 
Mensch.physicsBody.velocity = CGVectorMake(0, 0);
[Mensch.physicsBody applyImpulse:CGVectorMake(0, 20)];

NSLog(@"Jump");

}

1 个答案:

答案 0 :(得分:0)

您需要设置

Mensch.physicsBody.dynamic = YES;

根据Apple的documentation关于dynamic的{​​{1}}属性:

  

默认值为YES。如果值为NO,那么物理体   忽略适用于它的所有力量和冲动。这个属性是   在基于边缘的身体上被忽略;它们是自动静止的。