-2

I'm trying to using randX and randY from -(void) awakeaccelerometer inside the second function -(void) accelerometer.. The program cannot recognize them. Thanks in advance.

-(void) awakeaccelerometer
{
    [[UIAccelerometer sharedAccelerometer]setUpdateInterval:1.0/50.0];
    [[UIAccelerometer sharedAccelerometer]setDelegate:self];

    float randX = arc4random() % 320;
    float randY = arc4random() % 548;

    CGPoint randNewPlace = CGPointMake(randX, randY);
    Rand.center = randNewPlace;

}


//Controlling the accelerometer
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
.....

CGRect blockRect = CGRectMake(newX, newY, 20, 20);
CGRect targetRect = CGRectMake(randX, randY, 20, 20);

    if (CGRectIntersectsRect(blockRect, targetRect))
    {
        float tnewX = arc4random() % 320;
        float tnewY = arc4random() % 548;

        CGPoint randNewPl = CGPointMake(tnewX, tnewY);
        Rand.center = randNewPl;
    }


}
Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
Apetro
  • 63
  • 7
  • 8
    Word to the wise: Look up how [braces](http://aelinik.free.fr/c/ch14.htm) vs. [class definition](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html) vs. [globals](http://stackoverflow.com/questions/3010647/shared-global-variables-in-c) affect the scoping of variables. – CodaFi Apr 01 '13 at 02:51
  • 1
    This is basic programming knowledge, find a tutorial on Objective-C. What you are looking for is instance variables. – Nathan Day Apr 01 '13 at 03:16
  • So my nested function randX and randY will only use the variable of the same name from the block scope, correct? – Apetro Apr 01 '13 at 03:23
  • No need to even find a tutorial for Objective-C. A C or Java tutorial will cover this. Very basic stuff you need to understand before you tackle Objective-C. – Hot Licks Apr 01 '13 at 03:40
  • So I declared randY and randX as static variables and went with that instead. It can track the coordinates of the first placement, but sticks with those coordinates forever. Is there anyway to return the values of the newer coordinates (tnewX, tnewY).. – Apetro Apr 01 '13 at 03:51
  • Yes I understand it's basic, i just can't grasp this small concept? Not sure why transitioning into Obj-c is seemingly difficult when i've already taken plenty of C classes.. – Apetro Apr 01 '13 at 03:52

1 Answers1

2

You are missing an understanding of the lifetime and scope of variables. The lifetime of a variable is the period of time when it exists and can contain a value. The scope of a variable is the range of code over which a variable can be accessed. A variable can be alive but out-of-scope (but not vice-versa).

The lifetime of the local variables randX and randY is from their point of declaration with the method awakeaccelerometer until the particular call of awakeaccelerometer which creates them returns. The scope of these two variables is the code from their point of declaration to the end of the method. When the call to CGMakePoint is made within awakeaccelerometer the two variables randX and randY stay alive but are out of scope - any code within CGMakePoint cannot reference them.

The two variables randX and randY are not in scope within accelerometer:didAccelerate:.

The answer to your question is if you wish these two variables to be available into both these methods then you need to promote them to an enclosing scope so they are both alive and in scope within the two methods. The usual suggestion is to promote them to instance variable (which are declared within a braced block at the start of an @interface or @implementation) - the lifetime of an instance variable is the same as the enclosing object, and the scope includes at least all the instance methods of that object.

But you really need to understand why this is so and whether that is what you need, these are fundamental to programming in any language, and you should read about and understand them. On SO try searching for "[objective-C] scope lifetime", you'll get quite a few hits. If you drop "[objective-C]" you'll get even more as the topics are discussed in relation to other languages. Even better look in a text on programming (language concepts), there are too many to list here!

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86