如何检查是否有东西在移动?

时间:2012-11-17 02:01:39

标签: objective-c cocoa-touch cocos2d-iphone

我正在制作一个有敌人拍摄东西的cocos2d应用程序。很多时候,当一个精灵被编程为在其队友被杀后被移动到一个随机位置的精灵会直接进入他自己类型的其他敌人的交火中。我怎样做一些东西来检查它是否在移动,因为大多数时候,游戏看起来像是试图自杀的敌人。我希望敌人能够自己,但不是在他们移动的时候。

-(void)enemy2{

    enemy2 = [CCSprite spriteWithFile:@"enemy2.png"];
    int d = arc4random()%480+480;
    int o = arc4random()%320+320;
    x = arc4random()%480;
    if( x <= 480 && x>= 460){
    x=x-100;
}
if(x <= 100){
    x = x+50;
}

y = arc4random()%320;
if(y <=320 && y >= 290){
    y = y-100;
}
if(y < 100){
    y = y + 100;
}
enemy2.position = ccp(o,d);

xc = x;
te = y;
   [enemytwo addObject :enemy2];




[self addChild:enemy2];

for(CCSprite *enetw in enemytwo){

    CCRotateBy *rotation = [CCRotateBy actionWithDuration:15 angle:1080];
    CCRepeatForever * repeatforever = [CCRepeatForever actionWithAction:rotation];

    int f = arc4random()%480;
    int s = arc4random()%320;      
    if( f <= 480 && s>= 460){
        f=f-100;
    }
    if(f <= 100){
        f = f+50;
    }

    s = arc4random()%320;
    if(s <=320 && s >= 290){
        s = s-100;
    }
    if(s < 100){
        s = s + 100;
    }

    CCMoveTo *move = [CCMoveTo actionWithDuration:0.8 position:ccp(f,s)];
    [enetw runAction: repeatforever];
    [enetw runAction:[CCSequence actions:move, nil]];



}

}

弹丸:

-(void)projectileShooting:(ccTime)dt {
 [self schedule:@selector(projectileShooting:) interval:2.5];
projcount++;
if([proj count] <= 15 )
if(enemy1.position.y < 320){
v = ccp(player.position.x,player.position.y); 
for(CCSprite *enemies in enemy){ 
    CCSprite * projectilebullet = [CCSprite spriteWithFile:@"Projectile.png"];
    [proj addObject:projectilebullet];
      [self addChild:projectilebullet];
        CGPoint MyVector = ccpSub(enemies.position,player.position );
        MyVector = ccpNormalize(MyVector);
        MyVector = ccpMult(MyVector, enemies.contentSize.width/2);
        MyVector = ccpMult(MyVector,-1);
        projectilebullet.position = ccpAdd(enemies.position, MyVector);





}

}

    for(CCSprite *enem2 in enemytwo){
      if(  [proj count] <= 15){
            CCSprite * projectilebull = [CCSprite spriteWithFile:@"Projectile.png"];

            CGPoint MyVector = ccpSub(enem2.position,player.position );
            MyVector = ccpNormalize(MyVector);
            MyVector = ccpMult(MyVector, enem2.contentSize.width/2+10);
            MyVector = ccpMult(MyVector,-1);
            projectilebull.position = ccpAdd(enem2.position, MyVector);

            [self addChild:projectilebull];  
          [proj addObject:projectilebull];
        for(CCSprite *projectile in proj){


        }

       }

    }



    }







-(void)bulletMovement:(CCSprite *)bullet{

CGPoint location = ccpSub(bullet.position, player.position);
location = ccpNormalize(location);
location = ccpMult(location, -1);
bullet.position = ccpAdd(bullet.position, location);

}

碰撞(在敌人2部分):

-(void)update{

    deletehealth = [[NSMutableArray alloc] init];
enemytwodelete  = [[NSMutableArray alloc]init];
enemythreedelete2  = [[NSMutableArray alloc]init];

NSMutableArray *playerdelete = [[NSMutableArray alloc] init];


NSMutableArray * deleteclean = [[NSMutableArray alloc]init ];
projectilesToDelete = [[NSMutableArray alloc] init];
enemydelete  = [[NSMutableArray alloc]init];
     for(CCSprite *projectile in proj)
{

    CGRect project = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                projectile.position.y - (projectile.contentSize.height/2), 
                                projectile.contentSize.width, 
                                projectile.contentSize.height);


    CGRect playerRect = CGRectMake(player.position.x - (player.contentSize.width/2), 
                                   player.position.y - (player.contentSize.height/2), 
                                   player.contentSize.width, 
                                   player.contentSize.height);



    if (CGRectIntersectsRect(playerRect, project)) {
        [projectilesToDelete addObject: projectile];
        emitter = [[CCParticleExplosion alloc] init];
        emitter.texture = [[CCTextureCache sharedTextureCache]addImage:@"particle.png"];
        emitter.position = ccp(projectile.position.x,projectile
                               .position.y);
        [self addChild:emitter];
        // NSLog(@"%i",life);
        if(life > 0){
            //lifeholder = [[NSMutableArray alloc] init];
            life -= 5;

            [self.progressTimer setPercentage:life];
        }else{
            [playerdelete addObject:player];
            emitter.position = ccp(projectile.position.x,projectile
                                   .position.y);

            [[CCDirector sharedDirector]replaceScene:[CCTransitionFade transitionWithDuration:3 scene:[end node]]];
        }


    }

    for(CCSprite*enemies in enemy){
        CGRect enemyRect = CGRectMake((enemies.position.x-5), 
                                      enemies.position.y-8.3, 
                                      7, 
                                      15);


            if(CGRectIntersectsRect(enemyRect, project)){
                enemyex = [[CCParticleExplosion alloc] init];
               enemyex.texture = [[CCTextureCache sharedTextureCache]addImage:@"particle.png"];
               enemyex.position = ccp(enemies.position.x, enemies.position.y);
               [self addChild:enemyex];
                [enemydelete addObject:enemies];


                    [projectilesToDelete addObject:projectile];

                [self powerups];
                [self clear];


            }
    }   


    for(CCSprite * enemies2 in enemytwo){

    CGRect enemy2Rect = CGRectMake(enemies2 .position.x - (enemies2 .contentSize.width/2),
                                   enemies2 .position.y - (enemies2 .contentSize.height/2),
                                   enemies2 .contentSize.width, 
                                   enemies2.contentSize.height);

        if(CGRectIntersectsRect(enemy2Rect, project)){
           explode = [[CCParticleExplosion alloc] init];
            explode .texture = [[CCTextureCache sharedTextureCache]addImage:@"particle.png"];
            explode .position = ccp(enemies2.position.x, enemies2.position.y);
            [self addChild:explode ];

            [enemytwodelete addObject:enemies2];
            [projectilesToDelete addObject:projectile];
            [self powerups];
            [self clear];


        }

    }

1 个答案:

答案 0 :(得分:1)

我会创建一个BOOL来存储角色是否在移动。每当角色即将移动时,将该BOOL设置为YES。由于您使用动作移动精灵,您可以使用一系列动作,第一个动作是移动动作,然后第二个动作是将BOOL设置为NO的块。这样,一旦移动动作完成,它将被存储以供以后参考。它看起来像这样。

BOOL isObjectMoving;把它放在其他地方,可能是你的实体子类或其他什么

CCMoveTo *move = [CCMoveTo actionWithDuration:0.8 position:ccp(f,s)];
CCCallBlock *end = [CCCallBlock actionWithBlock:^{
     isObjectMoving = NO;
}];

isObjectMoving = YES; //Set it to YES until the movement action finishes and sets it back to NO

[yourObject runAction:[CCSequence actionOne:move actionTwo:end]];