Saturday 22 September 2012

#1.4 Tutorial (LibGDX & MTX) Test2_AbstractActor

"AbstractActor" is extending Actor, so it can do anything an Actor can do. I just gave couple of game specific functions to Actor.

What it can do:

  • Set a texture
  • Ready to use animation state timer
  • Set a animation 
  • Set a animation momentary (We will see in animations tutorial)
  • Translate in constant speed
  • All other actions that normal actors do




BAT TEST MODEL

I created a test model a bat, and I extended it to "AbstractActor" and nothing else. Just added the constructors which I may need from "AbstractActor"

package com.mtx.testmodels;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.moribitotech.mtx.AbstractActor;

public class Bat extends AbstractActor {

 public Bat(TextureRegion textureRegion, boolean isTextureRegionActive,
   float posX, float posY, float width, float height) {
  super(textureRegion, isTextureRegionActive, posX, posY, width, height);
 }

 public Bat(float posX, float posY, float width, float height) {
  super(posX, posY, width, height);
 }
}




TEST SCREEN

I just used previous tutorials screen, and added two bats (one only has texture one has animation). Simple as it is. Animation created in "Assets" class, I will talk about animations in later tutorials. You can set texture in constructor or use another constructor which has no view information, also there are other constructor options you many use.


  • Very quick implementation
  • No need to worry about animations or state timer
  • Cool momentary animation feature to use (Coming on one of the next tutorials)
  • No need to worry about texture



package com.mtx.tests;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.moribitotech.mtx.AbstractScreen;
import com.mtx.testassets.Assets;
import com.mtx.testmodels.Bat;

public class Test2_AbstractActor extends AbstractScreen {
 private Label lblFps;
 private Label lblScreenTime;
 private Bat bat;
 private Bat bat2;
 
 public Test2_AbstractActor(Game game, String screenName) {
  super(game, screenName);
  setUpGameElements();
 }
 


 @Override
 public void setUpScreenElements() {
  super.setUpScreenElements();
  
  // #1.1 TEST
  // Set background texture
  // #########################################################
  setBackgroundTexture(Assets.imgMtxBg);
   
  // #1.2 TEST
  // Set back button
  // (Override keyBackPressed to do some action see very below)
  // #########################################################
  setBackButtonActive(true);
  
  // #1.3 TEST
  // Screen time / Fps
  // Update by overriding render
  // #########################################################
  lblScreenTime = new Label("", Assets.getSkin());
  lblFps = new Label("", Assets.getSkin());
  lblScreenTime.setPosition(getStage().getWidth() - 80, getStage().getHeight() - 40);
  lblFps.setPosition(getStage().getWidth() - 80, getStage().getHeight() - 60);
  getStage().addActor(lblScreenTime);
  getStage().addActor(lblFps);
 }
 
 private void setUpGameElements() {
  
  // #2.1 TEST
  // Create an abstract actor
  // #####################################################
  bat = new Bat(Assets.imgBat, true, 50, 20 , 96, 96);
  getStage().addActor(bat);
  
  // #2.2 TEST
  // Create an abstract actor with animation
  // #####################################################
  bat2 = new Bat(50, 100 , 96, 96);
  bat2.setAnimation(Assets.animBatFlyRight, true, true);
  getStage().addActor(bat2);
 }
 
 @Override
 public void keyBackPressed() {
  super.keyBackPressed();
  getGame().setScreen(new Test0_AllTestsScreen(getGame(), ""));
 }
 
 @Override
 public void render(float delta) {
  super.render(delta);
  lblScreenTime.setText(getScreenTime());
  lblFps.setText("Fps: " + Gdx.graphics.getFramesPerSecond());
 }

}

2 comments:

  1. Hii Morbito. I want to start up with your nice Framework for my games. I am trying to make my tile game. Previously I was using Sprite as a base class for Player, Enemy, Coin etc.But they use vectors position and velocity. In your AbstractActor..there is nothing like that. For a platform game, I will need these vector calculations for sure.Please comment on this.

    ReplyDelete
    Replies
    1. You can extend my AbstractActor create your own actor with vector and velocity, at the same time you can get benefits from AbstractActor.

      Delete