Game-Apps für Smartphones und Tablets

Bern University oh Teacher Education  
HomeOnline-Editor startenDruckenAndroid-TurtlegrafikJava-Online

Touch-Stick-Control

Der Touch-Stick funktioniert ähnlich wie ein Joy-Stick. Mit platzieren des Fingers legt man ein Bewegungszentrum fest. Zieht man den Finger in einer bestimmten Richtung, so gewegt sich der Ballon in dieser Richtung. Es kommt nicht darauf an, wie weit der Finger vom Bewegungszentrum gezogen wird, wesentlich ist nur die Richung. Ziel ist es, den Ballon so zu bewegen, dass er den fliegenden Pfeilen ausweicht.

Beispiel im Online-Editor bearbeiten

App installieren auf Smartphone oder Tablet

QR-Code

Sources downloaden (TouchStick.zip)


 

// TouchStick.java

package app.touchstick;

import android.graphics.Point;
import ch.aplu.android.*;

public class TouchStick extends GameGrid implements GGActorCollisionListener
{
  private Balloon balloon;

  public TouchStick()
  {
    super("town"windowZoom(500));
  }

  public void main()
  {
    setSimulationPeriod(30);
    balloon = new Balloon();
    addActor(balloon, new Location(100, 100));
    addTouchListener(balloon, GGTouch.drag | GGTouch.press | GGTouch.release);
    balloon.addActorCollisionListener(this);
    balloon.setCollisionCircle(new Point(0, 0), 20);
    for (int = 0; i < 50; i++)
    {
      Dart = new Dart();
      balloon.addCollisionActor(d);
      addActorNoRefresh(d, new Location(-10-10)); //out of sight
    }
    doRun();
  }

  public int collide(Actor actor1, Actor actor2)
  {
    actor1.show(1);
    playTone(1200, 20);
    refresh();
    doPause();
    return 0;
  }
}

class Dart extends Actor
{
  private int launchCountdown;
  private int numberPasses = 0;

  public Dart()
  {
    super("dart");
  }

  public void reset()
  {
    setCollisionSpot(new Point(40, 0));
    // at first in [50, 1050], gradually increasing, maxing in [50, 150]
    launchCountdown = (int) (Math.max(100, (10 - numberPasses) * 100) * Math.random() + 50);
    L.d("Lc " + launchCountdown + " np " + numberPasses);
    int = (int) (Math.random() * getNbVertCells());
    setLocation(new Location(-70, y));
  }

  public void act()
  {
    if (getX() < getNbHorzCells() + 60)
    {
      if (launchCountdown < 0)
        move();
      else
        launchCountdown--;
    }
    else
    {
      reset();
      numberPasses++;
    }
  }
}

class Balloon extends Actor implements GGTouchListener
{
  private Location initialLoc;
  private boolean moving = false;

  public Balloon()
  {
    super("balloon""explosion");
  }

  public void act()
  {
    if (isMoveValid() && moving)
      move();
  }

  public boolean touchEvent(GGTouch touch)
  {
    Location loc = new Location(touch.getX(), touch.getY());
    switch (touch.getEvent())
    {
      case GGTouch.press:
        initialLoc = loc;
        moving = true;
        break;
      case GGTouch.drag:
      {//if (initialLoc.getDistanceTo(loc) < 50) { 
        double dir = initialLoc.getDirectionTo(loc);
        setDirection(dir);
      }
      break;
      case GGTouch.release:
        moving = false;
        break;
    }
    return true;
  }
}


Erklärungen zum Programmcode:
Location loc = new Location(touch.getX(), touch.getY()) Legt das Bewegungszentrum fest
case GGTouch.press:
        initialLoc = loc;
        moving = true;
Bewegung wird mit dem Tap gestartet
case GGTouch.drag:
        double dir = initialLoc.getDirectionTo(loc);
        setDirection(dir);
Beim ziehen der Fingers wird die Bewegungsrichtung registriert
case GGTouch.release:
        moving = false;
Beim Heben des Fingers wird die Bewegung gesoppt
int y = (int) (Math.random() * getNbVertCells());
setLocation(new Location(-70, y));
Die y-Koordinate der Schwerte wird zufällig auf die ganze Höhe des Fensters verteilt