情况:在我的游戏中,我有瓷砖与玩家发生碰撞,但当我试图在它们碰撞时发生事件时,什么都不会发生。
问题:我目前在每个 tile 类中都有一个 Collision 类。在 tile 类中,我有一个使用参数传入播放器矩形(x、y、宽度和高度)的方法。然后我调用tile Collision 类检查碰撞方法。经过一些测试,当我检查每个图块(使用 foreach)并在每个图块类中调用更新方法时,我发现在我的 tileMap 类(包含 Tile 类的 2D 数组)中识别出碰撞,只有第一个瓷砖碰撞。没有其他的。
这是我的代码:
瓦:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
namespace Project
{
class Tile
{
Texture2D tileTexture;
Rectangle tileRect;
public Collision collision;
private Vector2 pos;
public int tileNumber;
private SpriteFont tileT;
bool box;
public enum tileCollision
{
passable,
nonpassable,
trigger,
}
public tileCollision tileValue;
public Tile(string tileType,int number, Rectangle newTileRect, ContentManager Content)
{
tileTexture = Content.Load<Texture2D>(tileType);
tileT = Content.Load<SpriteFont>("TimesNewRoman");
tileRect = newTileRect;
tileNumber = number;
pos.X = tileRect.X;
pos.Y = tileRect.Y;
//check if tile is within draw block
if (tileNumber >= 9 && tileNumber <= 13)
{
tileValue = tileCollision.passable;
}
if (tileNumber >= 0 && tileNumber <= 8 )
{
tileValue = tileCollision.nonpassable;
System.Console.WriteLine("NON PASSABLE TILE: " + " x: " + tileRect.X + " y: " + tileRect.Y);
}
collision = new Collision(tileRect.X, tileRect.Y, tileRect.Width, tileRect.Height);
}
public void update(Rectangle rect)
{
//System.Console.WriteLine(playerRect.X, playerRect.Y);
if (collision.boundingBoxCollisionCheck(rect.X, rect.Y, rect.Width, rect.Height))
{
//if (tileValue == tileCollision.nonpassable)
//{
// System.Console.WriteLine("COLLISION!!!!!");
//}
box = true;
}
if (box == true)
{
System.Console.WriteLine("COLLISION!!!!! at x: " + tileRect.X + "y: " + tileRect.Y );
}
}
public void draw(SpriteBatch spritebatch)
{
//if tile is within draw block then draw
spritebatch.Draw(tileTexture, tileRect, Color.White);
spritebatch.DrawString(tileT, pos.X.ToString(), pos, Color.White);
}
}
}
瓦片地图:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.IO;
namespace Project
{
class TileMap
{
//public Tile[,] tile = new Tile[10,10];
public List<Tile> tiles = new List<Tile>();
int width;
int height;
string[] tileType = { "Tiles/BrickGrassTile1", "Tiles/BrickGrassTile2", "Tiles/BrickGrassTile3", "Tiles/BrickGrassTile4",
"Tiles/BrickGrassTile5", "Tiles/BrickRoofTile1", "Tiles/BrickRoofTile2", "Tiles/BrickRoofTile3",
"Tiles/BrickTile1", "Tiles/GrassPuddleTile1", "Tiles/GrassRockTile1", "Tiles/WallTile1", "Tiles/GrassTile1", "Tiles/GrassTile2", "Tiles/BrickDoor1" };
public void generateMap(int [,]map, int size, ContentManager content)
{
for (int x = 0; x < map.GetLength(1); x++)
{
for (int y = 0; y < map.GetLength(0); y++)
{
int number = map[y, x];
if (number >= 0)
{
tiles.Add(new Tile(tileType[number],number, new Rectangle(x * size, y * size, size, size), content));
}
width = (x + 1) * size;
height = (y + 1) * size;
}
}
}
public void update(Rectangle playerRect)
{
foreach (Tile tile in tiles)
{
tile.update(playerRect);
}
}
public void draw(SpriteBatch spritebatch)
{
foreach (Tile tile in tiles)
{
tile.draw(spritebatch);
}
}
}
}