1

I am making an android game. I am using RelativeLayout, which hosts a FrameLayout, which hosts a SurfaceView. However, i have added a textview onto the RelativeLayout to show how many health points the player has left. I have a method which sets the int value of how many health points he has left into the text view. The int value is located in the SurfaceView, which is another class. But the textview and the method which updates the textview, is inside the other class. I want the method in the other class to be constantly updated, so that the textview will always display the health value. How can i do this? The method is called updateHealthPointsTextView(). Please help me and thanks so much! My code:

package com.mysoftwaremobileapps.alien.attack;

import java.util.ArrayList;

public class GameScreenActivity extends Activity {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
RadioButton radioEasy, radioMedium, radioHard;
private ExampleView eView;
public int ParachuterIndex;
TextView healthPoints;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dropParachuters();
}
private void dropParachuters() { 
    // TODO Auto-generated method stub
}
    @Override
    protected void onStart()
    {
        FrameLayout fl = new FrameLayout(this);
        eView = new ExampleView(this);
        fl.addView(eView);

        healthPoints  = new TextView(this);
        healthPoints.setText("Health Points: " + eView.getThread().healthPoints);

        RelativeLayout relativeLayout= new RelativeLayout(this);
        relativeLayout.addView(fl);
        relativeLayout.addView(healthPoints);
        setContentView(relativeLayout);

        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().drawAlien();
        eView.getThread().publicFloatX = 750;

        eView.getThread().gettingAttacked();

        updateHealthPointsTextView();

        super.onStart();

        try {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                String value = extras.getString("KEY");
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

}

    private void updateHealthPointsTextView() {
        // TODO Auto-generated method stub
                    //Updating textview with health points value
            healthPoints.setText(Integer.toString(eView.getThread().healthPoints));
        }
    }
    @Override
    public boolean onKeyDown(int KeyCode, KeyEvent Event) {
        if ((KeyCode == KeyEvent.KEYCODE_MENU)) {
            //Call performSpecialAttack()
            Log.d("Parachute Hunter", "calling performSpecialAttack()");
            eView.getThread().performSpecialAttack();
            return true;
        }
        return super.onKeyDown(KeyCode, Event);
    }


@Override
protected void onStop()
{
    try {
        eView.getThread().setRunning(false);
        eView = null;
    }
    catch (NullPointerException e) {}

    super.onStop();
}
}
user1446632
  • 417
  • 2
  • 9
  • 24

3 Answers3

0

How about having a method like this:

public void setHealthPoints(int points) {
        eView.getThread().healthPoints = points;
        //Updating textview with health points value
        healthPoints.setText("Health Points: " + Integer.toString(points));
    }
}

call it from everywhere, it should not block the GUI-Thread

reggaemuffin
  • 1,188
  • 2
  • 11
  • 26
0

just pass the reference of textview in constructor of that class and use handler to update it frequently.

public final class MethodUtil {
TextView mTextView;

public MethodUtil(TextView mTextView) {
//this.mTextView =(TextView)mTextView.findViewById(R.id.textview);
    this.mTextView=mTextView;
}
public void updateHealthPointsTextView(){
mTextView.post(new Runnable() {

    @Override
    public void run() {
        mTextView.setText("Health Points: " +     eView.getThread().healthPoints);

    }
});
}
}
Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
  • Could you just type what i new to type? I dont understand this. What should i call? If i just type that into my code it does nothing – user1446632 Jul 18 '12 at 14:47
  • i thought of you are updating your text view from other thread or class or for updating UI from other thread you need handler that what i have implemented ,, i used constructor to pass the view so that you can update the test view from that class also – Vipin Sahu Jul 19 '12 at 09:40
0

I fixed it by simply making a countdown timer counting down from 1 second and then restarting itself and updating the textview when it's fininished. It works

        public void updateHealthPointsTextView() {
        //The aliens are firing and throwing rocks, make the defender lose health
        new CountDownTimer(1000, 1000) {

             public void onTick(long millisUntilFinished) {
             }

             public void onFinish() {
                 //Updating textview
                healthPoints.setText("Health points: " + Integer.toString(eView.getThread().healthPoints));
                updateHealthPointsTextView();
             }
          }.start();
    }
user1446632
  • 417
  • 2
  • 9
  • 24