【问题标题】:Call android runOnUiThread from cocos2dx may make the menutitem blink从 cocos2dx 调用 android runOnUiThread 可能会使菜单项闪烁
【发布时间】:2014-05-26 02:46:53
【问题描述】:

我最近一直在玩 cocos2dx,有时我需要调用 java 代码来完成一些 Android 特定的工作。因此,我在 Java 代码中添加了一个静态方法,并在调用 runOnUiThread 的方法中完成了这项工作。而C++调用静态java方法。

它确实工作正常,除了有时调用会使触发调用的 menuItem 闪烁,而在其他时候,它看起来很完美。

我有一个分享功能,代码如下所示,点击后分享按钮(菜单项)有时会闪烁。有人可以帮助我吗?谢谢!

我记得在某处看到过类似的问题,但今天无法谷歌搜索...

Java

...
public static void onShare(final int mode, final int score) {
    ((AppActivity)mContext).runOnUiThread(new Runnable() {
    public void run() {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        if(mode == 0) {
            sendIntent.putExtra(Intent.EXTRA_TEXT, ((AppActivity)mContext).getResources().getString(R.string.share_content0, score));
        }
        else {
            sendIntent.putExtra(Intent.EXTRA_TEXT, ((AppActivity)mContext).getResources().getString(R.string.share_content1, score));
        }
        sendIntent.setType("text/plain");
        ((AppActivity)mContext).startActivity(Intent.createChooser(sendIntent, ((AppActivity)mContext).getResources().getString(R.string.send_to)));
    }
    });
}
...

C++,平台.cpp

...
void doShare(int mode, int score) {
    JniMethodInfo t;
    if( JniHelper::getStaticMethodInfo(t,APPACTIVITY,
        "onShare", "(II)V")) {
        t.env->CallStaticIntMethod(t.classID, t.methodID, mode, score);
    }
}
...

C++,HelloScene.cpp

...
void GameBase::onGameOver(){
...
    auto menuShare = MenuItemFont::create(sr->getString(RSTR::share), CC_CALLBACK_1(GameBase::onShare, this));
...
}
...
void GameBase::onShare(Ref* pSender){
    CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_BTN);
#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
    doShare(this->_mode, this->_best);
#endif /* CC_TARGET_PLATFORM */
}
....

* sr->getString(RSTR::share),这是为了多语言支持,它在英文环境中返回文本'Share'。

AppDelegate.cpp

void AppDelegate::applicationDidEnterBackground() {
    director->pause();
    director->stopAnimation();
    GameBase *game;
    auto scene = director->getRunningScene();
    if (game = dynamic_cast<GameBase *>(scene->getChildByTag(TAG_GAMESCENE))) {
        game->onSaveProgress();
    }
}

void AppDelegate::applicationWillEnterForeground() {
    director->resume();
    director->startAnimation(); 
}

【问题讨论】:

  • 你指的这个闪烁到底是什么?请详细说明。
  • @Al-mo,我为不够清楚而道歉。闪烁是指菜单项在选定状态和正常状态之间频繁变化。以 MenuItemFont 为例,文字会在大尺寸和小尺寸之间变化。
  • 我必须看到它才能理解它,但是我们遇到了类似的问题,即精灵给出了生涩的效果(这也像你提到的那样断断续续),原因出来是切换我们的应用程序从后台/前台执行,而无需暂停动画......这可能是你的问题吗?这是我的猜测。
  • 感谢 cmets!我会尝试制作一个关于它的视频。我不确定你提到的动画是否起到了作用。但是我在appDelegate.cpp文件中并没有太大的改变,背景/前景之间切换的默认动作有处理动画的代码。您是偶尔还是经常看到生涩效应?
  • 大多数时候(在 Facebook 登录期间,当我们按下按钮时它会产生生涩的效果),下面的代码段修复了它 >>> applicationDidEnterBackground >>> CCDirector::sharedDirector()- >暂停(); ======== AND ======== >>> applicationWillEnterForeground >>> CCDirector::sharedDirector()->resume();

标签: android cocos2d-x cocos2d-x-3.0


【解决方案1】:

在您的 AppDelegate.cpp 中

函数 >>> applicationDidEnterBackground

And in Function >>> applicationWillEnterForeground

我建议您使用 c++ 指令来区分 iOS 和 Android 的代码片段。

对于 Android 部分,仅使用 PAUSE/RESUME 对,对于 iOS,使用您粘贴的那个。

原来如此

//applicationDidEnterBackground 
#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
    director->pause();
#else
    director->pause();
    director->stopAnimation();
#endif

//applicationWillEnterForeground 
#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
    director->resume();
#else
    director->resume();
    director->startAnimation(); 
#endif

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我在 platform.cpp 等效项中使用以下代码

        #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        void onShare() {
             cocos2d::JniMethodInfo t;
            if (cocos2d::JniHelper::getStaticMethodInfo(t
                                                        , NativeActivityClassName
                                                        , "onShare"
                                                        , "()V"))
            {
                t.env->CallStaticVoidMethod(t.classID, t.methodID);
                t.env->DeleteLocalRef(t.classID);
            }
        }
    
        #endif
    

    它可以在不闪烁共享按钮的情况下正常工作。你是用其他方式打电话吗?

    【讨论】:

    • 你能分享你创建菜单项的那一行吗?
    • 感谢回复,代码如下: auto share = MenuItemFont::create("Share", CC_CALLBACK_1(HelloWorld::onShareCallback, this));自动菜单 = Menu::create(share, nullptr);正如我所说,大多数情况下它都能完美运行,但如果我仔细观察分享按钮,它有时会在较大尺寸和较小尺寸之间频繁变化,并且只有在分享对话框关闭后才会停止。它确实并不困扰我,毕竟它不是很明显,但我只是想知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多