Skip to main content

Starting and Stopping a Game — Learn the difference between basla, duraklat, devam and durdur. See why starting again with the same config does not create another loop.

Game engine API · Lesson 2

Starting and Stopping a Game

Learn the difference between basla, duraklat, devam and durdur. See why starting again with the same config does not create another loop.

Four calls manage the game lifecycle. Three always return {ok:true}; basla() returns an error for an invalid mode or a module that is not loaded.

What you will learn

  • The basla(mod, config) contract
  • How duraklat() differs from devam()
  • Rebuilding after durdur()

API signatures

CallResult
basla(mod, config){ok, hata?} · validates mode and config
duraklat(){ok:true} · state is kept
devam(){ok:true} · loop resumes
durdur(){ok:true} · session closes
olay(ad, fn)function that cancels the subscription

What basla() does

The engine resolves the mode through MOD_ADLARI, finds the registered controller and calls its kur(kok, config, api) method. The config key is built from the mode name and JSON.stringify(config).

A second call with the same mode, same config and calisiyor=true returns early. Pressing a button twice therefore cannot create two game loops.

Safe start and restart

var M = window.OyunMotoru;
var cfg = {
  kok: document.getElementById('oyunAlani2D'),
  oyun: 'yakalama',
  genislik: 480,
  yukseklik: 320
};
M.basla('2d', cfg);  // set up
M.basla('2d', cfg);  // same config: skipped
M.duraklat();         // loop stops, state is kept
M.devam();            // loop resumes
M.durdur();           // session closes, key resets
M.basla('2d', cfg);  // set up again

What happens in this code? durdur() calls the active controller durdur(), clears the config key and sets calisiyor to false. The next basla() therefore rebuilds everything.

Lifecycle events

Every transition publishes its own event: basla, duraklat, devam, durdur. The wildcard * subscription receives all of them with ad and detay fields.

Watch every transition

var M = window.OyunMotoru;
var kapat = M.olay('*', function (e) {
  console.log('Event:', e.ad, e.detay);
});
M.basla('2d', { kok: document.getElementById('oyunAlani2D'), oyun: 'labirent' });
M.duraklat();
M.devam();
kapat();

What happens in this code? olay() returns a function that cancels the subscription. The same callback is never added twice for one event, and kapat() removes it from the list.

Common mistakes

Wrong approachResultCorrect approach
Treating duraklat() like durdur()The state is reset unnecessarilyduraklat keeps it, durdur closes it
Calling devam() while stoppedNothing happens, but it is safeThe engine returns {ok:true}
Clicking one button twiceFear of a duplicate loopThe same config is idempotent
Never cancelling a subscriptionListeners accumulateCall the value returned by olay()

Try it yourself

Call basla() from one button and durdur() from another. With the wildcard subscription, confirm that the basla and durdur events arrive in order.