在手势识别器功能中设置时变量状态丢失

发布于 2024-11-26 01:00:22 字数 1693 浏览 0 评论 0原文

我正在使用 UIPanGestureRecognizer 来检测我的应用程序中的滑动。手势识别器正确触发,但是当我设置变量或尝试从其中调用函数时,变量被设置,但在下一次迭代(例如我的勾选函数)时,变量未被设置。好像是在不同的实例上设置它?有人可以帮我吗?

因此,_ApplyForcehandlePan 函数中设置为 true,但当我尝试在其他地方(在我的 tick 函数中)读取它时,它始终为 false。

更新:我添加了所有使用 _ApplyForce 的地方,以帮助展示它的使用方式。另外,我使用这个变量触发力脉冲的原因是因为 这个问题,我似乎无法从手势函数中触发力量,经过一番调查后发现我无法更改或更改任何类变量 - 因此出现了新问题。

AppDelegate.mm:HelloWorldLayer.h

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    ....
    HelloWorldLayer *layer = (HelloWorldLayer *) [[HelloWorldLayer scene].children objectAtIndex:0];
    UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
    [viewController.view addGestureRecognizer:gestureRecognizer];
}

HelloWorldLayer.mm

@interface HelloWorldLayer : CCLayer
{
    ...

    bool _ApplyForce;
}

- (id)init {
    if ((self=[super init])) {
         _ApplyForce = false;
    }
}

- (void)tick:(ccTime) dt {
    _world->Step(dt, 10, 10);

    if (_ApplyForce)
    {
        NSLog(@"apply force");
        b2Vec2 force = b2Vec2(200, 200);
        _body->ApplyLinearImpulse(force,_body->GetWorldCenter());
        _ApplyForce = false;
    }
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
            _ApplyForce=true;
    }      
}

I am using UIPanGestureRecognizer to detect swipes in my app. The gesture recogniser fires correctly but when I set a variable or try to call a function from within it the variables are set but, on next iteration (of my tick function for example) the variable is not set. It is as if it is setting it on a different instance? Can anyone help me out here?

So _ApplyForce gets set to true in the handlePan function BUT it is always false when I try to read it elsewhere (in my tick function).

Update: I have added all the places _ApplyForce is used to help show how it is used. Also, the reason I use this variable to trigger off the force impulse is because of this question where I couldn't seem to trigger a force from within the gesture function, after some investigation it turned out I couldnt change or alter any class variable - hence the new question.

AppDelegate.mm:

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    ....
    HelloWorldLayer *layer = (HelloWorldLayer *) [[HelloWorldLayer scene].children objectAtIndex:0];
    UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
    [viewController.view addGestureRecognizer:gestureRecognizer];
}

HelloWorldLayer.h

@interface HelloWorldLayer : CCLayer
{
    ...

    bool _ApplyForce;
}

HelloWorldLayer.mm

- (id)init {
    if ((self=[super init])) {
         _ApplyForce = false;
    }
}

- (void)tick:(ccTime) dt {
    _world->Step(dt, 10, 10);

    if (_ApplyForce)
    {
        NSLog(@"apply force");
        b2Vec2 force = b2Vec2(200, 200);
        _body->ApplyLinearImpulse(force,_body->GetWorldCenter());
        _ApplyForce = false;
    }
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
            _ApplyForce=true;
    }      
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2024-12-03 01:00:22

这不是因为在你的 tick 方法中,如果 _ApplyForce 为 true ,则语句末尾将其设置为 false 吗?这意味着在它为真之后它立即就是假的。

我可能建议添加一些 NSLogs 来跟踪刻度和 _ApplyForce 状态。

Isn't that because in your tick method if _ApplyForce is true the end of the statement sets it to false? Which would mean that immediately after it is true it is false.

I might recommend adding some NSLogs to track ticks and _ApplyForce states.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文