이동중인 플랫폼이 타일을 치면 다른 방향으로 이동해야하는 타일 맵을 사용하고 있습니다. 타일 맵과 플랫폼 모두에 대한 목록을 사용하고 있습니다.이동 플랫폼을 타일 맵으로
아래 이미지는 플랫폼을 보여 주며 지속적으로 움직이며 검은 색 원 중 하나와 충돌하면 방향을 바꾸고 가고있는 것과 반대 방향으로 향해야합니다. 나는 그것이 다른 방향으로 갈 수 있도록 올바른 플랫폼을 찾을 필요로하는 곳에
는 불행하게도 나는 문제가 있습니다. 나는 메인 클래스에서리스트를 만들었고 메인 클래스 외에 다른 클래스 안에리스트를 호출하는 방법을 모른다.질문 : 플랫폼이 경우 내가 아는 수 있도록, 내가 다른 위치에서 다른 방향으로가는 여러 개체를 들고 목록을 부르는 것이고, 메인 클래스에서 생성되는 방법
타일과 충돌하는거야?
간단히 말해, 플랫폼을 타일과 충돌 시키려면 어떻게해야합니까?
두 개의 클래스가 사용되는 타일 맵, 블록 클래스와 GAME1 클래스 (기본 클래스)를 만들려면 : 여기
은 타일지도를 사용하고 호출 할 거쳐야하는 과정입니다. 블록 클래스 텍스처, 위치 내부
및 BlockState (그것이 무엇을 할 것 인 것을 결정하는)public Block(Texture2D Texture, Vector2 Position, int BlockState)
{
this.Texture = Texture;
this.Position = Position;
this.BlockState = BlockState;
}
다음 블록 클래스는 플레이어 클래스를 사용하는 방법을 사용합니다.
public Player BlockCollision(Player player)
{
Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Texture.Width - 10, 10);
Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Texture.Height, Texture.Width - 10, 10);
Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Texture.Height - 10);
Rectangle right = new Rectangle((int)Position.X + Texture.Width, (int)Position.Y + 5, 10, Texture.Height - 10);
if (BlockState == 1 || (BlockState == 2 && !player.goingUp))
{
if (top.Intersects(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height)))
{
if (player.Position.Y + player.Texture.Height > Position.Y && player.Position.Y + player.Texture.Height < Position.Y + Texture.Height/2)
{
player.Position.Y = player.ground = Position.Y - player.Texture.Height;
player.Velocity.Y = 0;
player.isJumping = false;
player.Time = 0;
player.botCollision = true;
}
}
}
return player;
}
다음으로 그리기 방법.
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
}
다음 주 클래스 Game1로 이동합니다.
저는 블록과 플랫폼 모두에 대한 목록을 만듭니다.
숯 시스템을 사용하여지도를 만듭니다.
List<char[,]> Levels = new List<char[,]>();
public int tileWidth, tileHeight;
그런 다음 Initialize 메서드에서 List를 호출하고지도를 만듭니다.
protected override void Initialize()
{
Blocks = new List<Block>();
Platforms = new List<MovingPlatform>();
char[,] Level1 = {{'.','.','#'},
{'.','.','#'},
{'.','.','#'}};
Levels.Add(Level1);
base.Initialize();
}
그러면 void LoadLevel이 on으로 설정됩니다.
void LoadLevel(int level)
{
Blocks.Clear();
Platforms.Clear();
player.Position = Vector2.Zero;
tileWidth = Levels[level].GetLength(1);
tileHeight = Levels[level].GetLength(0);
Texture2D blockSpriteA = Content.Load<Texture2D>("blockA2");
for (int x = 0; x < tileWidth; x++)
{
for (int y = 0; y < tileHeight; y++)
{
//Background
Blocks.Add(new Block(background, new Vector2(x * 50, y * 50), 0));
//Impassable Blocks
if (Levels[level][y, x] == '#')
{
Blocks.Add(new Block(blockSpriteA, new Vector2(x * 50, y * 50), 1));
}
//Vertical Moving Platform
if (Levels[level][y, x] == '=')
{
Platforms.Add(new MovingPlatform(platform, new Vector2(x * 50, y * 50), 3.0f, true));
}
다음 목록에서 업데이트를 호출합니다.
foreach (Block b in Blocks)
{
player = b.BlockCollision(player);
}
foreach (MovingPlatform m in Platforms)
{
player = m.BlockCollision(player);
m.Update(gameTime);
}
마지막으로 Draw 메서드가이를 호출합니다.
foreach (Block b in Blocks)
{
b.Draw(spriteBatch);
}
foreach (MovingPlatform m in Platforms)
{
m.Draw(spriteBatch);
}
지저분한 코드로 도와 주셔서 감사합니다. 나는이 일을하려고 노력할 것이고, 나는 나의 질문에 분명하지 않다는 점에 유감이다. 나는 당신이 약간 혼란스럽게 만들었을지도 모르는 것을 알고 있습니다. 클래스에서 코드를 따로 따로 추가하는 것이 가장 좋은 생각은 아니 었습니다. 나는 코드를 보여주는 더 쉬운 방법을 보지 못했다. 왜냐하면 누군가가 훑어보기에는 싫은 큰 덩어리 였기 때문이다. –
아니오 나는 당신의 게시물을 잘 이해했다. 나는 실제 충돌 탐지를하기위한 대답에 코드를 게시하기로되어 있었는지 확실하지 않았다. (나는 블록으로 그렇게하는 법을 모르겠다.) –
자, 이것에 대해서, 우리는 단지 쳐다 본다. 플랫폼 오브젝트를 포함한리스트 이제 for 루프를 만들고 타일의 위치가 겹치는 지 확인하기 위해 모두의 위치를 찾는 방법은 무엇입니까? 타일을 할 수있는 플랫폼을 찾는 방법을 말해줘. –