Page 1 of 1

PlayN - One code, many platforms

PostPosted: March 9th, 2012, 8:39 am
by Suyo
https://developers.google.com/playn/

PlayN is a framework made by Google, and is based on Java. The special thing is that PlayN comes with tools to compile that Java code on 5 different platforms - Java, HTML5, Flash, and if you want to go mobile, Android and iOS. It also has a very simple base code - here's a bouncing ball for you:
Code: Select all
public class MyGame implements Game {
  static final float GRAVITY = 64;

  ImageLayer layer;

  float px, py;
  float x, y;
  float vx, vy;
  float ax, ay;

  public void init() {
    Image img = assetManager().getImage("ball.png");
    layer = graphics().createImageLayer(img);
    graphics().rootLayer().add(layer);

    x = graphics().width() / 2;
    y = graphics().height() / 2;
    ay = GRAVITY;
  }

  public void update(float delta) {
    // Save previous position for interpolation.
    px = x;
    py = y;

    // Update physics.
    delta /= 1000;
    vx += ax * delta;
    vy += ay * delta;
    x += vx * delta;
    y += vy * delta;
  }

  public void paint(float alpha) {
    // Interpolate current position.
    float x = (this.x * alpha) + (px * (1f - alpha));
    float y = (this.y * alpha) + (py * (1f - alpha));

    // Update the layer.
    layer.resetTransform();
    layer.translate(
      x - layer.image().width() / 2,
      y - layer.image().height() / 2
    );
  }

  public int updateRate() {
    // 0 = max framerate
    return 0;
  }
}


Sounds very interesting to me, and the base code seems simple enough. I'll try it, though I heard from some people the documentation sucks. Meh.

Re: PlayN - One code, many platforms

PostPosted: March 9th, 2012, 12:57 pm
by Jellonator
do want.