最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Incisor press callbacks not responding as expected - Stack Overflow

programmeradmin3浏览0评论

I'm building a game using Incisor with two buttons, "Stop" and "Go", that act as toggle buttons. The behavior is as follows: when the "Go" button is visible, the action is stopped. Pressing "Go" starts the action, hides the "Go" button, and shows the "Stop" button in its place. However, even when the "Go" button is visible, I am only receiving the "Stop" action in the callback.

Here's the code:

class ProjectMain
{ 

   init() {

        this.goButton = nc.addButton( nc.graphicAssets.go, nc.mainScene, "Go" );
        this.stopButton = nc.addButton( nc.graphicAssets.stop, nc.mainScene, "Stop" );

        // initially the stop button is not visible
        this.stopButton.visible = false;
        
        // add the press callback to the buttons
        this.goButton.addPressCallback( this, "onPress", ["Go"] );
        this.stopButton.addPressCallback( this, "onPress", ["Stop"] );
    }

    onPress( event, camera, action ) {

        console.log(action); // this is always Stop??

        //toggle visibility
        if ( "Go" == action ) {
            this.stopButton.visible = true;
            this.goButton.visible = false;
        } else {
            this.stopButton.visible = false;
            this.goButton.visible = true;
        }
    }
}

Even when the "Go" button is visible, the callback always says "Stop" instead of "Go". Why is this happening, and how can I ensure the correct action is triggered?

I'm building a game using Incisor with two buttons, "Stop" and "Go", that act as toggle buttons. The behavior is as follows: when the "Go" button is visible, the action is stopped. Pressing "Go" starts the action, hides the "Go" button, and shows the "Stop" button in its place. However, even when the "Go" button is visible, I am only receiving the "Stop" action in the callback.

Here's the code:

class ProjectMain
{ 

   init() {

        this.goButton = nc.addButton( nc.graphicAssets.go, nc.mainScene, "Go" );
        this.stopButton = nc.addButton( nc.graphicAssets.stop, nc.mainScene, "Stop" );

        // initially the stop button is not visible
        this.stopButton.visible = false;
        
        // add the press callback to the buttons
        this.goButton.addPressCallback( this, "onPress", ["Go"] );
        this.stopButton.addPressCallback( this, "onPress", ["Stop"] );
    }

    onPress( event, camera, action ) {

        console.log(action); // this is always Stop??

        //toggle visibility
        if ( "Go" == action ) {
            this.stopButton.visible = true;
            this.goButton.visible = false;
        } else {
            this.stopButton.visible = false;
            this.goButton.visible = true;
        }
    }
}

Even when the "Go" button is visible, the callback always says "Stop" instead of "Go". Why is this happening, and how can I ensure the correct action is triggered?

Share Improve this question asked Jan 18 at 0:17 JohnJohn 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The issue is that visibility doesn’t affect whether a button is active. A button can be transparent and still respond to user interactions, or vice versa. To achieve the behavior you're looking for, use the enable() and disable() functions. These methods will make the button both invisible and inactive. Additionally, disabling a button (or any child of SceneObject) improves performance, as it reduces the GPU workload. Even transparent objects still require GPU resources to render, so disabling them can help optimize performance.

class ProjectMain
{

   init() {

        this.goButton = nc.addButton( nc.graphicAssets.go, nc.mainScene, "Go" );
        this.stopButton = nc.addButton( nc.graphicAssets.stop, nc.mainScene, "Stop" );

        // initially, DISABLE the stop button
        this.stopButton.disable();
        
        // add the press callback to the buttons
        this.goButton.addPressCallback( this, "onPress", ["Go"] );
        this.stopButton.addPressCallback( this, "onPress", ["Stop"] );
    }

    onPress( event, camera, action ) {

        console.log(action); // expected action

        //toggle DISABLED/ENABLED
        if ( "Go" == action ) {
            this.stopButton.enable();
            this.goButton.disable();
        } else {
            this.stopButton.disable();
            this.goButton.enable();
        }
    }

}

发布评论

评论列表(0)

  1. 暂无评论