applyImpulse不触发

时间:2014-05-05 13:29:14

标签: ios objective-c sprite-kit

我已在initWithSize方法中将重力定义为0,然后在touchesBegan中将其设置为-5,因此当您触摸屏幕时应用程序将启动问题是touchesBegan方法我也实现了applyImpulse,但它没有触发。只有重力被触发。这意味着移动器只是落在按钮上。

为什么不触发applyImpulse?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 );
    mover.physicsBody.restitution = 0.0;
    mover.physicsBody.velocity = CGVectorMake(0, 0);
    [mover.physicsBody applyImpulse:CGVectorMake(0, 15)];

}

和移动方法:

-(void)addMover:(CGSize) size {


    SKTexture* lolTexture1 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol1"]];
    lolTexture1.filteringMode = SKTextureFilteringNearest;

    SKTexture* lolTexture2 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol2"]];
    lolTexture2.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture3 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol3"]];
    lolTexture3.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture4 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol4"]];
    lolTexture4.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture5 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol5"]];
    lolTexture5.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture6 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol6"]];
    lolTexture6.filteringMode = SKTextureFilteringNearest;
    SKAction *flap = [SKAction repeatActionForever:[SKAction animateWithTextures:@[lolTexture1, lolTexture2, lolTexture3, lolTexture4, lolTexture5, lolTexture6] timePerFrame:0.15]];

    SKSpriteNode *mover = [SKSpriteNode spriteNodeWithTexture:lolTexture1];
    [mover setScale:1.0];

    mover.position = CGPointMake(self.frame.size.width / 4, CGRectGetMidY(self.frame));

    [mover runAction:flap withKey:@"birdGo"];

    [self addChild:mover];

    self.physicsWorld.gravity = CGVectorMake( 0.0, 0 );

    mover.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:mover.frame.size.width/2-7];

}

1 个答案:

答案 0 :(得分:0)

在addMover之外声明移动设备,以便您可以在touchesBegan方法中使用它。

SKSpriteNode *搬运工;

然后在addMover

mover = [SKSpriteNode spriteNodeWithTexture:lolTexture1];