方法:"getMap" 和 "map" 不起作用
Method: "getMap" and "map" doesn't work
我是一个非常初级的程序员,所以我关注了一个 youtube 教程,它向我展示了如何构建一个简单的 snake 应用程序。 youtube-tutorial 的人得到的 AndroidStudio
版本比我的旧。他用AndroidStudio
2.1.2 版,我用AndroidStudio
2.2.2 版java 语言。
我的问题是他使用命令 getMap
和 map
并且有效。当我这样做时,work.My guesion 是我必须在 >methods "getMap" 和 "map"
中替换的内容
我关注的 youtube-tutorial: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:你真的需要从基础开始。
我是一个非常初级的程序员,所以我关注了一个 youtube 教程,它向我展示了如何构建一个简单的 snake 应用程序。 youtube-tutorial 的人得到的 AndroidStudio
版本比我的旧。他用AndroidStudio
2.1.2 版,我用AndroidStudio
2.2.2 版java 语言。
我的问题是他使用命令 getMap
和 map
并且有效。当我这样做时,work.My guesion 是我必须在 >methods "getMap" 和 "map"
我关注的 youtube-tutorial: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:你真的需要从基础开始。