0

I'm trying to build a project incorporating both SpriteBuilder and RFduino and am having trouble coordinating the two in iOS/XCODE. I have a method, checkDevice(), that looks like this:

NSLog(@"Checking device: %@", rfduino);
NSLog(@"Physics node: %@", _physicsNode);
if (rfduino.advertisementData){
    NSString *advertising = @"";
    if (rfduino.advertisementData) {
        advertising = [[NSString alloc] initWithData:rfduino.advertisementData encoding:NSUTF8StringEncoding];
        NSLog(@"advertisement: %@", advertising);
    }

    int numLargeParticles = [[advertising substringToIndex:4] intValue];
    int numSmallParticles = [[advertising substringFromIndex:4] intValue];

    [self updateLargeParticles:numLargeParticles andSmallParticles:numSmallParticles];
}
else {
    NSLog(@"no connected rfduino found");
}

//recursive so always checking
[self performSelector:@selector(checkDevice) withObject:nil afterDelay:PARTICLE_CHECKING_DELAY];

The rfduino variable is set from another class, ScanViewController, while the _physicsNode is set when the CCB file is created. The method updateLargeParticles:andSmallParticles basically takes the data from the rfduino advertisement and uses it to add nodes to the physicsNode.

Depending on where I call checkDevice() from, one of these two variables is null.

  • If I call [self checkDevice] at the end of didLoadFromCCB, the rfduino variable is always null, even after subsequent recursive calls (when other log statements show that the variable has been successfully set).

  • If I call [self checkDevice] at the end of the method that sets the rfduino variable, the _physicsNode is always null (again even after subsequent recursive calls).

I think that I am misunderstanding global variables and that they are not persisting across multiple methods. Any insight would be greatly appreciated and I would be more than happy to provide more information/code/etc.

Here is a sample of the log output, as well:

RFduino is not found (ie method called from didLoadFromCCB):

ParticlesPhysics[695:86622] Checking device: (null)
ParticlesPhysics[695:86622] Physics node: <CCPhysicsNode = 0x13d62d5d0 | Name = >
ParticlesPhysics[695:86622] no connected rfduino found

PhysicsNode is not found (ie method called from RFduino setter):

ParticlesPhysics[695:86622] Checking device: <RFduino: 0x1740d4270>
ParticlesPhysics[695:86622] Physics node: (null)
ParticlesPhysics[695:86622] advertisement: 00250108
ParticlesPhysics[695:86622] 25, 108
ParticlesPhysics[695:86622] Physics node: (null)

both of the variables are declared in MainScene.h as follows

@property(strong, nonatomic) RFduino *rfduino;
@property CCPhysicsNode *_physicsNode;

and then synthesized in MainScene.m

@synthesize rfduino;
@synthesize _physicsNode;

I've tried declaring them within the implementation as well (as below), but this causes the same error

@implementation MainScene{
    CCPhysicsNode *_physicsNode;
    RFDuino *rfduino;
    CCNode *_bottomBorder;
    CCEffectBrightness *_brightnessEffect;
    CCEffectStack *_effectStack;
    CCLabelTTF *_largeLabel;
    CCLabelTTF *_smallLabel;
}

rfduino is set from the following method

(void) log:(RFduino *)rfduino_instance {
rfduino = rfduino_instance;
NSLog(@"log: rfduino is %@", rfduino);

[self checkDevice];
}

which is called from a ScanViewController class

(void)didConnectRFduino:(RFduino *)rfduino
{
NSLog(@"didConnectRFduino");

[rfduinoManager stopScan];
loadService = false;

MainScene *mainScene = [MainScene alloc];
[mainScene performSelector:@selector(log:) withObject:rfduino afterDelay:1.0];      //initial delay to give time for setup

}

the physicsNode isn't explicitly declared programmatically, it is created in SpriteBuilder.

Thank you! C

Christie
  • 1
  • 2
  • Could we see the code declaring rfduino and _physicsNode, as well as the code that sets them? I doubt the problem is in checkDevice. – DrGodCarl Sep 14 '15 at 16:06
  • If rfduino a member variable? If so, it will keep its value as long as the object it is a member of is alive. Perhaps you created two class instances ? –  Sep 14 '15 at 16:07
  • @MichaelL The variables are defined in MainScene, so (I think) they're members of that class, but there is only one MainScene and only one Scene (CCNode) total in my application, so I'm not sure how this would be the problem. – Christie Sep 14 '15 at 16:21

0 Answers0