Skip to content

Controls (Virtual Gamepad)

Dokunmatik cihazlarda kullanilmak uzere hazir joystick ve aksiyon butonlari sunar. Tamamen client-side calisir, dis bagimliligi yoktur.

Iki kullanim modu vardir:

  1. JS APIgameTegra.controls.* ile programatik kontrol
  2. Custom Elements<gametegra-joystick> ve <gametegra-buttons> HTML tag'leri

controls.show

Kontrolleri ekranda gosterir. Varsayilan olarak analog joystick (sol) + A/B butonlari (sag) olusturur.

js
// Varsayilan: analog joystick + A/B butonlari
gameTegra.controls.show()

// Ozel yapilandirma ile
gameTegra.controls.show({
  joystick: { type: 'dpad' },
  buttons: [
    { code: 'fire',  label: 'Fire',  icon: 'crosshair' },
    { code: 'jump',  label: 'Jump',  icon: 'arrow_up' },
  ],
  opacity: 0.8,
  theme: 'dark',
})

// Kendi container'ina render et
gameTegra.controls.show({
  target: 'game-controls',
})

Parametreler (ControlsConfig):

AlanTipZorunluVarsayilanAciklama
targetstringHayirnullHTML element ID — verilirse o elementin icine render eder, verilmezse fixed overlay
joystickboolean | objectHayirtruefalse=kapali, true=analog, { type: 'dpad' } gibi obje
buttonsarray | falseHayir[A, B]Buton listesi veya false (butonsuz)
opacitynumberHayir0.7Kontrol opakligi (0-1)
themestringHayir'dark''dark' veya 'light'

Joystick Config:

AlanTipZorunluVarsayilanAciklama
typestringHayir'analog''analog' veya 'dpad'
sizenumberHayir120Cap (px)
deadZonenumberHayir0.150-1 arasi — analog modda merkez hassasiyeti
diagonalsbooleanHayirtruefalse → sadece 4 yon (yukari/asagi/sol/sag)

Button Config:

AlanTipZorunluAciklama
codestringEvetEvent kodu (ornegin 'fire', 'jump')
labelstringHayirButon uzerinde gosterilecek metin
iconstringHayirBuilt-in ikon adi, raw SVG string veya null

Built-in Ikonlar: circle, cross, crosshair, arrow_up, lightning, stop, shield, sword


controls.on

Belirli bir kontrol kodunu dinler.

js
// Buton event'i dinle
const unsub = gameTegra.controls.on('fire', (e) => {
  console.log(e.type) // 'press' veya 'release'
})

// Joystick yon event'i dinle
gameTegra.controls.on('up', (e) => {
  console.log('Yukari yonde hareket')
})

// Joystick birakma
gameTegra.controls.on('idle', (e) => {
  console.log('Joystick serbest')
})

// Dinlemeyi birak
unsub()

Parametreler:

AlanTipZorunluAciklama
codestringEvetDinlenecek event kodu
handlerfunctionEvetCallback fonksiyonu

Donus Tipi: () => void — dinlemeyi durduran fonksiyon

Event Nesnesi:

AlanTipAciklama
codestringEvent kodu (ornegin 'fire', 'up', 'idle')
typestring'press', 'release' veya 'move'
timestampnumberMilisaniye cinsinden zaman damgasi

Joystick Yon Kodlari: up, down, left, right, up_left, up_right, down_left, down_right, idle


controls.onAny

Tum kontrol event'lerini tek bir handler ile dinler.

js
const unsub = gameTegra.controls.onAny((e) => {
  console.log(`${e.code}: ${e.type}`)
})

controls.hide

Kontrolleri ekrandan kaldirir ama event listener'lari korur. Tekrar show() ile gosterilebilir.

js
gameTegra.controls.hide()

controls.destroy

Kontrolleri tamamen yok eder — DOM, CSS ve tum listener'lar temizlenir.

js
gameTegra.controls.destroy()

Custom Elements

SDK yuklendiginde <gametegra-joystick> ve <gametegra-buttons> custom element'leri otomatik kayit edilir. HTML icinde dogrudan kullanilabilir.

html
<!-- Analog joystick (varsayilan) -->
<gametegra-joystick></gametegra-joystick>

<!-- D-Pad -->
<gametegra-joystick type="dpad"></gametegra-joystick>

<!-- Boyut ayari -->
<gametegra-joystick type="analog" size="150"></gametegra-joystick>

<!-- Ozel butonlar -->
<gametegra-buttons buttons='[{"code":"fire","label":"Fire","icon":"crosshair"},{"code":"jump","label":"Jump","icon":"arrow_up"}]'>
</gametegra-buttons>

Custom element'ler ayni event sistemini kullanir — gameTegra.controls.on() ile dinlenebilir.

Multi-Touch

Joystick ve butonlar ayni anda kullanilabilir. Her biri bagimsiz touch identifier takip eder.

Target Modu

target parametresi verildiginde kontroller fixed overlay yerine belirtilen HTML element'in icine render edilir. Bu sayede oyun alaninin belirli bir bolgesine yerlestirebilirsiniz.


Tam Ornek

js
// Kontrolleri goster
gameTegra.controls.show({
  joystick: { type: 'analog', deadZone: 0.2 },
  buttons: [
    { code: 'fire',  label: 'Fire',  icon: 'crosshair' },
    { code: 'jump',  label: 'Jump',  icon: 'arrow_up' },
    { code: 'shield', label: 'Shield', icon: 'shield' },
  ],
  theme: 'dark',
  opacity: 0.8,
})

// Yonleri dinle
gameTegra.controls.on('up', () => player.moveUp())
gameTegra.controls.on('down', () => player.moveDown())
gameTegra.controls.on('left', () => player.moveLeft())
gameTegra.controls.on('right', () => player.moveRight())
gameTegra.controls.on('idle', () => player.stop())

// Butonlari dinle
gameTegra.controls.on('fire', (e) => {
  if (e.type === 'press') player.startFiring()
  if (e.type === 'release') player.stopFiring()
})

gameTegra.controls.on('jump', (e) => {
  if (e.type === 'press') player.jump()
})

// Tum event'leri logla
gameTegra.controls.onAny((e) => {
  console.log(`[${e.code}] ${e.type}`)
})

// Oyun bittiginde temizle
gameTegra.controls.destroy()

D-Pad ile Basit Ornek

js
gameTegra.controls.show({
  joystick: { type: 'dpad' },
  buttons: [{ code: 'action', label: 'A', icon: 'circle' }],
})

gameTegra.controls.on('up',    () => move('up'))
gameTegra.controls.on('down',  () => move('down'))
gameTegra.controls.on('left',  () => move('left'))
gameTegra.controls.on('right', () => move('right'))
gameTegra.controls.on('action', (e) => {
  if (e.type === 'press') doAction()
})