LibGDX Video Tutorial: Scene2D Actors and Actions

 

In this second part of multiple part video tutorial looking at using Scene2D we look at creating Actors and Actions.  Actors are the entities that compose your game, while Actions are the things that make those Actors do… stuff.  The code for this example is included below the video.  The video on YouTube is available in high definition.

 

 

Actor Demo

package com.gamefromscratch;    import com.badlogic.gdx.ApplicationAdapter;  import com.badlogic.gdx.Gdx;  import com.badlogic.gdx.graphics.GL20;  import com.badlogic.gdx.scenes.scene2d.Stage;  import com.badlogic.gdx.utils.viewport.ScreenViewport;    public class ActorDemo extends ApplicationAdapter {       Stage stage;       @Override     public void create () {        ScreenViewport viewport = new ScreenViewport();        stage = new Stage(viewport);        Gdx.input.setInputProcessor(stage);          MyActor actor = new MyActor();        stage.addActor(actor);        stage.setKeyboardFocus(actor);     }       @Override     public void render () {        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);        stage.act(Gdx.graphics.getDeltaTime());        stage.draw();     }  }

 
package com.gamefromscratch;    import com.badlogic.gdx.Gdx;  import com.badlogic.gdx.Input;  import com.badlogic.gdx.graphics.Texture;  import com.badlogic.gdx.graphics.g2d.Batch;  import com.badlogic.gdx.graphics.g2d.Sprite;  import com.badlogic.gdx.scenes.scene2d.Actor;  import com.badlogic.gdx.scenes.scene2d.InputEvent;  import com.badlogic.gdx.scenes.scene2d.InputListener;  import com.badlogic.gdx.scenes.scene2d.Touchable;  import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;    public class MyActor extends Actor {      Sprite sprite = new Sprite(new Texture(Gdx.files.internal("badlogic.jpg")));        public MyActor(){          setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());          setTouchable(Touchable.enabled);            addListener(new InputListener(){              @Override              public boolean keyDown(InputEvent event, int keycode) {                  if(keycode == Input.Keys.RIGHT){                      MoveByAction mba = new MoveByAction();                      mba.setAmount(100f,0f);                      mba.setDuration(5f);                        MyActor.this.addAction(mba);                  }                  return true;              }          });      }        @Override      protected void positionChanged() {          sprite.setPosition(getX(),getY());          super.positionChanged();      }        @Override      public void draw(Batch batch, float parentAlpha) {          sprite.draw(batch);      }        @Override      public void act(float delta) {          super.act(delta);      }  }

 

Action Demo

package com.gamefromscratch;    import com.badlogic.gdx.ApplicationAdapter;  import com.badlogic.gdx.Gdx;  import com.badlogic.gdx.graphics.GL20;  import com.badlogic.gdx.graphics.Texture;  import com.badlogic.gdx.scenes.scene2d.Stage;  import com.badlogic.gdx.utils.viewport.ScreenViewport;    public class ActionDemo extends ApplicationAdapter {       Stage stage;       @Override     public void create () {        stage = new Stage(new ScreenViewport());        stage.addActor(new MyActor(new Texture(Gdx.files.internal("badlogic.jpg"))));        Gdx.input.setInputProcessor(stage);        stage.setKeyboardFocus(stage.getActors().first());     }       @Override     public void render () {        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);        stage.act(Gdx.graphics.getDeltaTime());        stage.draw();     }  }

 
package com.gamefromscratch;    import com.badlogic.gdx.Gdx;  import com.badlogic.gdx.Input;  import com.badlogic.gdx.graphics.Color;  import com.badlogic.gdx.graphics.Texture;  import com.badlogic.gdx.graphics.g2d.Batch;  import com.badlogic.gdx.scenes.scene2d.InputEvent;  import com.badlogic.gdx.scenes.scene2d.InputListener;  import com.badlogic.gdx.scenes.scene2d.actions.*;  import com.badlogic.gdx.scenes.scene2d.ui.Image;  import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;    import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;  import static com.badlogic.gdx.scenes.scene2d.actions.Actions.addAction;      public class MyActor extends Image {        @Override      public void draw(Batch batch, float parentAlpha) {          batch.setColor(this.getColor());            ((TextureRegionDrawable)getDrawable()).draw(batch, getX(),getY(),                  getOriginX(),getOriginY(),                  getWidth(),getHeight(),                  getScaleX(),getScaleY(),                  getRotation());      }        public MyActor(Texture texture){          super(texture);            setBounds(getX(),getY(),getWidth(),getHeight());            addListener(new InputListener(){              @Override              public boolean keyDown(InputEvent event, int keycode) {                  switch(keycode) {                      case Input.Keys.NUM_1:                          MoveToAction moveToAction = new MoveToAction();                          moveToAction.setPosition(200f,200f);                          moveToAction.setDuration(5f);                          MyActor.this.addAction(moveToAction);                          break;                        case Input.Keys.NUM_2:                          MoveByAction moveByAction = new MoveByAction();                          moveByAction.setAmount(-200f,0f);                          moveByAction.setDuration(3f);                          MyActor.this.addAction(moveByAction);                          break;                        case Input.Keys.NUM_3:                          ColorAction colorAction = new ColorAction();                          colorAction.setEndColor(Color.PURPLE);                          colorAction.setDuration(5f);                          MyActor.this.addAction(colorAction);                          break;                        case Input.Keys.NUM_4:                          MoveToAction mta = new MoveToAction();                          mta.setPosition(Gdx.graphics.getWidth() - 200f,Gdx.graphics.getHeight()-200f);                          mta.setDuration(3f);                            ScaleByAction sba = new ScaleByAction();                          sba.setAmount(2f);                          sba.setDuration(3f);                            RotateToAction rta = new RotateToAction();                          rta.setRotation(90f);                          rta.setDuration(3f);                            ParallelAction pa = new ParallelAction(mta,sba,rta);                          MyActor.this.addAction(pa);                          break;                        case Input.Keys.NUM_5:                          MoveToAction mta2 = new MoveToAction();                          mta2.setPosition(Gdx.graphics.getWidth() - 200f,Gdx.graphics.getHeight()-200f);                          mta2.setDuration(3f);                            ScaleByAction sba2 = new ScaleByAction();                          sba2.setAmount(2f);                          sba2.setDuration(3f);                            RotateToAction rta2 = new RotateToAction();                          rta2.setRotation(90f);                          rta2.setDuration(3f);                            SequenceAction sa = new SequenceAction(mta2,sba2,rta2);                          MyActor.this.addAction(sa);                          break;                        case Input.Keys.NUM_6:                          RunnableAction ra = new RunnableAction();                          ra.setRunnable(new Runnable(){                              @Override                              public void run() {                                  MyActor.this.setPosition(0f,0f);                                  MyActor.this.setRotation(0f);                                  MyActor.this.setScale(1f);                              }                          });                          MyActor.this.addAction(ra);                          break;                        case Input.Keys.SPACE:                          addAction(parallel(                              moveTo(200f, 200f, 3f),                              scaleTo(2f, 3f),                              rotateTo(90f, 3f)                          ));                          break;                  }                  return true;              }          });      }  }

Programming Video


Scroll to Top