SlideShare a Scribd company logo
LibGDX:	
  Internaliza1on	
  and	
  
Scene	
  2D	
  
Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
INTERNALIZATION	
  
Support	
  for	
  Mul1ple	
  Languages	
  
•  Create	
  defaults	
  proper1es	
  file:	
  
– MyBundle.properties
•  Create	
  other	
  language	
  files:	
  
– MyBundle.fi_FI.properties
MyBundle.proper1es	
  
title=My Game
score=You score {0}
MyBundle_fi.proper1es	
  
title=Pelini
score=Pisteesi {0}
In	
  Java	
  
Locale locale = new Locale("fi");
I18NBundle myBundle =
I18NBundle.createBundle(Gdx.files.internal("MyBundle"), locale);
String title = myBundle.get("title");
String score = myBundle.get("score", 50);
Notes	
  
•  If	
  you	
  don't	
  specify	
  locale,	
  default	
  locale	
  is	
  
used	
  
•  Charset	
  is	
  UTF-­‐8	
  (without	
  BOM)	
  
•  See:	
  	
  
– hVps://github.com/libgdx/libgdx/wiki/
Interna1onaliza1on-­‐and-­‐Localiza1on	
  
	
  
SCENE2D	
  
Scene2D?	
  
•  It’s	
  op1onal,	
  you	
  don’t	
  need	
  it.	
  But	
  you	
  may	
  
want	
  to	
  use	
  it.	
  
•  May	
  be	
  useful	
  for	
  "board	
  games"	
  
•  Higher	
  level	
  framework	
  for	
  crea>ng	
  games	
  
•  Provides	
  UI	
  Toolkit	
  also:	
  Scene2d.ui	
  
•  Tutorial	
  
–  https://github.com/libgdx/libgdx/wiki/Scene2d
Concepts	
  
•  Stage	
  
– “Screens”,	
  “Stages”,	
  “Levels”	
  
– Camera	
  watching	
  the	
  stage	
  
– Contains	
  group	
  of	
  actors	
  
•  Actors	
  
– “Sprites”	
  
	
  
	
  
Roughly	
  the	
  Idea	
  in	
  Code	
  
Stage gameStage = new Stage();
// PlayerActor extends Actor { .. }
PlayerActor player = new PlayerActor();
// Let's add the actor to the stage
gameStage.addActor(player);
Possibili1es	
  
•  Event	
  system	
  for	
  actors;	
  when	
  actor	
  is	
  
touched,	
  dragged	
  ..	
  
– Hit	
  detec>on;	
  dragging	
  /	
  touching	
  within	
  the	
  
bounds	
  of	
  actor	
  
•  Ac>on	
  system:	
  rotate,	
  move,	
  scale	
  actors	
  in	
  
parallel	
  
– Also	
  rota1on	
  and	
  scaling	
  of	
  group	
  of	
  actors	
  
Stage	
  
•  Stage	
  implements	
  InputProcessor,	
  so	
  it	
  can	
  
directly	
  input	
  events	
  from	
  keyboard	
  and	
  touch	
  
•  Add	
  to	
  your	
  Applica1onAdapter	
  
–  Gdx.input.setInputProcessor(myStage);
•  Stage	
  distributes	
  input	
  to	
  actors	
  
•  Use	
  setViewport	
  to	
  set	
  the	
  camera	
  
–  myStage.setViewPort(...);	
  
•  Stage	
  has	
  act	
  method,	
  by	
  calling	
  this,	
  every	
  act	
  
method	
  of	
  every	
  actor	
  is	
  called	
  
Actors	
  
•  Actor	
  has	
  an	
  posi1on,	
  rect	
  size,	
  scale,	
  
rota1on…	
  
•  Posi1on	
  is	
  the	
  leb	
  corner	
  of	
  the	
  actor	
  
•  Posi1on	
  is	
  rela1ve	
  to	
  the	
  actor’s	
  parent	
  
•  Actor	
  may	
  have	
  ac>ons	
  
– Change	
  the	
  presenta>on	
  of	
  the	
  actor	
  (move,	
  
resize)	
  
•  Actor	
  can	
  react	
  to	
  events	
  
public class StageGame extends ApplicationAdapter {
// Stage contains hierarcy of actors
private Stage stage;
// We will have player actor in the stage
private BlueBirdActor playerActor;
@Override
public void create () {
// Creating the stage
stage = new Stage();
// Creating the actors
playerActor = new BlueBirdActor();
// add actors to stage
stage.addActor(playerActor);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Call act on every actor
stage.act(Gdx.graphics.getDeltaTime());
// Call draw on every actor
stage.draw();
}
}
public class BlueBirdActor extends Actor {
private Texture texture;
public BlueBirdActor() {
texture = new Texture(Gdx.files.internal("blue-bird-icon.png"));
}
@Override
public void draw(Batch batch, float alpha) {
batch.draw(texture, getX(), getY());
}
@Override
public void act(float delta) {
super.act(delta);
}
}
Event	
  System	
  
•  Stage	
  will	
  be	
  responsible	
  for	
  gedng	
  user	
  input	
  
–  Gdx.input.setInputProcessor(stage);
•  Stage	
  will	
  fire	
  events	
  to	
  actors	
  
•  Actor	
  may	
  receive	
  events	
  if	
  it	
  has	
  a	
  listener	
  
–  actor.addListener(new InputListener()
{ … } );
•  Actor	
  must	
  specify	
  bounds	
  in	
  order	
  to	
  receive	
  
input	
  events	
  within	
  those	
  bounds!	
  
•  To	
  handle	
  key	
  input,	
  actor	
  has	
  to	
  have	
  keyboard	
  
focus	
  
public class StageGame extends ApplicationAdapter {
// Stage contains hierarcy of actors
private Stage stage;
// We will have player actor in the stage
private BlueBirdActor playerActor;
@Override
public void create () {
// Creating the stage
stage = new Stage();
// Sets the InputProcessor that will receive all touch and key input events.
// It will be called before the ApplicationListener.render() method each frame.
//
// Stage handles the calling the inputlisteners for each actor.
Gdx.input.setInputProcessor(stage);
// Creating the actors
playerActor = new BlueBirdActor();
// add actors to stage
stage.addActor(playerActor);
stage.setKeyboardFocus(playerActor);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Call act on every actor
stage.act(Gdx.graphics.getDeltaTime());
// Call draw on every actor
stage.draw();
}
}
public class BlueBirdActor extends Actor {
private boolean up, down, left, right
public BlueBirdActor() {
addListener(new PlayerListener());
}
@Override
public void act(float delta) {
if(up) {
setY(getY() + speed * delta);
}
}
// InputListener implements EventListener, just override the methods you need
// Also ActorGestureListener available: fling, pan, zoom, pinch..
class PlayerListener extends InputListener {
@Override
public boolean keyDown(InputEvent event, int keycode) {
if(keycode == Input.Keys.UP) {
up = true;
}
return true;
}
@Override
public boolean keyUp(InputEvent event, int keycode) {
if(keycode == Input.Keys.UP) {
up = false;
}
return true;
}
}
}
Ac1ons	
  
•  Each	
  actor	
  has	
  a	
  list	
  of	
  ac1ons	
  
•  Updated	
  on	
  every	
  frame	
  (act-­‐method)	
  
•  Many	
  ac1ons	
  available	
  
–  MoveToAction
–  RotateToAction
–  ScaleToAction
•  Example	
  
–  MoveToAction action = new MoveToAction();
–  action.setPosition(300f, 700f);
–  action.setDuration(2f);
–  actor.addAction(action);
Grouping	
  Ac1ons	
  in	
  Sequence	
  
SequenceAction sequenceAction = new SequenceAction();
MoveToAction moveAction = new MoveToAction();
RotateToAction rotateAction = new RotateToAction();
ScaleToAction scaleAction = new ScaleToAction();
moveAction.setPosition(200f, 400f);
moveAction.setDuration(1f);
rotateAction.setRotation(rotate);
rotateAction.setDuration(1f);
scaleAction.setScale(0.5f);
scaleAction.setDuration(1f);
sequenceAction.addAction(moveAction);
sequenceAction.addAction(rotateAction);
sequenceAction.addAction(scaleAction);
actor.addAction(sequenceAction);
Grouping	
  Ac1ons	
  in	
  Parallel	
  
ParallelAction parallel = new ParallelAction ();
MoveToAction moveAction = new MoveToAction();
RotateToAction rotateAction = new RotateToAction();
ScaleToAction scaleAction = new ScaleToAction();
moveAction.setPosition(200f, 400f);
moveAction.setDuration(1f);
rotateAction.setRotation(rotate);
rotateAction.setDuration(1f);
scaleAction.setScale(0.5f);
scaleAction.setDuration(1f);
parallel.addAction(moveAction);
parallel.addAction(rotateAction);
parallel.addAction(scaleAction);
actor.addAction(parallel);
Ac1ons	
  Complete?	
  
SequenceAction sequenceAction = new SequenceAction();
ParallelAction parallelAction = new ParallelAction();
MoveToAction moveAction = new MoveToAction();
RotateToAction rotateAction = new RotateToAction();
RunnableAction runnableAction = new RunnableAction();
moveAction.setPosition(200f, 400f);
moveAction.setDuration(1f);
moveAction.setInterpolation(Interpolation.bounceOut);
rotateAction.setRotation(rotate);
rotateAction.setDuration(1f);
runnableAction.setRunnable(new Runnable() {
public void run() {
System.out.println("done!");
}
});
parallelAction.addAction(rotateAction);
parallelAction.addAction(moveAction);
sequenceAction.addAction(parallelAction);
sequenceAction.addAction(runnableAction);
Enable	
  rotate	
  and	
  scale	
  in	
  drawing	
  
@Override
public void draw(Batch batch, float alpha){
batch.draw(texture,
this.getX(), this.getY(),
this.getOriginX(),
this.getOriginY(),
this.getWidth(),
this.getHeight(),
this.getScaleX(),
this.getScaleY(),
this.getRotation(),0,0,
texture.getWidth(), texture.getHeight(), false, false);
}
Grouping	
  
Group group = new Group();
group.addActor(playerActor);
group.addActor(monsterActor);
group.addAction( ... );
stage.addActor(group);
CREATING	
  UI:	
  SCENE2D.UI	
  
	
  
Scene2D.UI	
  
•  Tutorial	
  
– hVps://github.com/libgdx/libgdx/wiki/Scene2d.ui	
  
•  To	
  quickly	
  get	
  started,	
  add	
  following	
  to	
  your	
  
project	
  
– uiskin.png,	
  uiskin.atlas,	
  uiskin.json,	
  default.fnt	
  
– hVps://github.com/libgdx/libgdx/tree/master/
tests/gdx-­‐tests-­‐android/assets/data	
  
•  Tutorial	
  and	
  examples	
  
– hVps://github.com/libgdx/libgdx/wiki/Scene2d.ui	
  
TextBuVon	
  example	
  
Skin skin = new Skin( Gdx.files.internal("uiskin.json") );
final TextButton button = new TextButton("Hello", skin);
button.setWidth(200f);
button.setHeight(20f);
button.setPosition(Gdx.graphics.getWidth() /2 - 100f, Gdx.graphics.getHeight()/2
- 10f);
startStage.addActor(button);
Gdx.input.setInputProcessor(startStage);
button.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
whichScreen = !whichScreen;
Gdx.input.setInputProcessor(gameStage);
}
});

More Related Content

PDF
Box2D and libGDX
PDF
Implementing a Simple Game using libGDX
PDF
libGDX: User Input
PDF
libGDX: User Input and Frame by Frame Animation
PDF
libGDX: Tiled Maps
PDF
A split screen-viable UI event system - Unite Copenhagen 2019
PDF
Technical Deep Dive into the New Prefab System
PPTX
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Box2D and libGDX
Implementing a Simple Game using libGDX
libGDX: User Input
libGDX: User Input and Frame by Frame Animation
libGDX: Tiled Maps
A split screen-viable UI event system - Unite Copenhagen 2019
Technical Deep Dive into the New Prefab System
Cross-scene references: A shock to the system - Unite Copenhagen 2019

What's hot (20)

PPTX
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
PDF
Converting Scene Data to DOTS – Unite Copenhagen 2019
PPTX
Soc research
PPTX
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
PDF
Custom SRP and graphics workflows - Unite Copenhagen 2019
PDF
Sequence diagrams
PDF
HoloLens Programming Tutorial: AirTap & Spatial Mapping
PDF
Unity遊戲程式設計 - 2D Platformer遊戲
PPTX
Unity3 d devfest-2014
PDF
Creating Games for Asha - platform
PPTX
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
PDF
Game Development using SDL and the PDK
PDF
Using Android Things to Detect & Exterminate Reptilians
PDF
Game Development with SDL and Perl
PDF
Android dev toolbox - Shem Magnezi, WeWork
PPTX
Scene Graphs & Component Based Game Engines
PDF
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
PPT
iOS Training Session-3
PPTX
Code Pad
ODP
SDL2 Game Development VT Code Camp 2013
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019
Soc research
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
Sequence diagrams
HoloLens Programming Tutorial: AirTap & Spatial Mapping
Unity遊戲程式設計 - 2D Platformer遊戲
Unity3 d devfest-2014
Creating Games for Asha - platform
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
Game Development using SDL and the PDK
Using Android Things to Detect & Exterminate Reptilians
Game Development with SDL and Perl
Android dev toolbox - Shem Magnezi, WeWork
Scene Graphs & Component Based Game Engines
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
iOS Training Session-3
Code Pad
SDL2 Game Development VT Code Camp 2013
Ad

Viewers also liked (9)

PDF
Building Android games using LibGDX
PDF
Intro to Building Android Games using libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
Java Web Services
PDF
Moved to Speakerdeck
PDF
iOS Selectors Blocks and Delegation
PDF
Lib gdx 2015_corkdevio
PDF
Building games-with-libgdx
PDF
LibGDX: Cross Platform Game Development
Building Android games using LibGDX
Intro to Building Android Games using libGDX
libGDX: Screens, Fonts and Preferences
Java Web Services
Moved to Speakerdeck
iOS Selectors Blocks and Delegation
Lib gdx 2015_corkdevio
Building games-with-libgdx
LibGDX: Cross Platform Game Development
Ad

Similar to libGDX: Scene2D (20)

PPT
MIDP: Game API
PPTX
Game Development for Nokia Asha Devices with Java ME #2
PDF
Game Programming I - Introduction
PPTX
Game development with_lib_gdx
PPTX
C game programming - SDL
KEY
ARTDM 170, Week 7: Scripting Interactivity
KEY
Java Fx Tutorial01
PDF
Games 3 dl4-example
PDF
Game Salad Study
PDF
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
PPTX
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...
PPTX
Chapt 3 role of actors
PDF
Developing games for Series 40 full-touch UI
PDF
The Ring programming language version 1.10 book - Part 132 of 212
PPTX
Scratch Tom and Jerry
PPTX
Chapter 03 game input
PDF
Matching Game In Java
PDF
Java Fx Ajaxworld Rags V1
PPTX
Silverlight as a Gaming Platform
PDF
Hacking JavaFX with Groovy, Clojure, Scala, and Visage: Stephen Chin
MIDP: Game API
Game Development for Nokia Asha Devices with Java ME #2
Game Programming I - Introduction
Game development with_lib_gdx
C game programming - SDL
ARTDM 170, Week 7: Scripting Interactivity
Java Fx Tutorial01
Games 3 dl4-example
Game Salad Study
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...
Chapt 3 role of actors
Developing games for Series 40 full-touch UI
The Ring programming language version 1.10 book - Part 132 of 212
Scratch Tom and Jerry
Chapter 03 game input
Matching Game In Java
Java Fx Ajaxworld Rags V1
Silverlight as a Gaming Platform
Hacking JavaFX with Groovy, Clojure, Scala, and Visage: Stephen Chin

More from Jussi Pohjolainen (19)

PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
PDF
Intro to PhoneGap
PDF
Quick Intro to JQuery and JQuery Mobile
PDF
JavaScript Inheritance
PDF
JS OO and Closures
PDF
Short intro to ECMAScript
PDF
Building Web Services
PDF
Extensible Stylesheet Language
PDF
About Http Connection
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Intro to Asha UI
Intro to Java ME and Asha Platform
Intro to PhoneGap
Quick Intro to JQuery and JQuery Mobile
JavaScript Inheritance
JS OO and Closures
Short intro to ECMAScript
Building Web Services
Extensible Stylesheet Language
About Http Connection

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Encapsulation theory and applications.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
A Presentation on Artificial Intelligence
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Getting Started with Data Integration: FME Form 101
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
1. Introduction to Computer Programming.pptx
Machine Learning_overview_presentation.pptx
Heart disease approach using modified random forest and particle swarm optimi...
Mushroom cultivation and it's methods.pdf
Encapsulation theory and applications.pdf
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
A comparative study of natural language inference in Swahili using monolingua...
A Presentation on Artificial Intelligence
OMC Textile Division Presentation 2021.pptx
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Empathic Computing: Creating Shared Understanding
Getting Started with Data Integration: FME Form 101

libGDX: Scene2D

  • 1. LibGDX:  Internaliza1on  and   Scene  2D   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 3. Support  for  Mul1ple  Languages   •  Create  defaults  proper1es  file:   – MyBundle.properties •  Create  other  language  files:   – MyBundle.fi_FI.properties
  • 6. In  Java   Locale locale = new Locale("fi"); I18NBundle myBundle = I18NBundle.createBundle(Gdx.files.internal("MyBundle"), locale); String title = myBundle.get("title"); String score = myBundle.get("score", 50);
  • 7. Notes   •  If  you  don't  specify  locale,  default  locale  is   used   •  Charset  is  UTF-­‐8  (without  BOM)   •  See:     – hVps://github.com/libgdx/libgdx/wiki/ Interna1onaliza1on-­‐and-­‐Localiza1on    
  • 9. Scene2D?   •  It’s  op1onal,  you  don’t  need  it.  But  you  may   want  to  use  it.   •  May  be  useful  for  "board  games"   •  Higher  level  framework  for  crea>ng  games   •  Provides  UI  Toolkit  also:  Scene2d.ui   •  Tutorial   –  https://github.com/libgdx/libgdx/wiki/Scene2d
  • 10. Concepts   •  Stage   – “Screens”,  “Stages”,  “Levels”   – Camera  watching  the  stage   – Contains  group  of  actors   •  Actors   – “Sprites”      
  • 11. Roughly  the  Idea  in  Code   Stage gameStage = new Stage(); // PlayerActor extends Actor { .. } PlayerActor player = new PlayerActor(); // Let's add the actor to the stage gameStage.addActor(player);
  • 12. Possibili1es   •  Event  system  for  actors;  when  actor  is   touched,  dragged  ..   – Hit  detec>on;  dragging  /  touching  within  the   bounds  of  actor   •  Ac>on  system:  rotate,  move,  scale  actors  in   parallel   – Also  rota1on  and  scaling  of  group  of  actors  
  • 13. Stage   •  Stage  implements  InputProcessor,  so  it  can   directly  input  events  from  keyboard  and  touch   •  Add  to  your  Applica1onAdapter   –  Gdx.input.setInputProcessor(myStage); •  Stage  distributes  input  to  actors   •  Use  setViewport  to  set  the  camera   –  myStage.setViewPort(...);   •  Stage  has  act  method,  by  calling  this,  every  act   method  of  every  actor  is  called  
  • 14. Actors   •  Actor  has  an  posi1on,  rect  size,  scale,   rota1on…   •  Posi1on  is  the  leb  corner  of  the  actor   •  Posi1on  is  rela1ve  to  the  actor’s  parent   •  Actor  may  have  ac>ons   – Change  the  presenta>on  of  the  actor  (move,   resize)   •  Actor  can  react  to  events  
  • 15. public class StageGame extends ApplicationAdapter { // Stage contains hierarcy of actors private Stage stage; // We will have player actor in the stage private BlueBirdActor playerActor; @Override public void create () { // Creating the stage stage = new Stage(); // Creating the actors playerActor = new BlueBirdActor(); // add actors to stage stage.addActor(playerActor); } @Override public void render () { Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Call act on every actor stage.act(Gdx.graphics.getDeltaTime()); // Call draw on every actor stage.draw(); } }
  • 16. public class BlueBirdActor extends Actor { private Texture texture; public BlueBirdActor() { texture = new Texture(Gdx.files.internal("blue-bird-icon.png")); } @Override public void draw(Batch batch, float alpha) { batch.draw(texture, getX(), getY()); } @Override public void act(float delta) { super.act(delta); } }
  • 17. Event  System   •  Stage  will  be  responsible  for  gedng  user  input   –  Gdx.input.setInputProcessor(stage); •  Stage  will  fire  events  to  actors   •  Actor  may  receive  events  if  it  has  a  listener   –  actor.addListener(new InputListener() { … } ); •  Actor  must  specify  bounds  in  order  to  receive   input  events  within  those  bounds!   •  To  handle  key  input,  actor  has  to  have  keyboard   focus  
  • 18. public class StageGame extends ApplicationAdapter { // Stage contains hierarcy of actors private Stage stage; // We will have player actor in the stage private BlueBirdActor playerActor; @Override public void create () { // Creating the stage stage = new Stage(); // Sets the InputProcessor that will receive all touch and key input events. // It will be called before the ApplicationListener.render() method each frame. // // Stage handles the calling the inputlisteners for each actor. Gdx.input.setInputProcessor(stage); // Creating the actors playerActor = new BlueBirdActor(); // add actors to stage stage.addActor(playerActor); stage.setKeyboardFocus(playerActor); } @Override public void render () { Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Call act on every actor stage.act(Gdx.graphics.getDeltaTime()); // Call draw on every actor stage.draw(); } }
  • 19. public class BlueBirdActor extends Actor { private boolean up, down, left, right public BlueBirdActor() { addListener(new PlayerListener()); } @Override public void act(float delta) { if(up) { setY(getY() + speed * delta); } } // InputListener implements EventListener, just override the methods you need // Also ActorGestureListener available: fling, pan, zoom, pinch.. class PlayerListener extends InputListener { @Override public boolean keyDown(InputEvent event, int keycode) { if(keycode == Input.Keys.UP) { up = true; } return true; } @Override public boolean keyUp(InputEvent event, int keycode) { if(keycode == Input.Keys.UP) { up = false; } return true; } } }
  • 20. Ac1ons   •  Each  actor  has  a  list  of  ac1ons   •  Updated  on  every  frame  (act-­‐method)   •  Many  ac1ons  available   –  MoveToAction –  RotateToAction –  ScaleToAction •  Example   –  MoveToAction action = new MoveToAction(); –  action.setPosition(300f, 700f); –  action.setDuration(2f); –  actor.addAction(action);
  • 21. Grouping  Ac1ons  in  Sequence   SequenceAction sequenceAction = new SequenceAction(); MoveToAction moveAction = new MoveToAction(); RotateToAction rotateAction = new RotateToAction(); ScaleToAction scaleAction = new ScaleToAction(); moveAction.setPosition(200f, 400f); moveAction.setDuration(1f); rotateAction.setRotation(rotate); rotateAction.setDuration(1f); scaleAction.setScale(0.5f); scaleAction.setDuration(1f); sequenceAction.addAction(moveAction); sequenceAction.addAction(rotateAction); sequenceAction.addAction(scaleAction); actor.addAction(sequenceAction);
  • 22. Grouping  Ac1ons  in  Parallel   ParallelAction parallel = new ParallelAction (); MoveToAction moveAction = new MoveToAction(); RotateToAction rotateAction = new RotateToAction(); ScaleToAction scaleAction = new ScaleToAction(); moveAction.setPosition(200f, 400f); moveAction.setDuration(1f); rotateAction.setRotation(rotate); rotateAction.setDuration(1f); scaleAction.setScale(0.5f); scaleAction.setDuration(1f); parallel.addAction(moveAction); parallel.addAction(rotateAction); parallel.addAction(scaleAction); actor.addAction(parallel);
  • 23. Ac1ons  Complete?   SequenceAction sequenceAction = new SequenceAction(); ParallelAction parallelAction = new ParallelAction(); MoveToAction moveAction = new MoveToAction(); RotateToAction rotateAction = new RotateToAction(); RunnableAction runnableAction = new RunnableAction(); moveAction.setPosition(200f, 400f); moveAction.setDuration(1f); moveAction.setInterpolation(Interpolation.bounceOut); rotateAction.setRotation(rotate); rotateAction.setDuration(1f); runnableAction.setRunnable(new Runnable() { public void run() { System.out.println("done!"); } }); parallelAction.addAction(rotateAction); parallelAction.addAction(moveAction); sequenceAction.addAction(parallelAction); sequenceAction.addAction(runnableAction);
  • 24. Enable  rotate  and  scale  in  drawing   @Override public void draw(Batch batch, float alpha){ batch.draw(texture, this.getX(), this.getY(), this.getOriginX(), this.getOriginY(), this.getWidth(), this.getHeight(), this.getScaleX(), this.getScaleY(), this.getRotation(),0,0, texture.getWidth(), texture.getHeight(), false, false); }
  • 25. Grouping   Group group = new Group(); group.addActor(playerActor); group.addActor(monsterActor); group.addAction( ... ); stage.addActor(group);
  • 27. Scene2D.UI   •  Tutorial   – hVps://github.com/libgdx/libgdx/wiki/Scene2d.ui   •  To  quickly  get  started,  add  following  to  your   project   – uiskin.png,  uiskin.atlas,  uiskin.json,  default.fnt   – hVps://github.com/libgdx/libgdx/tree/master/ tests/gdx-­‐tests-­‐android/assets/data   •  Tutorial  and  examples   – hVps://github.com/libgdx/libgdx/wiki/Scene2d.ui  
  • 28. TextBuVon  example   Skin skin = new Skin( Gdx.files.internal("uiskin.json") ); final TextButton button = new TextButton("Hello", skin); button.setWidth(200f); button.setHeight(20f); button.setPosition(Gdx.graphics.getWidth() /2 - 100f, Gdx.graphics.getHeight()/2 - 10f); startStage.addActor(button); Gdx.input.setInputProcessor(startStage); button.addListener(new ClickListener(){ @Override public void clicked(InputEvent event, float x, float y){ whichScreen = !whichScreen; Gdx.input.setInputProcessor(gameStage); } });