Alexyuiop
Alexyuiop

Reputation: 823

How to check if something is moving?

I am making a cocos2d app that has enemies shooting things. A a lot of times, when a sprite that is programmed to move to a random place once its team mate is killed goes straight into the crossfire of other enemies of his own kind. How do I make something to check if it is moving or not because most of the time, the game just looks like enemies trying to kill themselves. I want the enemies to be able to themselves, but not when they are moving.

-(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]];



}

}

Projectile:

-(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);

}

Collision(in the enemy2 section):

-(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];


        }

    }

Upvotes: 1

Views: 234

Answers (1)

tallen11
tallen11

Reputation: 1377

I would create a BOOL that stores whether or not the character is moving. Whenever the character is about to move, set that BOOL to YES. Since you are using actions to move the sprite, you could use a sequence of actions with the first action being the move action and then the second one being a block that would set your BOOL to NO. That way, as soon as the move action was finished, it would be stored for later reference. It would look something like this.

BOOL isObjectMoving; Put this somewhere else, possibly in your entity subclass or whatever else

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]];

Upvotes: 1

Related Questions