【问题标题】:Android, how to pass values between two functions?Android,如何在两个函数之间传递值?
【发布时间】:2014-05-23 12:58:50
【问题描述】:

我正在尝试将 PosXPosY 值传递给 onLoadScene 函数,但我尝试了各种方法都找不到解决方案。任何帮助将不胜感激。

public class GameField extends LayoutGameActivity implements CvCameraViewListener2
{
      @Override
      public Scene onLoadScene() {
                 this.mScene = new Scene();
                 spritePic1.setPosition((int)PosX- textRegPic1.getWidth()/2,(int) PosY- textRegPic1.getHeight()/2);

    return this.mScene;
        }
        @Override
        public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

                mRgba = inputFrame.rgba();  

                        PosX= Math.round(data2[i]);
                        PosY = Math.round(data2[i+1]);            
               return mRgba; 
        }
}

我编辑了我的问题。 OnLoadScene 是 LayoutGameActiviy 的方法。这就是为什么我不能向 onLoadScene 添加任何参数的原因。

【问题讨论】:

  • 什么问题!!,哪个函数先运行?
  • 虽然这个问题已经(正确地)回答了,但我不得不问 - 还有多少其他方法可以将值从一个函数传递到另一个函数?你说你“尝试了各种方法”;你尝试了什么?
  • 你说的太对了。我无法准确解释。但是由于LayoutGameActivity的方法,我无法在onLoadScene中添加任何参数。

标签: java android opencv andengine


【解决方案1】:
  public Scene onLoadScene(float PosX,float PosY) {//PosX,PosY is float you can take as per your choice
             this.mScene = new Scene();
             spritePic1.setPosition((int)PosX- textRegPic1.getWidth()/2,(int) PosY- textRegPic1.getHeight()/2);

return this.mScene;
    }

调用函数

onLoadScene(PosX,PosY)

【讨论】:

  • 谢谢你的回答,但是我不能给onLoadScene添加任何参数,因为那是GameActivity的方法。
  • 全局定义 float PosX,PosY 并将值放入 posX,PosY 中您要提供的位置,然后在 onLoadScene() 中使用该 PosX,PosY
  • 我已经定义为全局并将值放在 onCameraFrame 中,但结果在 onLoadScene 中返回零。也许问题是两个功能必须同时工作?
  • 我解决了这个问题。问题是关于更新场景。刚刚添加了更新处理程序,它就起作用了。无论如何感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
相关资源
最近更新 更多