对于Obj-C,我很新,所以如果问题相当明显,我们很抱歉!从另一个方法引用一个变量objective-c
无论如何,我只是进入objective-c并尝试创建一个基本的iOS计数器,每次用户点击/触摸UI时,点计数器都会增加。
我已经创建了currentScore int并可以将其记录到控制台。我还将每次触摸的UI都成功记录到控制台。
什么我现在试图做的是,用1
正如我说的,可能很容易访问每个触摸currentScore int和递增int,但不能100%确定如何引用其他变量在其他函数中!
在此先感谢。
//
// JPMyScene.m
//
#import "JPMyScene.h"
@implementation JPMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [UIColor redColor];
//Initiate a integer point counter, at 0.
int currentScore = 0;
NSLog(@"Your score is: %d", currentScore);
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
NSLog(@"Tap, tap, tap");
//TODO: For each tap, increment currentScore int by 1
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end
全局变量?请不要将这样的东西教给面向对象的初学者,事情可能很容易脱离他们的手。顺便说一句,静态的会有同样的效果,但没有讨厌的“全局性”。 – Cristik 2015-04-14 18:20:22