How to Make Android 2D car game Tutorial 4

 


Class Attributes: Actor,InputEvent,ButtonGroup,CheckBox,ChangeListener, ClickListener,TextureRegionDrawable

Actor: 2D scene graph node. An actor has a position, rectangular size, origin, scale, rotation, Z index, and color. The position corresponds to the unrotated, unscaled bottom left corner of the actor. The position is relative to the actor’s parent. The origin is relative to the position and is used for scale and rotation.An actor has a list of in progress actions that are applied to the actor (often over time). These are generally used to change the presentation of the actor (moving it, resizing it, etc). See act(float)Action, and its many subclasses. An actor has two kinds of listeners associated with it: “capture” and regular. The listeners are notified of events the actor or its children receive. The regular listeners are designed to allow an actor to respond to events that have been delivered. The capture listeners are designed to allow a parent or container actor to handle events before child actors. See fire(com.badlogic.gdx.scenes.scene2d.Event) for more details. An InputListener can receive all the basic input events. More complex listeners (like ClickListener and ActorGestureListener) can listen for and combine primitive events and recognize complex interactions like multi-touch or pinch.

InputEvent: Event for actor input: touch, mouse, keyboard, and scroll.

ButtonGroup: Manages a group of buttons to enforce a minimum and maximum number of checked buttons. This enables “radio button” functionality and more. A button may only be in one group at a time.The canCheck(Button, boolean) method can be overridden to control if a button check or uncheck is allowed.

CheckBox: A checkbox is a button that contains an image indicating the checked or unchecked state and a label.

ChangeListener:Listener for ChangeListener.ChangeEvent.Fired when something in an actor has changed.Try to handle the given event, if it is applicable.

ClickListener: Detects mouse over, mouse or finger touch presses, and clicks on an actor. A touch must go down over the actor and is considered pressed as long as it is over the actor or within the tap square. This behavior makes it easier to press buttons on a touch interface when the initial touch happens near the edge of the actor. Double clicks can be detected using getTapCount(). Any touch (not just the first) will trigger this listener. While pressed, other touch downs are ignored.

TextureRegionDrawable: Drawable for a TextureRegion.

 package screens; 
import settings.DifficultyLevel;
 import settings.GameManager;
 import assets.AssetsDescriptors;
 import assets.RegionNames;
 import com.badlogic.gdx.audio.Sound;
 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 import com.badlogic.gdx.graphics.g2d.TextureAtlas;
 import com.badlogic.gdx.scenes.scene2d.Actor;
 import com.badlogic.gdx.scenes.scene2d.InputEvent;
 import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
 import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
 import com.badlogic.gdx.scenes.scene2d.ui.Label;
 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 import com.badlogic.gdx.scenes.scene2d.ui.Table;
 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
 import com.badlogic.gdx.utils.Logger;
 import com.ninal.game.GdxGame; 
public class OptionScreen extends ScreenBase {
     private static final Logger log = new Logger(
             OptionScreen.class.getSimpleName(), Logger.DEBUG);
     private BitmapFont font;
     private ButtonGroup checkButtonGroup;
     private CheckBox easy,medium,hard;
     private Sound clickSound;
public OptionScreen(GdxGame game) {
    super(game);

}

@Override
protected Actor createUI() {

    Skin btnskin = manager.get(AssetsDescriptors.BTN_SKIN);
    Skin uiSkin = manager.get(AssetsDescriptors.UISKIN);
    TextureAtlas gameAtlas = manager.get(AssetsDescriptors.GAME_ATLAS);
    TextureRegionDrawable optionbg = new TextureRegionDrawable(gameAtlas.findRegion(RegionNames.LOAD_BG));
    clickSound = manager.get(AssetsDescriptors.CLICK_SOUND);

    font = manager.get(AssetsDescriptors.FONT);
    // ==Parent table
    Table table = new Table();
    table.defaults().pad(10);

    DifficultyLevel difficultyLevel = GameManager.GAME_MANAGER.getDifficultyLevel();

    Label difficulty = new Label("DIFFICULTY", uiSkin);
    easy =  checkBox(difficultyLevel.easy.name(), uiSkin);
    medium =  checkBox(difficultyLevel.medium.name(), uiSkin);
    hard =  checkBox(difficultyLevel.hard.name(), uiSkin);
checkButtonGroup = new ButtonGroup(easy, medium, hard);
    checkButtonGroup.setChecked(difficultyLevel.name());
TextButton backbtn = new TextButton("BACK", uiSkin);
    backbtn.pad(4, 28, 4, 30);

    backbtn.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            back();
            clickSound.play();
    }});


    ChangeListener listener = new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            difficultyChange();
        }};

        ClickListener mouseClick = new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                clickSound.play();
            }};


    easy.addListener(listener);
    easy.addListener(mouseClick);

    medium.addListener(listener);
    medium.addListener(mouseClick);
    hard.addListener(listener);
    hard.addListener(mouseClick);


    Table btnTable = new Table(btnskin);
    btnTable.defaults().pad(10);
    btnTable.setBackground(RegionNames.PANEL);
    btnTable.add(difficulty).row();
    btnTable.add(easy).row();
    btnTable.add(medium).row();
    btnTable.add(hard).row();
    btnTable.add(backbtn);

    table.add(btnTable);
    table.setBackground(optionbg);
    table.setFillParent(true);
    table.center();
    table.pack();

    return table;
}

private void difficultyChange() {
    CheckBox checked = checkButtonGroup.getChecked();
    if (checked == easy) {
        log.debug("easy");
        GameManager.GAME_MANAGER.updateDifficultyLevel(DifficultyLevel.easy);
    }
    else if (checked == medium) {
        log.debug("medium");
        GameManager.GAME_MANAGER.updateDifficultyLevel(DifficultyLevel.medium);
    }
    else if (checked == hard) {
        log.debug("hard");
        GameManager.GAME_MANAGER.updateDifficultyLevel(DifficultyLevel.hard);
    }

}

private void back() {
    game.setScreen(new MenuScreen(game));
}

private static CheckBox checkBox(String difficultyName, Skin skin) {
    CheckBox checkbox = new CheckBox(difficultyName, skin);
    checkbox.left().pad(8);
    checkbox.getLabelCell().pad(8);
    return checkbox;
}
}

Post a Comment

0 Comments