2

我想要雪粒子效果跟随我的精灵,我尝试了一些方法,但最终发生的只是雪将保持静止而不是跟随。我做了这个教程(一找到它就会发布),它展示了它是如何用火来做的,但根本没有成功。任何教程或建议将不胜感激。我相信我必须在片段部分添加某种代码,其中显示在屏幕外创建敌人。

   [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];
     }
     }
4

5 回答 5

1

I Have found this, at 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;

Hope it helps.

于 2013-06-13T05:24:26.563 回答
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 发生变化时,助推器就会随之而来。它甚至会随船旋转。

于 2014-06-22T05:09:07.430 回答
0

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

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

随着精灵和 CCParticleSystem 的移动,这个位置 (x, y) 会在您的调度方法中按照间隔步骤时间不断地随机更新。

希望这是有道理的。

于 2012-07-10T10:33:15.847 回答
0

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

我在我的一个游戏中做了类似以下几行的事情,它的工作方式和我想的一样

    -(无效)更新:(CCTime)增量{

        _sprite.position = CGPointMake(newXPosition, newYPosition);

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

    }

我希望它有帮助!

于 2014-05-27T23:49:59.657 回答
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方法中调用。

船由用户通过手柄移动。

希望这可以帮助。粒子的位置稍微在车辆后面以获得引擎效果。

于 2012-07-10T11:46:39.823 回答