我試圖從 - (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