繁体   English   中英

基于图块的地图滚动

[英]Tile based map scrolling

该脚本绘制了地图和其他内容:

public void render(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);

        Drawable myImage;

        int tileWidth = 50;
        int tileHeight = 50;

        int mapWidth = 3;
        int mapHeight = 3;

        int rowBaseX = 0;
        int rowBaseY = 0;

        int[][] board = new int[][] {
                {0,0,0},
                {0,0,0},
                {0,0,2}
                };

        for (int row = 0; row < mapHeight; row++)
        {

        for (int col = 0; col < mapWidth; col++)
        {
        Resources res = this.getContext().getResources();

        switch(board[row][col])
        {
        case 0:
        myImage = res.getDrawable(R.drawable.tile1);
        break;
        case 1:
        myImage = res.getDrawable(R.drawable.tile2);
        break;
        default:
        myImage = res.getDrawable(R.drawable.tile3);
        break;
        }

        int curL;
        int curU;

        int curR;
        int curD;

        curL = rowBaseX + (col * tileWidth);
        curU = rowBaseY + (row * tileHeight);

        curR = curL + tileWidth;
        curD = curU + tileHeight;

        if (droid.x - decentreX < curR & droid.x + decentreX > curL) {
            if (droid.y - decentreY < curD & droid.y + decentreY > curU) {
        myImage.setBounds(curL,curU,curR,curD);
        myImage.draw(canvas);
            }
        }
        }
        }

        droid.draw(canvas);
        butt.draw(canvas);
        butt1.draw(canvas);
        butt2.draw(canvas);
        butt3.draw(canvas);
        buttz.draw(canvas);
        buttz1.draw(canvas);
        buttz2.draw(canvas);
        buttz3.draw(canvas);
        buttx.draw(canvas);
    }

在每一帧上都会调用render(Canvas canvas)方法。 如何滚动地图图块? 我尝试了这个:

public void render(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);

        Drawable myImage;

        int tileWidth = 50;
        int tileHeight = 50;

        int mapWidth = 3;
        int mapHeight = 3;

        int rowBaseX = 0;
        int rowBaseY = 0;

        int[][] board = new int[][] {
                {0,0,0},
                {0,0,0},
                {0,0,2}
                };

        for (int row = 0; row < mapHeight; row++)
        {

        for (int col = 0; col < mapWidth; col++)
        {
        Resources res = this.getContext().getResources();

        switch(board[row][col])
        {
        case 0:
        myImage = res.getDrawable(R.drawable.tile1);
        break;
        case 1:
        myImage = res.getDrawable(R.drawable.tile2);
        break;
        default:
        myImage = res.getDrawable(R.drawable.tile3);
        break;
        }

        int curL;
        int curU;

        int curR;
        int curD;

        curL = rowBaseX + (col * tileWidth);
        curU = rowBaseY + (row * tileHeight);

        if (droid.touched & !droid.touched1 & !droid.touched3) {
            curL -= 1;
        }else if (droid.touched1 & !droid.touched & !droid.touched2){
            curU += 1;
        }else if (droid.touched2 & !droid.touched1 & !droid.touched3){
            curL += 1;
        }else if (droid.touched3 & !droid.touched2 & !droid.touched){
            curU -= 1;
        }else if (droid.touched & droid.touched1){
            curL -= 1;
            curU += 1;
        }else if (droid.touched1 & droid.touched2){
            curL += 1;
            curU += 1;
        }else if (droid.touched2 & droid.touched3){
            curL += 1;
            curU -= 1;
        }else if (droid.touched3 & droid.touched){
            curL -= 1;
            curU -= 1;
        }

        curR = curL + tileWidth;
        curD = curU + tileHeight;

        if (droid.x - decentreX < curR & droid.x + decentreX > curL) {
            if (droid.y - decentreY < curD & droid.y + decentreY > curU) {
        myImage.setBounds(curL,curU,curR,curD);
        myImage.draw(canvas);
            }
        }
        }
        }

        droid.draw(canvas);
        butt.draw(canvas);
        butt1.draw(canvas);
        butt2.draw(canvas);
        butt3.draw(canvas);
        buttz.draw(canvas);
        buttz1.draw(canvas);
        buttz2.draw(canvas);
        buttz3.draw(canvas);
        buttx.draw(canvas);
    }

但是没有用。 不在此方法中的所有内容都不重要。 帮我! :)

似乎您正在更改lcal vars curLcurU 在每次调用时都会从rowBase重新计算Thore,将移动限制为您在检查droid.touched时使用的1 droid.touched

您应该通过更改rowBase变量来更改图块的全局位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM