如何在cocos2d中使用粒子效果跟踪我的精灵?

时间:2012-07-06 15:10:29

标签: iphone ios5 cocos2d-iphone sprite

我想要一个雪粒子效果跟随我的精灵,我尝试了一些方法,但所有最终发生的事情是雪将保持静止而不是跟随。我做了这个教程(将在我找到它后立即发布),这表明它是如何用火做的,但根本没有用。任何教程或建议将不胜感激。我相信我必须在片段部分添加某种代码,然后在屏幕上创建敌人。

   [self schedule:@selector(gameLogicboss:) interval:180 ];        
      [self schedule:@selector(updateboss:)];                

 -(void)addTarget1 {

     Boss *target1 = nil;    


     if ((arc4random() % 2) == 0) {{
      target1 = [WeakAndFastBoss boss];
   }}  else {
     target1 = [WeakAndFastBoss boss];
      }                      

       // Determine where to spawn the target along the Y axis
      CGSize winSize = [[CCDirector sharedDirector] winSize];
     int minY = target1.contentSize.height/2;
    int maxY = winSize.height - target1.contentSize.height/2;
    int rangeY = maxY - minY;
    int actualY = (arc4random() % rangeY) + minY;

  // Create the target slightly off-screen along the right edge,
      // and along a random position along the Y axis as calculated above
    target1.position = ccp(winSize.width + (target1.contentSize.width/2), actualY);
     [self addChild:target1 ];

       // Determine speed of the target

       int minDuration = target1.minMoveDuration;
       int maxDuration = target1.maxMoveDuration;
         int rangeDuration = maxDuration - minDuration;
          int actualDuration = (arc4random() % rangeDuration) + minDuration;

           // Create the actions
                                                                                                 id actionMove = [CCMoveTo actionWithDuration:actualDuration      position:ccp(-target1.contentSize.width/2, actualY)];


  id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                     selector:@selector(spriteMoveFinished:)];
 [target1 runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
  target1.tag = 1;
     [_targets addObject:target1];   
       }

 -(void)gameLogicboss:(ccTime)dt {
 [self addTarget1];
   iterations_++;
       }

                - (void)updateboss:(ccTime)dt {
        CGRect projectileRect = CGRectMake(projectile.position.x -                  (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2),                           projectile.contentSize.width,                                   projectile.contentSize.height);

BOOL bossHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target1 in _targets) {
    CGRect target1Rect = CGRectMake(target1.position.x - (target1.contentSize.width/2),                                        target1.position.y - (target1.contentSize.height/2),                                        target1.contentSize.width,                                  target1.contentSize.height);

    if (CGRectIntersectsRect(projectileRect, target1Rect)) {

        //[targetsToDelete addObject:target];   
        bossHit = TRUE;
        Boss *boss = (Boss *)target1;
        boss.hp--;
        if (boss.hp <= 0) {
            _score ++;
            [targetsToDelete addObject:target1];
        }
        break;

    }                       
}

for (CCSprite *target in targetsToDelete) {
    [_targets removeObject:target];
    [self removeChild:target cleanup:YES];                                  
    _projectilesDestroyed++;
    if (_projectilesDestroyed > 2) {

          } 
         }

if (bossHit) {
    //[projectilesToDelete addObject:projectile];
    [[SimpleAudioEngine sharedEngine] playEffect:@"explosion.caf"];
}
[targetsToDelete release];
 }

     -(void)spriteMoveFinishedboss:(id)sender {
       CCSprite *sprite = (CCSprite *)sender;
      [self removeChild:sprite cleanup:YES];
       GameOverScene *gameOverScene = [GameOverScene node];
         [gameOverScene.layer.label setString:@"You Lose"];
       [[CCDirector sharedDirector] replaceScene:gameOverScene];    

      if (sprite.tag == 1) { // target
[_targets removeObject:sprite];
      } else if (sprite.tag == 2) { // projectile
[_projectiles removeObject:sprite];
     }
     }

5 个答案:

答案 0 :(得分:1)

您无需使用精灵更新粒子发射器的位置。 您可以将子系统作为子项添加到精灵中。 粒子系统确实需要输入:

 CCParticleSystem * booster = [CCParticleSystem particleWithFile:@"boosterParticles.plist"];
    //customize your particles' options

    //assuming you have a sprite defined as _motherShip
    [_motherShip addChild:booster];

    /*
     * now that the particles are the _motherShip's child, you must remember 
     * to set the position relative to the mothership's origin...
     */
    particles.position = ccp(15,0);

...所以现在每当_motherShip.position发生变化时,助推器都会随之而来。它甚至会随船一起旋转。

答案 1 :(得分:0)

非常简单的逻辑而无需进入代码:

我生成一个精灵并给它一个位置(x,y)。 对于每个精灵,我还会产生一个CCParticleSystem,给它所需的粒子类型,产生率等。 CCParticleSystem位置现在设置为与精灵相同的(x,y)位置,并且应该在精灵的(x,y)位置更新时更新。

当精灵和CCParticleSystem四处移动时,这个位置(x,y)会按照间隔步骤时间在您的计划方法中随机获取更新。

希望这是有道理的。

答案 2 :(得分:0)

我这样做

vehicleParticleSystem = [CCParticleSystemQuad particleWithFile:@"vehicleParticle.plist"];
vehicleParticleSystem.position = ccp(_ship.position.x - _ship.contentSize.width/3, _ship.position.y - _ship.contentSize.height/3);
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
    vehicleParticleSystem.scale = 0.5;
}
[self addChild:vehicleParticleSystem z:-1];

并使用此

更新其位置
- (void) updateParticleSystem:(ccTime)dt {
vehicleParticleSystem.position = ccp(_ship.position.x - _ship.contentSize.width/3, _ship.position.y - _ship.contentSize.height/3);
}

-(void) update:(ccTime)dt方法中调用。

用户通过游戏手柄移动船只。

希望这会有所帮助。对于发动机效应,颗粒的定位稍微落后于车辆。

答案 3 :(得分:0)

我在CCParticleSystem.h找到了这个。

/** @typedef tCCPositionType
 possible types of particle positions
 /
typedef enum {
    /* Living particles are attached to the world and are unaffected by emitter repositioning. */
    kCCPositionTypeFree,

/** Living particles are attached to the world but will follow the emitter repositioning.
 Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.
 */
kCCPositionTypeRelative,

/** Living particles are attached to the emitter and are translated along with it. */
kCCPositionTypeGrouped,

you should set it like

myparticleSystem.positionType=kCCPositionTypeGrouped;

希望它有所帮助。

答案 4 :(得分:0)

您是否尝试使用更新方法更新粒子位置?

我在我的一个游戏中做了类似以下几行的事情,它的工作原理我认为你期待

    -(void) update:(CCTime)delta{

        _sprite.position = CGPointMake(newXPosition, newYPosition);

        _fire.position = CGPointMake(_sprite.position.x, _sprite.position.y);

    }

我希望它有所帮助!