如何停止无休止地玩SpriteKit发射器?

时间:2014-06-07 19:21:53

标签: sprite-kit skemitternode

我试图阻止发射器在我的Sprite Kit项目中无休止地玩。 “生命周期”设置设置为Start=1End=0,并显示我想要的正确结果(仅发出一次)。

当我在更新方法中调用它时会出现问题,它会无休止地发出,我尝试使用removeFromParentremoveAllAction,但没有运气。你能帮我解决这个问题吗?

-(void)update:(NSTimeInterval)currentTime {

    [self updateheartPositionFromMotionManager];
    [self matchIt];

    if ( _heart.position.x == 512 && _heart.position.y == 484 ){
        SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:
                  [[NSBundle mainBundle] pathForResource:@"sparkly" ofType:@"sks"]];

        emitter.position = CGPointMake(self.frame.size.width/2,               
                                       CGRectGetMidY(self.frame)+100);;
        emitter.name = @"exhaust";
        emitter.targetNode =_heart;
        [self addChild:emitter];

    }else{

        [self removeFromParent];
    }
}

1 个答案:

答案 0 :(得分:0)

//步骤1采用全局变量    @property BOOL sparklyAdded;在.h文件中

.m文件的init或instance方法

sparklyAdded = FALSE;

- (无效)particleAdded   {

}

- (void)update:(NSTimeInterval)currentTime {

  [self updateheartPositionFromMotionManager];
  [self matchIt];

 if (( _heart.position.x>= 512 && _heart.position.y=< 484 ) && !sparklyAdded){
     SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:
              [[NSBundle mainBundle] pathForResource:@"sparkly" ofType:@"sks"]];

     emitter.position = CGPointMake(self.frame.size.width/2,               
                                   CGRectGetMidY(self.frame)+100);;
     emitter.name = @"exhaust";
     emitter.targetNode =_heart;
     [self addChild:emitter];
     sparklyAdded=TRUE;

  }else{

    [self removeEffect];
 }

}

-(void) removeEffect

{       [self enumerateChildNodesWithName:@“exhaust”usingBlock:^(SKNode * node,BOOL * stop){

    [node removeAllActions];
    [node removeFromParent];
     sparklyAdded=FALSE;
}];

}

//在添加粒子时记住几条大腿 1)它们是昂贵的cpu调用,因此只需从更新循环中添加和删除它们