所有的变化都是相同的片段,为什么?

时间:2014-12-29 22:49:59

标签: android android-fragments

我创建了一个战舰游戏,其中玩家1在底部屏幕中看到他的船只,顶部屏幕是他在玩家2的棋盘上移动的地方。但是一切都被发送到了player1屏幕。所以所有的球员1和所有针对球员2的动作都出现在_p1BattleshipFragment中。这是片段的限制,还是我在某个地方使用了错误的片段名称(即使我已经查看了我的代码几个小时试图找出原因),或者不知何故我将所有内容发送到该片段而没有意识到它?我需要一双新鲜的眼睛和/或解释我在这里做错了什么。非常感谢任何帮助!

public static final String GAME_INDEX_EXTRA = "game_index";
int _gameIndex = -1;

Game _currentGame;
BattleshipFragment _p2BattleshipFragment;
BattleshipFragment _p1BattleshipFragment;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    LinearLayout rootLayout = new LinearLayout(this);
    rootLayout.setOrientation(LinearLayout.VERTICAL);
    setContentView(rootLayout);

    if(getIntent().hasExtra(GAME_INDEX_EXTRA))
    {
        _gameIndex = getIntent().getExtras().getInt(GAME_INDEX_EXTRA);

        if(_gameIndex != -1)
            _currentGame = GameLibrary.getInstance().getGame(_gameIndex);
        else
            _currentGame = new Game();

    }
    else
    {
        finish();
    }

    FrameLayout p2_screen = new FrameLayout(this);
    p2_screen.setId(10);
    rootLayout.addView(p2_screen, new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 49));

    View divider = new View(this);
    divider.setBackgroundColor(Color.BLACK);
    rootLayout.addView(divider, new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 2));

    FrameLayout p1_screen = new FrameLayout(this);
    p1_screen.setId(11);
    rootLayout.addView(p1_screen, new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 49));


    _p2BattleshipFragment = new BattleshipFragment();
    _p1BattleshipFragment = new BattleshipFragment();

    FragmentTransaction addTransaction = getFragmentManager().beginTransaction();
    addTransaction.add(10, _p2BattleshipFragment);
    addTransaction.add(11, _p1BattleshipFragment);
    addTransaction.commit();

    p2_screen.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            _currentGame.gameState = "In Progress";

            int id = _p2BattleshipFragment._currentViewId;
            int x = 0;
            int y = id;
            int value = 0;
            if(y > 10)
            {
                x = id/10;
                y = id%10;
            }
            value = _currentGame.markHit(_currentGame.p2, x, y);

            switch(value)
            {
                case -1: // hit
                    _p2BattleshipFragment.setColor(id, Color.RED);
                    break;
                case 0: // already guessed
                    break;
                case 10: // miss
                    _p2BattleshipFragment.setColor(id, Color.BLACK);
                    break;
                case 100: // sunk ship
                    break;
                case 1000: // game over
                    break;
                default:
                    break;
            }
            return true;
        }
    });

}

@Override
protected void onStart()
{
    super.onStart();

    int[][] board = _currentGame.p1._board;
    for(int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            int id = 0;
            try
            {
                String idString = Integer.toString(j) + Integer.toString(i);
                id = Integer.parseInt(idString);
            } catch (Exception e)
            {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
            int value = board[i][j];
            switch (value)
            {
                case -1: // hit
                    _p1BattleshipFragment.setColor(id, Color.RED);
                    break;
                case 0: // already guessed
                    _p1BattleshipFragment.setColor(id, Color.BLACK);
                    break;
                case 1: // miss
                    _p1BattleshipFragment.setColor(id, Color.GRAY);
                    break;
                default:
                    break;
            }

        }
    }

    int[][] board2 = _currentGame.p2._board;
    for(int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            int id = 0;
            try
            {
                String idString = Integer.toString(j) + Integer.toString(i);
                id = Integer.parseInt(idString);
            } catch (Exception e)
            {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
            int value = board2[i][j];
            switch (value)
            {
                case -1: // hit
                    _p2BattleshipFragment.setColor(id, Color.RED);
                    break;
                case 0: // already guessed
                    _p2BattleshipFragment.setColor(id, Color.BLACK);
                    break;
                case 1: // miss
                    _p2BattleshipFragment.setColor(id, Color.GRAY);
                    break;
                default:
                    break;
            }

        }
    }

}

没有xml,因为这是一个赋值,它是指令的一部分 - 一切都是以编程方式完成的。

0 个答案:

没有答案