我试图从 - (void)awakeaccelerometer里面使用randX和randY函数 - 第二个函数 - (void)加速度计..程序无法识别它们。提前致谢。在不同的功能中使用变量? Objective-C
-(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;
}
}
聪明人的话:查找如何[括号](http://aelinik.free.fr/c/ch14.htm)与[类定义(http://developer.apple .com/library/ios /#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html)与[globals](http://stackoverflow.com/questions/3010647/shared-global-variables-in-c)影响变量的范围。 – CodaFi
这是基本的编程知识,在Objective-C上找到一个教程。你正在寻找的是实例变量。 –
所以我的嵌套函数randX和randY将只使用块范围中的同名变量,是否正确? – Apetro