SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
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!	
  

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 
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
 
7java Events
7java Events7java Events
7java Events
 
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
 
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遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
 
Unity 13 space shooter game
Unity 13 space shooter gameUnity 13 space shooter game
Unity 13 space shooter game
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
 
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
 
Soc research
Soc researchSoc research
Soc research
 
HoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingHoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial Mapping
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game Programming
 
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)
 
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)
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
 
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)
 
Cocos2dx 7.1-7.2
Cocos2dx 7.1-7.2Cocos2dx 7.1-7.2
Cocos2dx 7.1-7.2
 
Input and Interaction
Input and InteractionInput and Interaction
Input and Interaction
 

Ähnlich wie libGDX: User Input and Frame by Frame Animation

14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
Adil Jafri
 

Ähnlich wie 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
 
Y Tiles
Y TilesY Tiles
Y Tiles
 
Java-Events
Java-EventsJava-Events
Java-Events
 
Working with Callbacks
Working with CallbacksWorking with Callbacks
Working with Callbacks
 
Real life XNA
Real life XNAReal life XNA
Real life XNA
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
 
Mobile Application Development class 005
Mobile Application Development class 005Mobile Application Development class 005
Mobile Application Development class 005
 
Hill ch2ed3
Hill ch2ed3Hill ch2ed3
Hill ch2ed3
 
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
 
Md2010 jl-wp7-sl-game-dev
Md2010 jl-wp7-sl-game-devMd2010 jl-wp7-sl-game-dev
Md2010 jl-wp7-sl-game-dev
 
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
 
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
 
Unity programming 1
Unity programming 1Unity programming 1
Unity programming 1
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
Advance ui development and design
Advance ui  development and design Advance ui  development and design
Advance ui development and design
 
A 3D printing programming API
A 3D printing programming APIA 3D printing programming API
A 3D printing programming API
 
Swift for tensorflow
Swift for tensorflowSwift for tensorflow
Swift for tensorflow
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 

Mehr von Jussi Pohjolainen

Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
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 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
 
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
 

Mehr von Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
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
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
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
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 
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
 
Intro to PhoneGap
Intro to PhoneGapIntro to PhoneGap
Intro to PhoneGap
 
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
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
 
XAMPP
XAMPPXAMPP
XAMPP
 
Building Web Services
Building Web ServicesBuilding Web Services
Building Web Services
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

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!