0

我在头文件中初始化了两个属性:

@property (readwrite, assign) int Figure1;
@property (readwrite, assign) int State;

在.m

@synthesize Figure1;
@synthesize State;

然后我得到了

UISwipeGestureRecognizer *swipeLeft = 
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftMade:)] autorelease];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self addGestureRecognizer:swipeLeft];

UITapGestureRecognizer *oneFingerTwoTaps = 
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ooneFingerTwoTaps)] autorelease];
    [oneFingerTwoTaps setNumberOfTapsRequired:2];
    [oneFingerTwoTaps setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:oneFingerTwoTaps];

在类中初始化。

调用的方法:

- (void)ooneFingerTwoTaps
{
    [NSThread detachNewThreadSelector:@selector(oneFingerTwoTaps) toTarget:self withObject:nil];
}

- (void)swipeLeftMade:(UISwipeGestureRecognizer *)recognizer
{
    [NSThread detachNewThreadSelector:@selector(moveLeftSwipe) toTarget:self withObject:nil];
}

在第一个线程中有主程序:

- (void)oneFingerTwoTaps
{
    PlayScene *tView = [[PlayScene alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];
    [self addSubview:tView];
    while (GameState==GamePlaying) {
        Figure1 = 1; State = 1;
        [self moveFig];
    }
}

在第二个线程中,我需要使用在第一个线程上更改的属性值

-(void)moveLeftSwipe  {
int fset = State, figure=Figure1;
//some other stuff
}

但问题是属性的值不在线程之间共享,有人告诉我“非原子”可能会导致这样的问题,但我没有使用它。可能是我声明有问题吗?

4

1 回答 1

0

只是使用静态变量而不是属性。静态变量在线程之间共享。

于 2012-05-04T17:36:02.450 回答