【发布时间】: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