libgdx操作不起作用

时间:2014-07-08 14:28:01

标签: java android libgdx

我已经在这个问题上工作了几天而没有运气。

我有一个名为Tile的类,它扩展了Actor类

public class Tile extends Actor{
    final String log = "" + this.getClass();
    static int numberOfTileTypes = 6;
    int column;
    int row;
    int tileType;
    Sprite sprite;
    Texture texture;
    int xPos;
    int yPos;
    String actorName;

@Override
public void act(float delta) {
    // TODO Auto-generated method stub
    super.act(delta);   
}


}

和一个名为Scene的类,它也扩展了Actor,并被设置为屏幕上所有actor的inputProcessor。它的绘制方法设置为绘制所有演员。

public class Scene extends Actor implements InputProcessor{
public enum SwapDirection {
    LEFT, RIGHT, UP, DOWN;
}
SwapDirection swapDirection;
private final String log = "" + this.getClass();
private static float TILE_WIDTH = 32.0f;
private static float TILE_HEIGHT = 36.0f;
Texture texture;
Texture greyBackgroundTexture = new Texture(Gdx.files.internal("Tile.png"));
List<Tile> tileList = new ArrayList<Tile>();
int swipeFromColumn;
int swipeFromRow;
int swipeToColumn;
int swipeToRow;
Tile fromTile;
Tile toTile;
boolean dragging  = false;
boolean swapPending = false;

public Scene(){
    Gdx.input.setInputProcessor(this);
}
public void addSpritesForTiles(List<Tile> list){

    for(Tile tile: list){
        int type = tile.tileType;
        switch (type){
        case Constants.CROISSANT:
            texture = new Texture(Gdx.files.internal("Croissant.png"));
            break;
        case Constants.CUPCAKE:
            texture = new Texture(Gdx.files.internal("Cupcake.png"));
            break;
        case Constants.DANISH:
            texture = new Texture(Gdx.files.internal("Danish.png"));
            break;
        case Constants.DONUT:
            texture = new Texture(Gdx.files.internal("Donut.png"));
            break;
        case Constants.MACAROON:
            texture = new Texture(Gdx.files.internal("Macaroon.png"));
            break;
        case Constants.SUGAR_COOKIE:
            texture = new Texture(Gdx.files.internal("SugarCookie.png"));
            break;
        }
        tile.texture = texture;
        Sprite sprite = new Sprite(texture);
        tile.sprite = sprite;
        tile.xPos = (int) (tile.column * TILE_WIDTH + TILE_WIDTH/2);
        tile.yPos = (int) (tile.row * TILE_HEIGHT + TILE_HEIGHT/2);
        tile.sprite.setPosition(tile.xPos, tile.yPos);
        tileList.add(tile);
    }

}
public void updateSpritePositions(Tile[] list) {
    tileList.remove(list[0]);
    tileList.remove(list[1]);
    tileList.add(list[2]);
    tileList.add(list[3]);  
}
@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    for(Tile t: tileList ) {
        batch.draw(greyBackgroundTexture, t.xPos, t.yPos);
        batch.draw(t.texture, t.xPos, t.yPos);
    }
}
@Override
public void act(float delta) {
    super.act(delta);
    int totalActions = this.getActions().size;
    for(Iterator<Action> iter = this.getActions().iterator(); iter.hasNext();){
        iter.next().act(delta);
    }
    Gdx.app.log(log, "Running Actions: "+ totalActions);
}
}

和一个名为Level的类,它被设置为创建Tile对象

public class Level {

private int numColumns = 9;
private int numRows = 9;
Tile board[][] = new Tile [numColumns][numRows];
List<Tile> tileList = new ArrayList<Tile>();
List<Tile> spacesList = new ArrayList<Tile>();
LevelDetails ld;

public void loadLevel(int levelNumber){
    Json json = new Json();
    String s = "Levels/Level_" + levelNumber + ".json";
    ld = json.fromJson(LevelDetails.class, Gdx.files.internal(s));  
}
public List<Tile> shuffle(){

    for(int row=0; row< numRows; row++){
        for(int column=0; column< numColumns; column++){
            int pos = row *9 + column;
            if(ld.tiles[pos]!= 0){
                Random random = new Random();
                int tileType = random.nextInt(Tile.numberOfTileTypes)+1;
                Tile tile = createTileAtPosition(column,row,tileType);
                board[column][row] = tile;
                tileList.add(tile);
                Gdx.app.log("TILELIST:", tile.description());
            }
        }
    }
    return tileList;
}
private Tile createTileAtPosition(int column, int row, int tileType) {
    Tile tile = new Tile();
    tile.tileType = tileType;
    tile.column = column;
    tile.row = row;
    tile.actorName = "" + column + "" + row;
    return tile;
}

public  Tile[] swapTiles(int fromCol,int fromRow,int toCol,int toRow) {
    // TODO Auto-generated method stub
    Tile []tiles = new Tile[4];
    Tile fromTile = tileAtPosition(fromCol, fromRow);
    Tile toTile = tileAtPosition(toCol, toRow);
    Gdx.app.log("BEOFRE", "First POS: "+fromTile.description()+" Second POS : " + toTile.description());
    tiles[0]=fromTile;
    tiles[1]=toTile;

    Tile newFromTile = new Tile();
    newFromTile.column = fromTile.column;
    newFromTile.row = fromTile.row;
    newFromTile.tileType = toTile.tileType;
    newFromTile.texture = fromTile.texture;
    newFromTile.xPos = toTile.xPos;
    newFromTile.yPos = toTile.yPos;
    newFromTile.sprite = toTile.sprite;
    newFromTile.actorName = toTile.actorName;

    Tile newToTile = new Tile();
    newToTile.column = toTile.column;
    newToTile.row = toTile.row;
    newToTile.tileType = fromTile.tileType;
    newToTile.texture = toTile.texture;
    newToTile.xPos = fromTile.xPos;
    newToTile.yPos = fromTile.yPos;
    newToTile.sprite = fromTile.sprite;
    newToTile.actorName = fromTile.actorName;

    board[fromCol][fromRow]= newFromTile;
    board[toCol][toRow]= newToTile;
    tiles[2]= newFromTile;
    tiles[3]= newToTile;
    Gdx.app.log("AFTER", "First POS: "+newFromTile.description()+" SecondPOS: " +         newToTile.description());



    return tiles;
}

}

//Inner Class for the Json files.  Parses into a LevelDetails object.
class LevelDetails {
    int[] tiles;
    int targetScore;
    int moves;

}

最后是一个名为Game的类,它创建了一个新的Level,一个新的Scene和Stage。该级别基本上发回一个瓦片列表,这些瓦片被传递到场景以便将精灵附加到它们,然后它们作为演员添加到舞台上。

public class Game extends ApplicationAdapter {
private static String log = "Game Class";
Texture background;
Scene scene;
Level level;
List<Tile> list;
private Stage stage;
@Override
public void create () {
    stage = new Stage(new ScreenViewport());
    background = new Texture(Gdx.files.internal("Background@2x.png"));
    Image im = new Image(background);
    scene = new Scene();
    level = new Level();
    stage.addActor(im);
    stage.addActor(scene);
    beginGame();


}

private void beginGame() {
    level.loadLevel(0);
    List<Tile>tempList = level.shuffle();
    this.scene.addSpritesForTiles(tempList);
    list = scene.tileList;// test...does this need to be called??

    for(Tile t: list){
        t.setName(t.actorName);
        stage.addActor(t);
        //Gdx.app.log("ACTOR NAME=== ", t.getName());
    }
}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if(scene.swapPending){
        scene.swapPending = false;
        swapTilePositions();
    }
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

public  void swapTilePositions() {
    Tile[] list = level.swapTiles(scene.swipeFromColumn,scene.swipeFromRow,scene.swipeToColumn,scene.swipeToRow);

//  scene.updateSpritePositions(list);

    String oneName = list[2].actorName;
    String twoName = list[3].actorName;
    Gdx.app.log("ONENAME VAR", oneName);
    Gdx.app.log("TWONAME VAR", twoName);
    Actor one = stage.getRoot().findActor(oneName);
    Actor two = stage.getRoot().findActor(twoName);
    Gdx.app.log("ACTOR1 VAR", one.getName());
    Gdx.app.log("ACTOR2 VAR", two.getName());

    one.addAction(moveTo(100,100,2.0f));

    MoveToAction moveAction2 = new MoveToAction();
    moveAction2.setPosition(200, 200);
    moveAction2.setDuration(2.0f);
    two.addAction(moveAction2);

    Gdx.app.log(log, "# of actions: " + stage.getRoot().getActions().size);
}
}

当场景实例确定应交换两个平铺位置时,它会将swapPending变量设置为false,以便

    public  void swapTilePositions() {
    Tile[] list = level.swapTiles(scene.swipeFromColumn,scene.swipeFromRow,scene.swipeToColumn,scene.swipeToRow);

    //scene.updateSpritePositions(list);

    String oneName = list[2].actorName;
    String twoName = list[3].actorName;
    Gdx.app.log("ONENAME VAR", oneName);
    Gdx.app.log("TWONAME VAR", twoName);
    Actor one = stage.getRoot().findActor(oneName);
    Actor two = stage.getRoot().findActor(twoName);
    Gdx.app.log("ACTOR1 VAR", one.getName());
    Gdx.app.log("ACTOR2 VAR", two.getName());

    one.addAction(moveTo(100,100,2.0f));

    MoveToAction moveAction2 = new MoveToAction();
    moveAction2.setPosition(200, 200);
    moveAction2.setDuration(2.0f);
    two.addAction(moveAction2);

    Gdx.app.log(log, "# of actions: " + stage.getRoot().getActions().size);
}

被调用。上述方法中的所有日志输出都会返回您期望的结果。例如,当String oneName记录“34”时,Actor one.getName()也返回“34”,所以我假设变量一和二是它们应该对应的屏幕上的actor的句柄,但是当我添加动作给他们......什么都没有!此处显示操作数的日志也为0。

我最好的猜测是,我在绘制,渲染或动作方法中搞砸了一些东西,但无法找出出错的地方。

BTW,所有内容都按照预期在屏幕上绘制,添加动作时没有任何动作。

提前致谢!

0 个答案:

没有答案