方法: “getMap”和 “map”不起作用

2020年8月30日 40点热度 0条评论

我是一个非常初级的程序员,所以我遵循一个youtube教程,向我展示了如何构建一个简单的蛇应用程序。 youtube-tutorial上的人比我使用AndroidStudio的旧版本。他使用AndroidStudio版本2.1.2,我使用AndroidStudio版本2.2.2和Java语言。

我的问题是他使用了getMapmap命令,它可以正常工作。当我这样做时,它是行不通的。我的邀请函是我必须在>方法“getMap”和“map”中替换的内容

我遵循的youtube教程:https://www.youtube.com/watch?v=bPlG7ra83lo
在12:00,他第一次使用此命令。

Game Engine

package pelgrom.laurens.snake101.engine;

import android.service.quicksettings.Tile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import pelgrom.laurens.snake101.Classes.Coordinate;
import pelgrom.laurens.snake101.enums.TileType;

/**
 * Created by Laptop on 23-1-2017.
 */

public class GameEngine {
    public static final int GameWidth = 28;
    public static final int Gameheight = 42;

    private List<Coordinate> walls = new ArrayList<>();


    public GameEngine() {

    }

    public void initGame(){

        AddWalls();

    }

    public TileType()() getMap() {
        TileType()() map = new TileType(GameWidth) (Gameheight);

        for (int x = 0; x < GameWidth; x++) {
            for (int y = 0; y < Gameheight; y++) {
                map(X)(Y) = TileType.Nothing;
            }
        }

        for (Coordinate wall: walls) {
            map(wall.getX())(wall.getY()) = TileType.Wall;
        }

        return map;


    }
           //fout zit hem in de "map" en de  "getMap"




    private void AddWalls() {
        //Top and bottom walls
        for (int x = 0; x < GameWidth; x ++) {
            walls.add(new Coordinate(x,0));
            walls.add(new Coordinate(x,Gameheight-1));
        }

        //Left and Right walls
        for (int y = 1; y < Gameheight; y++){
            walls.add(new Coordinate(0,y));
            walls.add (new Coordinate(GameWidth -1 , y));

        }

    }
}
`

SnakeView

`package pelgrom.laurens.snake101.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import pelgrom.laurens.snake101.enums.TileType;

/**
 * Created by Laptop on 23-1-2017.
 */

public class SnakeView extends View {
    private Paint mPaint = new Paint();
    private TileType snakeViewMap()();

    public SnakeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setSnakeViewMap(TileType()() map )( this.snakeViewMap = map; )

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if( snakeViewMap != null ){
            float tileSizeX = canvas.getWidth() / snakeViewMap.length;
            float tileSizeY = canvas.getHeight() / snakeViewMap.length;

            float circleSize = Math.min(tileSizeX, tileSizeY) / 2;

            for (int x = 0; x < snakeViewMap.lenght; x++) {
                for (int y = 0; y < snakeViewMap(x). lenght; y++) {
                    switch (snakeViewMap(x)) {

                        case Nothing:
                            mPaint.setColor(Color.WHITE);
                            break;
                        case Wall:
                            mPaint.setColor(Color.GREEN);
                            break;
                        case SnakeHead:
                            mPaint.setColor(Color.RED);
                            break;
                        case SnakeTail:
                            mPaint.setColor(Color.GREEN);
                            break;
                        case Apple:
                            mPaint.setColor(Color.RED);
                            break;

                    }

                    canvas.drawCircle(x * tileSizeX + tileSizeX / 2f + circleSize / 2, y * tileSizeY + tileSizeY / 2f + circleSize / 2, circleSize, mPaint);
                }
            }
        }
    }
 }
`

main activity

`package pelgrom.laurens.snake101;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import pelgrom.laurens.snake101.engine.GameEngine;
import pelgrom.laurens.snake101.views.SnakeView;

public class Activity extends AppCompatActivity {

    private GameEngine gameEngine;
    private SnakeView snakeView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        gameEngine = new GameEngine();
        gameEngine.initGame();

        snakeView = (SnakeView)findViewById(R.id.snakeView);
        snakeView.setSnakeViewMap(gameEngine.getMapAsync());
        snakeView.invalidate();
    }
}
`

解决方案如下:

代码的问题在于,由于声明了二维数组,因此您在使用圆括号()时必须使用方括号[]。像这样替换它们:

public TileType[][] getMap() {
    TileType[][] map = new TileType[GameWidth][Gameheight];

    for (int x = 0; x < GameWidth; x++) {
        for (int y = 0; y < Gameheight; y++) {
            map[x][y] = TileType.Nothing;
        }
    }

    for (Coordinate wall: walls) {
        map[wall.getX()][wall.getY()] = TileType.Wall;
    }

    return map;


 }

PS:您真的需要从基础开始。