SlideShare a Scribd company logo
libGDX:	
  User	
  Input	
  and	
  Frame	
  by	
  
Frame	
  Anima8on	
  
Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
TOUCH	
  +	
  KEYBOARD	
  
Event	
  vs	
  Polling	
  
•  Polling	
  
–  Do	
  something	
  on	
  each	
  frame,	
  really	
  fast	
  
•  Was	
  mouse	
  clicked?	
  Was	
  mouse	
  clicked?	
  Was	
  mouse	
  
clicked?	
  
–  Good	
  for	
  arcade	
  games	
  
•  Event	
  
–  Do	
  something	
  when	
  event	
  handles	
  
•  No8fy	
  when	
  mouse	
  was	
  clicked	
  
•  Mouse	
  /	
  touch	
  /	
  keyboard	
  can	
  be	
  received	
  either	
  
in	
  1)	
  polling	
  or	
  2)	
  event	
  handling	
  
Polling	
  Touch	
  /	
  Keyboard	
  
•  For	
  most	
  arcade	
  games,	
  polling	
  is	
  good	
  
•  Mul8touch	
  is	
  supported!	
  
–  boolean first = Gdx.input.isTouched(0);
–  boolean second = Gdx.input.isTouched(1);
–  int firstX = Gdx.input.getX();
–  int firstY = Gdx.input.getY();
–  int secondX = Gdx.input.getX(1);
–  int secondY = Gdx.input.getY(1);
•  Keyboard	
  
–  boolean isAPressed = Gdx.input.isKeyPressed(Keys.A);
Polling:	
  InputProcessor
•  An	
  InputProcessor	
  (Interface)	
  is	
  used	
  to	
  
receive	
  input	
  events	
  from	
  the	
  keyboard	
  and	
  
the	
  touch	
  screen	
  
•  It	
  has	
  to	
  be	
  registered	
  
– Input.setInputProcessor(InputProcessor);
•  When	
  registered	
  the	
  methods	
  from	
  
InputProcessor	
  interface	
  are	
  called.
InputProcessor	
  -­‐	
  methods	
  
Using	
  InputProcessor
public class InputDemo extends ApplicationAdapter implements InputProcessor {
@Override
public void create () {
Gdx.input.setInputProcessor(this);
}
@Override
public void render () {
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
...
	
  
Using	
  InputAdapter	
  and	
  Inner	
  Class	
  
public class InputDemo extends ApplicationAdapter {
@Override
public void create () {
Gdx.input.setInputProcessor(new Listener());
}
private class Listener extends InputAdapter {
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// implementation here
return true;
}
}
}
GESTURES	
  
GestureDetector	
  
•  TouchDown	
  
–  User	
  touches	
  screen	
  
•  LongPress	
  
–  User	
  touches	
  screen	
  for	
  some	
  8me	
  
•  Tap	
  
–  User	
  touches	
  screen	
  and	
  liVs	
  the	
  finger	
  again	
  
•  Pan	
  
–  User	
  drags	
  a	
  finger	
  across	
  the	
  screen.	
  Useful	
  for	
  implemenIng	
  Camera	
  panning	
  in	
  2D	
  
•  PanStop	
  
–  Called	
  when	
  no	
  longer	
  panning	
  
•  Fling	
  
–  User	
  dragged	
  a	
  finger	
  across	
  the	
  screen,	
  then	
  liVed	
  up.	
  Useful	
  for	
  swipe	
  gestures	
  
•  Zoom	
  
–  User	
  places	
  two	
  fingers	
  on	
  the	
  screen	
  and	
  moves	
  them	
  together/apart.	
  Useful	
  for	
  Camera	
  
zooming	
  
•  Pinch	
  
–  Zooming	
  +	
  rota8on	
  
Star8ng	
  to	
  Listen	
  to	
  Gestures	
  
•  Really	
  easy	
  
–  Gdx.input.setInputProcessor(new
GestureDetector(GestureListener));
•  GestureListener	
  is	
  an	
  interface	
  
•  GestureAdapter	
  also	
  available	
  
	
  
GestureListener	
  
public class MyGestureListener implements GestureListener {
@Override
public boolean touchDown(float x, float y, int pointer, int button) {}
@Override
public boolean tap(float x, float y, int count, int button) {}
@Override
public boolean longPress(float x, float y) {}
@Override
public boolean fling(float velocityX, float velocityY, int button) {}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {}
@Override
public boolean panStop(float x, float y, int pointer, int button) {}
@Override
public boolean zoom (float originalDistance, float currentDistance){}
@Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2
firstPointer, Vector2 secondPointer){}
}
ACCELEROMETER	
  
Accelerometer	
  
•  An	
  accelerometer	
  
measures	
  the	
  
accelera8on	
  of	
  a	
  device	
  
on	
  three	
  axes	
  
•  From	
  this	
  accelera8on	
  
one	
  can	
  derive	
  the	
  8lt	
  or	
  
orienta8on	
  of	
  the	
  device.	
  
–  Phones:	
  portrait	
  default	
  
–  Tablet:	
  landscape	
  default	
  
•  LibGDX	
  shows	
  
accelerometer	
  readings	
  
always	
  as	
  in	
  the	
  image	
  
(0,0)	
  
Accelerometer	
  x	
  
Accelerometer	
  y	
  
y	
  increments	
  
x	
  increments	
  
Accelerometer	
  Readings	
  
•  Accelerometer	
  readings	
  can	
  be	
  accessed	
  	
  
–  float accelX = Gdx.input.getAccelerometerX();
–  float accelY = Gdx.input.getAccelerometerY();
–  float accelZ = Gdx.input.getAccelerometerZ();
•  When	
  moving	
  and	
  if	
  in	
  landscape	
  mode	
  in	
  
android,	
  no8ce	
  X	
  vs	
  Y!	
  
–  speedX += Gdx.input.getAccelerometerY();
SIMPLE	
  TEXT	
  INPUT	
  
User	
  input	
  
•  Desktop	
  
– Swing	
  dialog	
  
•  Android	
  
– Android	
  dialog	
  
•  Use	
  TextInputListener
public class InputDemo extends ApplicationAdapter {
@Override
public void create () {
MyTextInputListener listener = new MyTextInputListener();
Gdx.input.setInputProcessor(newGestureDetector(newGestureDetector.GestureAdapter(){
@Override
public boolean tap(float x, float y, int count, int button) {
Gdx.input.getTextInput(new MyTextInputListener(), "title", "test", "hint");
return true;
}
}));
}
private class MyTextInputListener implements Input.TextInputListener {
@Override
public void input (String text) {
Gdx.app.log("InputDemo", text);
}
@Override
public void canceled () {
Gdx.app.log("InputDemo", "canceled");
}
}
}
SPRITE	
  
Game	
  Object	
  
•  Game	
  object	
  may	
  hold	
  informa8on	
  about	
  
–  Texture	
  
–  Geometry	
  
•  width	
  
•  height	
  
•  x,	
  y	
  
–  Color	
  
•  You	
  could	
  create	
  a	
  class	
  for	
  your	
  game	
  object	
  
that	
  capsulates	
  all	
  of	
  these	
  
•  But	
  even	
  beaer,	
  libGDX	
  has	
  already	
  this	
  class,	
  it's	
  
called	
  Sprite
Sprite	
  -­‐	
  class	
  
•  Holds	
  geometry,	
  color	
  and	
  texture	
  
informa8on	
  
•  Has	
  a	
  posi8on	
  and	
  a	
  size	
  given	
  as	
  width	
  and	
  
height	
  
•  Sprite	
  is	
  always	
  rectangular	
  
•  Sprite	
  has	
  also	
  origin	
  for	
  rota8on	
  and	
  scaling	
  
– origin	
  is	
  in	
  boaom	
  leV	
  
Crea8ng	
  a	
  Sprite
public class SpriteDemo extends ApplicationAdapter {
private Sprite player;
private Texture alienTexture;
private OrthographicCamera camera;
private SpriteBatch batch;
public final static float WIDTH = 1280;
public final static float HEIGHT = 720;
@Override
public void create () {
player = new Sprite( alienTexture = new Texture("alien-displeased-icon.png") );
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
batch = new SpriteBatch();
}
@Override
public void render() {
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
player.draw(batch);
batch.end();
}
@Override
public void dispose() {
alienTexture.dispose();
}
}
	
  
FRAME	
  ANIMATION	
  
Anima8on	
  
•  Use	
  Anima8on	
  class	
  
– Animation walkAnimation = new
Animation(frameDuration, frames);
•  Frame	
  dura8on?	
  Time	
  between	
  frames	
  in	
  
seconds:	
  1	
  /	
  60	
  fps	
  
•  Frames?	
  
– TextureRegion	
  array	
  
•  TextureRegion?	
  
– Part	
  of	
  texture
TextureRegion	
  
Split	
  .png	
  into	
  TextureRegions	
  
walkSheet = new Texture(Gdx.files.internal(”image.png"));
// Method for splitting one texture to TextureRegions
TextureRegion[][] tmp = TextureRegion.split(
walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS );
2D	
  array	
  -­‐>	
  1D	
  
private TextureRegion[] transformTo1D(TextureRegion[][] tmp) {
TextureRegion [] walkFrames
= new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
return walkFrames;
}
Split	
  .png	
  into	
  TextureRegions	
  
walkSheet = new Texture(Gdx.files.internal(”image.png"));
// Method for splitting one texture to TextureRegions
TextureRegion[][] tmp = TextureRegion.split(
walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS );
TextureRegion [] frames = transformTo1D(tmp);
Animation walkAnimation = new Animation(1 / 60f, frames);
Rendering	
  
float stateTime = 1.0f;
TextureRegion currentFrame;
public void render() {
// stateTime was initialized to 0.0f
stateTime += Gdx.graphics.getDeltaTime();
// stateTime is used to calculate the next frame
// frameDuration!
// true = it's a looping anim
// false = it's not a looping anim
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.begin();
spriteBatch.draw(currentFrame, 150, 150);
spriteBatch.end();
}
TIPS	
  
Crea8ng	
  Game	
  Objects	
  
•  For	
  each	
  Game	
  Object,	
  create	
  own	
  class	
  
•  The	
  class	
  may	
  have	
  
– Inheritance	
  relaIonship	
  with	
  Sprite	
  /	
  Texture	
  (is	
  –	
  
a)	
  or	
  
– ComposiIon	
  relaIonship	
  with	
  Sprite	
  /	
  Texture	
  
(has	
  –	
  a)	
  
•  Add	
  aaributes	
  like	
  
– speedX,	
  speedY,	
  sounds,	
  anima8ons	
  
•  See	
  example	
  from	
  course	
  homepage!	
  
Ad

More Related Content

What's hot (20)

Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019
Unity Technologies
 
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Unity Technologies
 
7java Events
7java Events7java Events
7java Events
Adil Jafri
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
Unity Technologies
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Unity Technologies
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
吳錫修 (ShyiShiou Wu)
 
Unity 13 space shooter game
Unity 13 space shooter gameUnity 13 space shooter game
Unity 13 space shooter game
吳錫修 (ShyiShiou Wu)
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
Richard Jones
 
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity Technologies
 
Soc research
Soc researchSoc research
Soc research
Bryan Duggan
 
HoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingHoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial Mapping
Takashi Yoshinaga
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
Andreas Jakl
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game Programming
Richard Jones
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
noorcon
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
noorcon
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
Bryan Duggan
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
noorcon
 
Cocos2dx 7.1-7.2
Cocos2dx 7.1-7.2Cocos2dx 7.1-7.2
Cocos2dx 7.1-7.2
Kyungryul KIM
 
Input and Interaction
Input and InteractionInput and Interaction
Input and Interaction
Syed Zaid Irshad
 
Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019
Unity Technologies
 
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Unity Technologies
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
Unity Technologies
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Unity Technologies
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
吳錫修 (ShyiShiou Wu)
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
Richard Jones
 
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Unity Technologies
 
HoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingHoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial Mapping
Takashi Yoshinaga
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
Andreas Jakl
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game Programming
Richard Jones
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
noorcon
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
noorcon
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
Bryan Duggan
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
noorcon
 

Similar to libGDX: User Input and Frame by Frame Animation (20)

Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2
Marlon Luz
 
Y Tiles
Y TilesY Tiles
Y Tiles
Christopher Yunker
 
Java-Events
Java-EventsJava-Events
Java-Events
Arjun Shanka
 
Working with Callbacks
Working with CallbacksWorking with Callbacks
Working with Callbacks
Syed Zaid Irshad
 
Real life XNA
Real life XNAReal life XNA
Real life XNA
Johan Lindfors
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
DrDGayathriDevi
 
Mobile Application Development class 005
Mobile Application Development class 005Mobile Application Development class 005
Mobile Application Development class 005
Dr. Mazin Mohamed alkathiri
 
Hill ch2ed3
Hill ch2ed3Hill ch2ed3
Hill ch2ed3
Kunal Verma
 
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2
NokiaAppForum
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
Adil Jafri
 
Animations in Flutter - FlutterConf LATAM 2024
Animations in Flutter - FlutterConf LATAM 2024Animations in Flutter - FlutterConf LATAM 2024
Animations in Flutter - FlutterConf LATAM 2024
johnpryan
 
Md2010 jl-wp7-sl-game-dev
Md2010 jl-wp7-sl-game-devMd2010 jl-wp7-sl-game-dev
Md2010 jl-wp7-sl-game-dev
Jose Luis Latorre Millas
 
Game Development using SDL and the PDK
Game Development using SDL and the PDK Game Development using SDL and the PDK
Game Development using SDL and the PDK
ardiri
 
Developing games for Series 40 full-touch UI
Developing games for Series 40 full-touch UIDeveloping games for Series 40 full-touch UI
Developing games for Series 40 full-touch UI
Microsoft Mobile Developer
 
Unity programming 1
Unity programming 1Unity programming 1
Unity programming 1
Petri Lankoski
 
java - topic (event handling notes )1.ppt
java - topic (event handling notes )1.pptjava - topic (event handling notes )1.ppt
java - topic (event handling notes )1.ppt
adityakanna2
 
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPTJAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
adityakanna2
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
shafiq sangi
 
"Pemrograman Python untuk Pemula dan Ahli"
"Pemrograman Python untuk Pemula dan Ahli""Pemrograman Python untuk Pemula dan Ahli"
"Pemrograman Python untuk Pemula dan Ahli"
Muhammadlenterabawon
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
Nitigan Nakjuatong
 
Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2
Marlon Luz
 
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2
NokiaAppForum
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
Adil Jafri
 
Animations in Flutter - FlutterConf LATAM 2024
Animations in Flutter - FlutterConf LATAM 2024Animations in Flutter - FlutterConf LATAM 2024
Animations in Flutter - FlutterConf LATAM 2024
johnpryan
 
Game Development using SDL and the PDK
Game Development using SDL and the PDK Game Development using SDL and the PDK
Game Development using SDL and the PDK
ardiri
 
java - topic (event handling notes )1.ppt
java - topic (event handling notes )1.pptjava - topic (event handling notes )1.ppt
java - topic (event handling notes )1.ppt
adityakanna2
 
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPTJAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
adityakanna2
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
shafiq sangi
 
"Pemrograman Python untuk Pemula dan Ahli"
"Pemrograman Python untuk Pemula dan Ahli""Pemrograman Python untuk Pemula dan Ahli"
"Pemrograman Python untuk Pemula dan Ahli"
Muhammadlenterabawon
 
Ad

More from Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
Jussi Pohjolainen
 
Intro to Java ME and Asha Platform
Intro to Java ME and Asha PlatformIntro to Java ME and Asha Platform
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 
Intro to PhoneGap
Intro to PhoneGapIntro to PhoneGap
Intro to PhoneGap
Jussi Pohjolainen
 
Quick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileQuick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery Mobile
Jussi Pohjolainen
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
Jussi Pohjolainen
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
Jussi Pohjolainen
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
Jussi Pohjolainen
 
XAMPP
XAMPPXAMPP
XAMPP
Jussi Pohjolainen
 
Building Web Services
Building Web ServicesBuilding Web Services
Building Web Services
Jussi Pohjolainen
 
Ad

Recently uploaded (20)

IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 

libGDX: User Input and Frame by Frame Animation

  • 1. libGDX:  User  Input  and  Frame  by   Frame  Anima8on   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 3. Event  vs  Polling   •  Polling   –  Do  something  on  each  frame,  really  fast   •  Was  mouse  clicked?  Was  mouse  clicked?  Was  mouse   clicked?   –  Good  for  arcade  games   •  Event   –  Do  something  when  event  handles   •  No8fy  when  mouse  was  clicked   •  Mouse  /  touch  /  keyboard  can  be  received  either   in  1)  polling  or  2)  event  handling  
  • 4. Polling  Touch  /  Keyboard   •  For  most  arcade  games,  polling  is  good   •  Mul8touch  is  supported!   –  boolean first = Gdx.input.isTouched(0); –  boolean second = Gdx.input.isTouched(1); –  int firstX = Gdx.input.getX(); –  int firstY = Gdx.input.getY(); –  int secondX = Gdx.input.getX(1); –  int secondY = Gdx.input.getY(1); •  Keyboard   –  boolean isAPressed = Gdx.input.isKeyPressed(Keys.A);
  • 5. Polling:  InputProcessor •  An  InputProcessor  (Interface)  is  used  to   receive  input  events  from  the  keyboard  and   the  touch  screen   •  It  has  to  be  registered   – Input.setInputProcessor(InputProcessor); •  When  registered  the  methods  from   InputProcessor  interface  are  called.
  • 7. Using  InputProcessor public class InputDemo extends ApplicationAdapter implements InputProcessor { @Override public void create () { Gdx.input.setInputProcessor(this); } @Override public void render () { } @Override public boolean keyDown(int keycode) { return false; } @Override public boolean keyUp(int keycode) { return false; } ...  
  • 8. Using  InputAdapter  and  Inner  Class   public class InputDemo extends ApplicationAdapter { @Override public void create () { Gdx.input.setInputProcessor(new Listener()); } private class Listener extends InputAdapter { @Override public boolean touchDragged(int screenX, int screenY, int pointer) { // implementation here return true; } } }
  • 10. GestureDetector   •  TouchDown   –  User  touches  screen   •  LongPress   –  User  touches  screen  for  some  8me   •  Tap   –  User  touches  screen  and  liVs  the  finger  again   •  Pan   –  User  drags  a  finger  across  the  screen.  Useful  for  implemenIng  Camera  panning  in  2D   •  PanStop   –  Called  when  no  longer  panning   •  Fling   –  User  dragged  a  finger  across  the  screen,  then  liVed  up.  Useful  for  swipe  gestures   •  Zoom   –  User  places  two  fingers  on  the  screen  and  moves  them  together/apart.  Useful  for  Camera   zooming   •  Pinch   –  Zooming  +  rota8on  
  • 11. Star8ng  to  Listen  to  Gestures   •  Really  easy   –  Gdx.input.setInputProcessor(new GestureDetector(GestureListener)); •  GestureListener  is  an  interface   •  GestureAdapter  also  available    
  • 12. GestureListener   public class MyGestureListener implements GestureListener { @Override public boolean touchDown(float x, float y, int pointer, int button) {} @Override public boolean tap(float x, float y, int count, int button) {} @Override public boolean longPress(float x, float y) {} @Override public boolean fling(float velocityX, float velocityY, int button) {} @Override public boolean pan(float x, float y, float deltaX, float deltaY) {} @Override public boolean panStop(float x, float y, int pointer, int button) {} @Override public boolean zoom (float originalDistance, float currentDistance){} @Override public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){} }
  • 14. Accelerometer   •  An  accelerometer   measures  the   accelera8on  of  a  device   on  three  axes   •  From  this  accelera8on   one  can  derive  the  8lt  or   orienta8on  of  the  device.   –  Phones:  portrait  default   –  Tablet:  landscape  default   •  LibGDX  shows   accelerometer  readings   always  as  in  the  image  
  • 15. (0,0)   Accelerometer  x   Accelerometer  y   y  increments   x  increments  
  • 16. Accelerometer  Readings   •  Accelerometer  readings  can  be  accessed     –  float accelX = Gdx.input.getAccelerometerX(); –  float accelY = Gdx.input.getAccelerometerY(); –  float accelZ = Gdx.input.getAccelerometerZ(); •  When  moving  and  if  in  landscape  mode  in   android,  no8ce  X  vs  Y!   –  speedX += Gdx.input.getAccelerometerY();
  • 18. User  input   •  Desktop   – Swing  dialog   •  Android   – Android  dialog   •  Use  TextInputListener
  • 19. public class InputDemo extends ApplicationAdapter { @Override public void create () { MyTextInputListener listener = new MyTextInputListener(); Gdx.input.setInputProcessor(newGestureDetector(newGestureDetector.GestureAdapter(){ @Override public boolean tap(float x, float y, int count, int button) { Gdx.input.getTextInput(new MyTextInputListener(), "title", "test", "hint"); return true; } })); } private class MyTextInputListener implements Input.TextInputListener { @Override public void input (String text) { Gdx.app.log("InputDemo", text); } @Override public void canceled () { Gdx.app.log("InputDemo", "canceled"); } } }
  • 21. Game  Object   •  Game  object  may  hold  informa8on  about   –  Texture   –  Geometry   •  width   •  height   •  x,  y   –  Color   •  You  could  create  a  class  for  your  game  object   that  capsulates  all  of  these   •  But  even  beaer,  libGDX  has  already  this  class,  it's   called  Sprite
  • 22. Sprite  -­‐  class   •  Holds  geometry,  color  and  texture   informa8on   •  Has  a  posi8on  and  a  size  given  as  width  and   height   •  Sprite  is  always  rectangular   •  Sprite  has  also  origin  for  rota8on  and  scaling   – origin  is  in  boaom  leV  
  • 24. public class SpriteDemo extends ApplicationAdapter { private Sprite player; private Texture alienTexture; private OrthographicCamera camera; private SpriteBatch batch; public final static float WIDTH = 1280; public final static float HEIGHT = 720; @Override public void create () { player = new Sprite( alienTexture = new Texture("alien-displeased-icon.png") ); camera = new OrthographicCamera(); camera.setToOrtho(false, WIDTH, HEIGHT); batch = new SpriteBatch(); } @Override public void render() { batch.setProjectionMatrix(camera.combined); Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); player.draw(batch); batch.end(); } @Override public void dispose() { alienTexture.dispose(); } }  
  • 26. Anima8on   •  Use  Anima8on  class   – Animation walkAnimation = new Animation(frameDuration, frames); •  Frame  dura8on?  Time  between  frames  in   seconds:  1  /  60  fps   •  Frames?   – TextureRegion  array   •  TextureRegion?   – Part  of  texture
  • 28. Split  .png  into  TextureRegions   walkSheet = new Texture(Gdx.files.internal(”image.png")); // Method for splitting one texture to TextureRegions TextureRegion[][] tmp = TextureRegion.split( walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS );
  • 29. 2D  array  -­‐>  1D   private TextureRegion[] transformTo1D(TextureRegion[][] tmp) { TextureRegion [] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; int index = 0; for (int i = 0; i < FRAME_ROWS; i++) { for (int j = 0; j < FRAME_COLS; j++) { walkFrames[index++] = tmp[i][j]; } } return walkFrames; }
  • 30. Split  .png  into  TextureRegions   walkSheet = new Texture(Gdx.files.internal(”image.png")); // Method for splitting one texture to TextureRegions TextureRegion[][] tmp = TextureRegion.split( walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS ); TextureRegion [] frames = transformTo1D(tmp); Animation walkAnimation = new Animation(1 / 60f, frames);
  • 31. Rendering   float stateTime = 1.0f; TextureRegion currentFrame; public void render() { // stateTime was initialized to 0.0f stateTime += Gdx.graphics.getDeltaTime(); // stateTime is used to calculate the next frame // frameDuration! // true = it's a looping anim // false = it's not a looping anim currentFrame = walkAnimation.getKeyFrame(stateTime, true); spriteBatch.begin(); spriteBatch.draw(currentFrame, 150, 150); spriteBatch.end(); }
  • 33. Crea8ng  Game  Objects   •  For  each  Game  Object,  create  own  class   •  The  class  may  have   – Inheritance  relaIonship  with  Sprite  /  Texture  (is  –   a)  or   – ComposiIon  relaIonship  with  Sprite  /  Texture   (has  –  a)   •  Add  aaributes  like   – speedX,  speedY,  sounds,  anima8ons   •  See  example  from  course  homepage!  
  翻译: