0

ヘッダー ファイルで初期化された 2 つのプロパティを取得しました。

@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];
    }
}

2 番目のスレッドでは、最初のスレッドで変更されたプロパティの値を使用する必要があります

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

しかし問題は、プロパティの値がスレッド間で共有されていないことです。「非アトミック」はそのような問題を引き起こす可能性があると言われましたが、使用しませんでした。私は何か間違ったことを宣言している可能性がありますか?

4

1 に答える 1

0

プロパティの代わりに静的変数を使用しました。静的変数はスレッド間で共有されます。

于 2012-05-04T17:36:02.450 に答える