UndeadLeech
- Forum posts: 3
Aug 17, 2013, 7:16:29 PM via Website
Aug 17, 2013 7:16:29 PM via Website
I have a GameLayout class, which is a View class for animating a Sprite, next to my main game XML.
I update this animation by using view.invalidate() in the Runnable of my main game activity. So every time the run() method is called the Bitmap updates 4 times (so it's exactly through 1 time).
My problem is, that if I click on the button on my XML the Bitmap sometimes refreshs, too, what destroys everything.
If you have any solutions for my problem how I can avoid my Image to refresh please tell me.
This is the GameLayout class:
Would be nice if you could help me out with this!
I update this animation by using view.invalidate() in the Runnable of my main game activity. So every time the run() method is called the Bitmap updates 4 times (so it's exactly through 1 time).
My problem is, that if I click on the button on my XML the Bitmap sometimes refreshs, too, what destroys everything.
If you have any solutions for my problem how I can avoid my Image to refresh please tell me.
This is the GameLayout class:
1public class GameLayout extends View
2{
3 private Sprite sprite;
4 private Bitmap bmp;
5
6 public GameLayout(Context context)
7 {
8 super(context);
9 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.froschanimation);
10 sprite = new Sprite(bmp, 0, 0, 400, 100, 5, 4);
11 }
12
13 @Override
14 protected void onDraw(Canvas canvas)
15 {
16 sprite.draw(canvas);
17 update(System.currentTimeMillis());
18 }
19
20 public void update(long currentTimeMillis)
21 {
22 sprite.update(currentTimeMillis);
23 }
24}
And this is my main Game Activity:2{
3 private Sprite sprite;
4 private Bitmap bmp;
5
6 public GameLayout(Context context)
7 {
8 super(context);
9 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.froschanimation);
10 sprite = new Sprite(bmp, 0, 0, 400, 100, 5, 4);
11 }
12
13 @Override
14 protected void onDraw(Canvas canvas)
15 {
16 sprite.draw(canvas);
17 update(System.currentTimeMillis());
18 }
19
20 public void update(long currentTimeMillis)
21 {
22 sprite.update(currentTimeMillis);
23 }
24}
1public class GameActivity extends Activity implements OnClickListener, Runnable
2{
3 private int punkte;
4 private int highscore;
5 private Handler handler = new Handler();
6 private int balken = 0;
7 private boolean abnehmend = false;
8 private int Klick = 0;
9 private boolean balkenAktiv = true;
10 private boolean gewartet = false;
11 private int versuche;
12 private View view;
13 private int counter = 0;
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState)
17 {
18 super.onCreate(savedInstanceState);
19
20 // inflate mainXML->
21 View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
22 // find container->
23 LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
24 // initialize your custom view->
25 view = new GameLayout(this);
26 // add your custom view to container->
27 container.addView(view);
28
29 setContentView(mainView);
30
31 versuche = leseVersuche();
32 highscore = leseHighscore();
33 setupActionBar();
34 neueRunde();
35
36 Button button = (Button) findViewById(R.id.thebutton);
37 button.setOnClickListener(this);
38 }
39
40 /**
41 * Set up the {@link android.app.ActionBar}, if the API is available.
42 */
43 @TargetApi(Build.VERSION_CODES.HONEYCOM
44 private void setupActionBar()
45 {
46 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOM
47 {
48 getActionBar().setDisplayHomeAsUpEnabled(true);
49 }
50 }
51
52 @Override
53 public boolean onCreateOptionsMenu(Menu menu)
54 {
55 // Inflate the menu; this adds items to the action bar if it is present.
56 getMenuInflater().inflate(R.menu.game, menu);
57 return true;
58 }
59
60 @Override
61 public boolean onOptionsItemSelected(MenuItem item)
62 {
63 switch (item.getItemId())
64 {
65 case android.R.id.home:
66 NavUtils.navigateUpFromSameTask(this);
67 return true;
68 }
69 return super.onOptionsItemSelected(item);
70 }
71
72 private void neueRunde()
73 {
74 punkte = 0;
75 refreshScreen();
76 }
77
78 private void refreshScreen()
79 {
80 TextView tvPunkte = (TextView) findViewById(R.id.points);
81 tvPunkte.setText("Punkte: " + Integer.toString(punkte));
82
83 TextView tvHighscores = (TextView) findViewById(R.id.highscores);
84 tvHighscores.setText("Highscore: " + Integer.toString(highscore));
85
86 TextView tvVersuche = (TextView) findViewById(R.id.versuches);
87 tvVersuche.setText("Versuche: " + Integer.toString(versuche));
88 }
89
90 @Override
91 public void onClick(View v)
92 {
93 if(Klick == 0)
94 {
95 handler.postDelayed(this, 0);
96 Klick = 1;
97 }
98 else
99 {
100 if(Klick == 1)
101 {
102 balkenAktiv = false;
103 handler.postDelayed(this, 500);
104 punkte = balken;
105 versuche++;
106 schreibeVersuche(versuche);
107 highscore += punkte;
108 schreibeHighscore(highscore);
109 Klick = 2;
110 refreshScreen();
111 }
112 else
113 {
114 if(Klick == 2)
115 {
116 Klick = 0;
117 punkte = 0;
118 balken = 0;
119 ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
120 proBar.setProgress(balken);
121 refreshScreen();
122 }
123 }
124 }
125 }
126
127 @Override
128 public void run()
129 {
130 if(balkenAktiv == true)
131 {
132 gewartet = false;
133 ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
134 if(abnehmend == false)
135 {
136 if(balken < 100)
137 {
138 balken+=5;
139 proBar.setProgress(balken);
140 refreshScreen();
141 handler.postDelayed(this, 0);
142 }
143 else if(balken == 100)
144 {
145 abnehmend = true;
146 proBar.setProgress(balken);
147 refreshScreen();
148 handler.postDelayed(this, 0);
149 }
150 }
151
152 else if(abnehmend == true)
153 {
154 if(balken > 0)
155 {
156 balken-=5;
157 proBar.setProgress(balken);
158 refreshScreen();
159 handler.postDelayed(this, 0);
160 }
161 else if (balken == 0)
162 {
163 abnehmend = false;
164 proBar.setProgress(balken);
165 refreshScreen();
166 handler.postDelayed(this, 0);
167 }
168 }
169 }
170 else if (balkenAktiv == false)
171 {
172 if(gewartet == true)
173 {
174 if(counter < 4)
175 {
176 counter++;
177 view.invalidate();
178 handler.postDelayed(this, 500);
179 }
180 else if (counter >= 4)
181 {
182 balkenAktiv = true;
183 counter = 0;
184 }
185 }
186 gewartet = true;
187 }
188 }
189
190 private void schreibeHighscore(int highscore)
191 {
192 SharedPreferences pref = getSharedPreferences("GAME", 0);
193 SharedPreferences.Editor editor = pref.edit();
194 editor.putInt("HIGHSCORE", highscore);
195 editor.commit();
196 }
197
198 private void schreibeVersuche(int versuche)
199 {
200 SharedPreferences pref = getSharedPreferences("GAME", 0);
201 SharedPreferences.Editor editor = pref.edit();
202 editor.putInt("VERSUCHE", versuche);
203 editor.commit();
204 }
205
206 private int leseVersuche()
207 {
208 SharedPreferences pref = getSharedPreferences("GAME", 0);
209 return pref.getInt("VERSUCHE", 0);
210 }
211
212 private int leseHighscore()
213 {
214 SharedPreferences pref = getSharedPreferences("GAME", 0);
215 return pref.getInt("HIGHSCORE", 0);
216 }
217}
2{
3 private int punkte;
4 private int highscore;
5 private Handler handler = new Handler();
6 private int balken = 0;
7 private boolean abnehmend = false;
8 private int Klick = 0;
9 private boolean balkenAktiv = true;
10 private boolean gewartet = false;
11 private int versuche;
12 private View view;
13 private int counter = 0;
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState)
17 {
18 super.onCreate(savedInstanceState);
19
20 // inflate mainXML->
21 View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
22 // find container->
23 LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
24 // initialize your custom view->
25 view = new GameLayout(this);
26 // add your custom view to container->
27 container.addView(view);
28
29 setContentView(mainView);
30
31 versuche = leseVersuche();
32 highscore = leseHighscore();
33 setupActionBar();
34 neueRunde();
35
36 Button button = (Button) findViewById(R.id.thebutton);
37 button.setOnClickListener(this);
38 }
39
40 /**
41 * Set up the {@link android.app.ActionBar}, if the API is available.
42 */
43 @TargetApi(Build.VERSION_CODES.HONEYCOM
44 private void setupActionBar()
45 {
46 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOM
47 {
48 getActionBar().setDisplayHomeAsUpEnabled(true);
49 }
50 }
51
52 @Override
53 public boolean onCreateOptionsMenu(Menu menu)
54 {
55 // Inflate the menu; this adds items to the action bar if it is present.
56 getMenuInflater().inflate(R.menu.game, menu);
57 return true;
58 }
59
60 @Override
61 public boolean onOptionsItemSelected(MenuItem item)
62 {
63 switch (item.getItemId())
64 {
65 case android.R.id.home:
66 NavUtils.navigateUpFromSameTask(this);
67 return true;
68 }
69 return super.onOptionsItemSelected(item);
70 }
71
72 private void neueRunde()
73 {
74 punkte = 0;
75 refreshScreen();
76 }
77
78 private void refreshScreen()
79 {
80 TextView tvPunkte = (TextView) findViewById(R.id.points);
81 tvPunkte.setText("Punkte: " + Integer.toString(punkte));
82
83 TextView tvHighscores = (TextView) findViewById(R.id.highscores);
84 tvHighscores.setText("Highscore: " + Integer.toString(highscore));
85
86 TextView tvVersuche = (TextView) findViewById(R.id.versuches);
87 tvVersuche.setText("Versuche: " + Integer.toString(versuche));
88 }
89
90 @Override
91 public void onClick(View v)
92 {
93 if(Klick == 0)
94 {
95 handler.postDelayed(this, 0);
96 Klick = 1;
97 }
98 else
99 {
100 if(Klick == 1)
101 {
102 balkenAktiv = false;
103 handler.postDelayed(this, 500);
104 punkte = balken;
105 versuche++;
106 schreibeVersuche(versuche);
107 highscore += punkte;
108 schreibeHighscore(highscore);
109 Klick = 2;
110 refreshScreen();
111 }
112 else
113 {
114 if(Klick == 2)
115 {
116 Klick = 0;
117 punkte = 0;
118 balken = 0;
119 ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
120 proBar.setProgress(balken);
121 refreshScreen();
122 }
123 }
124 }
125 }
126
127 @Override
128 public void run()
129 {
130 if(balkenAktiv == true)
131 {
132 gewartet = false;
133 ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
134 if(abnehmend == false)
135 {
136 if(balken < 100)
137 {
138 balken+=5;
139 proBar.setProgress(balken);
140 refreshScreen();
141 handler.postDelayed(this, 0);
142 }
143 else if(balken == 100)
144 {
145 abnehmend = true;
146 proBar.setProgress(balken);
147 refreshScreen();
148 handler.postDelayed(this, 0);
149 }
150 }
151
152 else if(abnehmend == true)
153 {
154 if(balken > 0)
155 {
156 balken-=5;
157 proBar.setProgress(balken);
158 refreshScreen();
159 handler.postDelayed(this, 0);
160 }
161 else if (balken == 0)
162 {
163 abnehmend = false;
164 proBar.setProgress(balken);
165 refreshScreen();
166 handler.postDelayed(this, 0);
167 }
168 }
169 }
170 else if (balkenAktiv == false)
171 {
172 if(gewartet == true)
173 {
174 if(counter < 4)
175 {
176 counter++;
177 view.invalidate();
178 handler.postDelayed(this, 500);
179 }
180 else if (counter >= 4)
181 {
182 balkenAktiv = true;
183 counter = 0;
184 }
185 }
186 gewartet = true;
187 }
188 }
189
190 private void schreibeHighscore(int highscore)
191 {
192 SharedPreferences pref = getSharedPreferences("GAME", 0);
193 SharedPreferences.Editor editor = pref.edit();
194 editor.putInt("HIGHSCORE", highscore);
195 editor.commit();
196 }
197
198 private void schreibeVersuche(int versuche)
199 {
200 SharedPreferences pref = getSharedPreferences("GAME", 0);
201 SharedPreferences.Editor editor = pref.edit();
202 editor.putInt("VERSUCHE", versuche);
203 editor.commit();
204 }
205
206 private int leseVersuche()
207 {
208 SharedPreferences pref = getSharedPreferences("GAME", 0);
209 return pref.getInt("VERSUCHE", 0);
210 }
211
212 private int leseHighscore()
213 {
214 SharedPreferences pref = getSharedPreferences("GAME", 0);
215 return pref.getInt("HIGHSCORE", 0);
216 }
217}
Would be nice if you could help me out with this!
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.