1

I am having trouble getting objects added to my NSMutableArray to log properly (which definitely means they won't process any of the appropriate functions correctly) with Spritebuilder [version 1.4.9, from the Apple App Store]. I am creating several objects using the same class, but each new one is overriding the older objects which exist. I thought an array would help keep things in order (and then on collision, I could call the array to check for which object was collided with), but it simply is not working that way - at all. Here is the relevant code.

Main.h

@property Coven *coven;
@property Nellie *nellie;
@property NSMutableArray *array;  
//Physics, other things

Main.m

/Adding other things...

-(void) addCovenMember{
//This function is called on a RANDOM time interval
_array = [[NSMutableArray] alloc]init];
for (i = 0, i < 15, i++){
    _coven = (Coven*) [CCBReader load:@"CovenMember"];
    [_array addChild:_coven];
    }
[_physicNode addChild:_coven];
}

-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair nellie:(Nellie*)nellie coven:(Coven*)coven{
for (_coven in _array){
    NSLog(@"%@",_coven.name)
       if (CGRectIntersectsRect(_nellie.boundingBox, _coven.boundingBox){
       NSLog(@"We're intersecting!");
    }
}

Coven. h

//Nothing Important Here

Coven.m

-(void)didLoadFromCCB{
self.physicsBody.CollisionType = @"coven";
}

Nellie.h

//Nothing Here

Nellie.m

-(void) didLoadFromCCB{
self.physicsBody.CollisionType = @"nellie";
}

The collision is logging with every collision - but only as the name of the LATEST _coven member to be generated, no matter what I am colliding with. This also means that the _coven.boundingBox is solely on the latest _coven member and interaction only occurs when I hit the new member as soon as it generates on to the screen.

Any ideas? Any help?

Note: This is also posted on the Spritebuilder website - I decided to post it here as well because answers can be a little slow on those forums.

Misha Stone
  • 631
  • 7
  • 22

2 Answers2

0

The -(void) addCovenMember overwrites (creates a new instance) of _array every time it's called. Thus, when you try to iterate in -ccPhysicsCollisionBegin: you'll only ever see 1 coven.

Add a nil check around your array creation:

if(_array == nil) {
     _array = [[NSMutableArray] alloc]init];
}

The for loop in the -addCovenMember method looks broken (at least not a c loop). Reaplace the , with ;:

for (i = 0; i < 15 i++){

Also, using for(_coven in _array) seems wrong, you already have a property self.coven (presumably) with a backing _coven ivar. Try changing it to for(Coven * c in self.array) and use the local c in the loop:

for (Coven * c in _array){
    NSLog(@"%@",c.name)
    if (CGRectIntersectsRect(_nellie.boundingBox, c.boundingBox){
       NSLog(@"We're intersecting!");
    }
}
thelaws
  • 7,991
  • 6
  • 35
  • 54
  • thank you for the suggestions, tried your edits, but still the same problem (added the nil check, that was brilliant), replaced `,` with `;`, and tried the change of `_coven` to a local ivar (`Coven* c`) - still no dice. I did decide to add a log in there to see if I could understand what is happening, and I just realized that the collision logs (that initial `NSLog(@"%@",_coven.name`), logs i# blurbs of the same name. So if it's 3, it logs '`WitchA`' 3 times - of course, if WitchA is the latest one to have been generated. – Misha Stone Jul 28 '15 at 20:20
  • When you change the loop variable to `c`, you have to replace the use of `_coven` with `c` inside the loop. Also, I'm not sure why you're passing a `Coven` in it it's not used (it uses the `Coven`'s in the array) However, based on the code you posted (esp. the for loop), there are potentially many bugs throughout the app which could effect your outcome. – thelaws Jul 28 '15 at 20:23
  • so the loop in of itself seems fine? Or are you saying my loop is faulty and therefore my app in general is faulty (not offended, just for clarification) – Misha Stone Jul 28 '15 at 20:26
  • The for loop is a very basic building block, and it's (repeated) misuse shows the developers inexperience with the language (C in this case). This is reinforced by the use of `_coven` in the loop, which is a reference to the `@property` that was defined above (that's an Obj-C feature). These issues just make me skeptical that the omitted code works as intended. – thelaws Jul 28 '15 at 20:31
  • Basically, I've made as many suggestions as I can based on the code that was posted, and beyond that it's hard to tell if there's still issues. – thelaws Jul 28 '15 at 20:39
0

To everyone out in the world struggling with their ccPhysicsCollisions, arrays may not be the answer - this was a simple fix that left me incapacitated for days.

Using the basic ccPhysicsCollisionsBegan that ships with spritebuilder, try this without arrays first:

Scene.m
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair nellie:(Nellie*)nellie coven:(Coven*)coven{
[_coven stopAction:coven.path];
}

I initially created the method with:

[_coven stopAction:_coven.path];

Yes, that (underscore) set me back three weeks. Be sure you refer to the object interacting through the physics delegate, and not the object itself, which in my case, was constantly being overwritten by the new ones being generated.

Check your underscores.

Solved! :D

Thanks to @thelaws for your help! I'll get better at Obj C... eventually.

Misha Stone
  • 631
  • 7
  • 22