开发者

Need help with NSMutableArray solution

Im currently devising a solution for my object BuildingNode *tower which is held inside NSMutableArray *gameObjects, to attack EnemyNo开发者_JAVA百科de *enemy objects also held inside the gameObjects array.

I have a proposed solution which occasionally causes my game to freeze (temporarily) as the solution employed is quite buggy.

My solution is that each tower object contains its own NSMutableArray *targets which is synthesized from the Tower Class. If an enemy comes into range or out of range of any given tower object, the corrosponding index of the EnemyNode *enemy object from the gameObjects array is saved as an NSNumber into the targets array, or alternatively if the enemy object is out of range, it is removed from the .targets array.

Basically the idea is that the targets array holds the indices of any enemy that is in scope of the tower.

The problem I seem to be facing is that because this tower.targets array is updated dynamically all the time, i believe that if i'm doing some sort of operation on a particular index of tower.targets that then is removed, i get this error:

-[BuildingNode distance]: unrecognized selector sent to instance 0x2dd140

Each BuildingNode *tower has different attacking alogrithms that use the tower.targets array for calling back the sorted/desired enemy. For example, a random attack style will randomize a number between 0 & [tower.targets count] then I can create a pointer to gameObjects with the corresponding [tower.targets intValue]. Something like this:

EnemyNode *enemy = (EnemyNode *)[gameObjects objectAtIndex:[[tower.targets objectAtIndex:rand]intValue]];

So this will find a random enemy from the potential .targets array and then create a pointer object to an enemy.

I've put in many if statements to ensure that in the case of a .targets index being removed mid-sorting, the operation shouldnt go ahead, thus removing the game failure rate, but it still occurs occassionally.

Heres my code:

Please note that BuildingNode *tower == BuildingNode *build.

This is just a snippet of the iteration inside gameObjects.

 //Potential Enemies (Indices) Add and Delete/
 if (enemy.distance < build.atk_distance && !enemy.isExploding){
       NSNumber *index = [NSNumber numberWithInt:[array indexOfObject:enemy]];
        if(![build.targets containsObject:index]){
            [build.targets addObject:index];
            }
        }
        else {
            NSNumber *index = [NSNumber numberWithInt:[array indexOfObject:enemy]];
            if ([build.targets containsObject:index]){
                [build.targets removeObject:index];
            }
        }
  }

//Aiming// Nearest Algorithm. //Will find the nearest enemy to the building

 if (enemy.distance < build.atk_distance){
     if (!build.isAttacking || build.atk_id == 0){
           if ([build.targets count]){
                if ([build.atk_style isEqualToString:@"near"]){
                    int l_dist;
                    for (int i = 0; i < [build.targets count]; i++){
                        //Grab the Enemy from Targets
                        if ([build.targets objectAtIndex:i]){
                            if([array objectAtIndex:[[build.targets objectAtIndex:i]intValue]]){
                                EnemyNode *temp = [array objectAtIndex:[[build.targets objectAtIndex:i]intValue]];
                                if (temp){
                                    int c_dist = temp.distance;
                                    if (!l_dist || c_dist < l_dist){
                                        l_dist = c_dist;
                                        build.atk_id = temp.uniqueID;
                                        build.isAttacking = YES;
                                    }
                                }
                            }
                        }
                    }
                }
 }}


Unrecognized selector sent = calling a method on an object that doesn't exist. In this case, you're calling the distance method/property getter on a BuildingNode object.

The only distance I see is on your temp object, which is retrieved by EnemyNode *temp = [array objectAtIndex:[[build.targets objectAtIndex:i]intValue]]; That indicates you are really pulling a BuildingNode object out of the array instead of an EnemyNode. Find out why that happens.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜