frame, really fast • Was mouse clicked? Was mouse clicked? Was mouse clicked? – Good for arcade games • Event – Do something when event handles • Notify when mouse was clicked • Mouse / touch / keyboard can be received either in 1) polling or 2) event handling
is good • Multitouch 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);
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.
User touches screen for some time • Tap – User touches screen and lifts the finger again • Pan – User drags a finger across the screen. Useful for implementing Camera panning in 2D • PanStop – Called when no longer panning • Fling – User dragged a finger across the screen, then lifted up. Useful for swipe gestures • Zoom – User places two fingers on the screen and moves them together/apart. Useful for Camera zooming • Pinch – Zooming + rotation
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){} }
on three axes • From this acceleration one can derive the tilt or orientation of the device. – Phones: portrait default – Tablet: landscape default • LibGDX shows accelerometer readings always as in the image
accelX = Gdx.input.getAccelerometerX(); – float accelY = Gdx.input.getAccelerometerY(); – float accelZ = Gdx.input.getAccelerometerZ(); • When moving and if in landscape mode in android, notice X vs Y! – speedX += Gdx.input.getAccelerometerY();
Texture – Geometry • width • height • x, y – Color • You could create a class for your game object that capsulates all of these • But even better, libGDX has already this class, it's called Sprite
• Has a position and a size given as width and height • Sprite is always rectangular • Sprite has also origin for rotation and scaling – origin is in bottom left
Animation(frameDuration, frames); • Frame duration? Time between frames in seconds: 1 / 60 fps • Frames? – TextureRegionarray • TextureRegion? – Part of texture
{ // 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(); }
class • The class may have – Inheritance relationship with Sprite / Texture(is – a) or – Composition relationship with Sprite / Texture (has – a) • Add attributes like – speedX, speedY, sounds, animations • See example from course homepage!