Note that there are some explanatory texts on larger screens.

plurals
  1. POinvalidate() not resulting in a onDraw() call
    primarykey
    data
    text
    <p>I'm working on a simple game engine and am having a view invalidate() issue. Basically, when I try to invalidate() the view from any where but default Activity methods if fails to call onDraw(). Any ideas?</p> <pre><code>public class GameActivity extends Activity { //Name for my app String sTag = "CloneOut"; //Variable to point to my view View myView; //Variable controlling the running of the game boolean bRunning = true; //Fixed framerate variables long desiredFramerate = 1; long targetTimeMillis; //Creates a new MusicManager object to handle all music MusicManager mp = new MusicManager(this); //Creates a new SoundEffectManager object to handle all sound effects SoundEffectManager sp = new SoundEffectManager(this); //Create new int variables to hold SoundEffectManager sound ids //int iBeer; //Create an array of bitmaps to store all the actors in the game Bitmap[] actor = new Bitmap[10]; //Automatically called when an activity begins @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Critical line that sets teh view of this activity to BitmapView and // thus BitmapView.draw is called to repaint the view setContentView(new BitmapView(this)); // Show the Up button in the action bar. setupActionBar(); myView = findViewById(android.R.id.content); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { //Action bar only exists in API 11+ so check before displaying if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.game, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } class BitmapView extends View { public BitmapView(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { //Writes an informational message to the LogCat window Log.i(sTag,"Drawing"); //Draw the background first canvas.drawColor(Color.BLACK); //Draw all the actors in the game canvas.drawBitmap(actor[0], 500, 100, null); } } //----------------------------------------------------------------------------------------- //Game Engine Code Start //----------------------------------------------------------------------------------------- //The following methods run automatically in this order // onCreate() - Call immediately on starting an activity // onStart() // onResume() - Called when the activity starts interacting with the user public void onResume() { super.onResume(); //Populate the first actor with the paddle actor[0] = BitmapFactory.decodeResource(getResources(), R.drawable.paddle); //Calculate the time at which to render the next frame targetTimeMillis = System.currentTimeMillis() + (1000 / desiredFramerate); //Start the game running GameLoop(); } public void GameLoop() { while (bRunning) { CheckFramerate(); } } public void CheckFramerate() { if (System.currentTimeMillis() &gt; targetTimeMillis) { //Calculate the time at which to render the next frame targetTimeMillis = System.currentTimeMillis() + (1000L / desiredFramerate); //Writes an informational message to the LogCat window Log.i(sTag,"Renderframe"); RenderFrame(); } } //Invalidates the entire view and thus forces a redraw of the screen public void RenderFrame() { //Writes an informational message to the LogCat window Log.i(sTag,"Inside Renderframe"); myView.invalidate(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload