Input

Buttons, Analog-Sticks und Touch-Screen — Eingabeverarbeitung auf der Nintendo Switch.

Buttons

Drei Zustände: IsDown (einmalig beim Drücken), IsHeld (solange gehalten), IsUp (einmalig beim Loslassen).

public override void OnFrame()
{
    if (Input.IsDown(NpadButton.A))   { /* einmalig beim Drücken  */ }
    if (Input.IsHeld(NpadButton.ZR))  { /* solange gehalten       */ }
    if (Input.IsUp(NpadButton.B))     { /* einmalig beim Loslassen */ }

    if (Input.IsDown(NpadButton.Plus)) Environment.Exit(0);
}

Verfügbare Buttons

NpadButton.A
NpadButton.B
NpadButton.X
NpadButton.Y
NpadButton.L
NpadButton.R
NpadButton.ZL
NpadButton.ZR
NpadButton.Plus
NpadButton.Minus
NpadButton.Up
NpadButton.Down
NpadButton.Left
NpadButton.Right
NpadButton.StickL
NpadButton.StickR
NpadButton.StickLUp
NpadButton.StickLDown
NpadButton.StickLLeft
NpadButton.StickLRight
NpadButton.StickRUp
NpadButton.StickRDown
NpadButton.StickRLeft
NpadButton.StickRRight

Analog-Sticks

Deadzone ±3000 wird automatisch herausgefiltert. X: negativ = links, positiv = rechts. Y: positiv = oben, negativ = unten.

StickPos left  = Input.GetStickLeft();
StickPos right = Input.GetStickRight();

// Roher Wert: -32767 .. +32767
if (left.x > 5000)  _px += 5;
if (left.x < -5000) _px -= 5;
if (left.y > 5000)  _py -= 5;
if (left.y < -5000) _py += 5;
FeldTypBeschreibung
xint-32767 (links) bis +32767 (rechts)
yint+32767 (oben) bis -32767 (unten)

Touch-Screen

TouchState touch = Input.GetTouch();

if (touch.count > 0)
{
    int x = touch.x[0];
    int y = touch.y[0];
    Graphics.FillCircle(x, y, 20, Color.Red);
}

// Multi-Touch (bis zu 10 Punkte)
for (int i = 0; i < touch.count && i < 10; i++)
    Graphics.FillCircle(touch.x[i], touch.y[i], 15, Color.Green);
FeldTypBeschreibung
countintAnzahl aktiver Touch-Punkte (max. 10)
x[i]intX-Koordinate (0–1280)
y[i]intY-Koordinate (0–720)
id[i]uintFinger-ID für Multi-Touch-Tracking

Vollständiges Beispiel

public class MyApp : SwitchApp
{
    private int _px = 640, _py = 360;

    public override void OnInit()
    {
        Graphics.Init(1280, 720);
    }

    public override void OnFrame()
    {
        StickPos stick = Input.GetStickLeft();

        if (stick.x != 0) _px += stick.x / 6000;
        if (stick.y != 0) _py -= stick.y / 6000;

        _px = Math.Clamp(_px, 0, 1280);
        _py = Math.Clamp(_py, 0, 720);

        if (Input.IsDown(NpadButton.Plus)) Environment.Exit(0);

        Graphics.FillScreen(Color.Black);
        Graphics.FillCircle(_px, _py, 16, Color.Cyan);

        // Touch-Position anzeigen
        TouchState t = Input.GetTouch();
        if (t.count > 0)
            Graphics.FillCircle(t.x[0], t.y[0], 10, Color.Yellow);
    }
}