【发布时间】:2018-11-28 15:53:53
【问题描述】:
我知道这个问题对于那些既熟悉平铺游戏又熟悉单人游戏的人来说是非常具体的,所以我会尽量做到全面。我已经在这个问题上工作了 15 多个小时,而且很绝望。
我正在尝试使用 Tiled 的对象层将精灵绘制到屏幕上。我创建了一个新的对象层,为其绘制了一些矩形,并添加了我在 LoadContent 方法中引用的自定义属性。
这是我的 LoadContent 方法:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
mymap = Content.Load<TiledMap>("Misc/newmap");
enemyShip = Content.Load<Texture2D>("Enemies/enemyship");
//TiledMapObject[] islands = mymap.GetLayer<TiledMapObjectLayer>("Islands").Objects;
ship_sprite = Content.Load<Texture2D>("character/wooden_ship");
Texture2D texture = Content.Load<Texture2D>("character/anima2");
animatedSprite = new AnimatedSprite(texture, 1, 2);
font = Content.Load<SpriteFont>("Misc/londrinasolid");
Song song = Content.Load<Song>("Music/Chad_Crouch_-_Stellars_Jay");
MediaPlayer.Volume = 0.7F;
MediaPlayer.Play(song);
TiledMapObject[] littleBoats = mymap.GetLayer<TiledMapObjectLayer>("SmallBoats").Objects;
foreach (var en in littleBoats)
{
string type;
en.Properties.TryGetValue("type", out type);
if (type == "smallboat")
{
Enemy.enemies.Add(new SmallBoat(en.Position));
}
}
// TODO: use this.Content to load your game content here
}
我创建了一个 TiledMapObject 数组,它从 tiledmap 对象层获取项目。它还检查自定义属性名称。如果它是“类型”,您可以在 tiledmap 图片的左下角看到标记,它会获取该对象并将其添加到 tiledmap 上位置的数组中(至少应该如此)。
在 draw 方法中,我创建了一个临时 Texture2D 变量,它引用了前面提到的敌人列表(现在填充了平铺地图中的两个元素)。如果键入 Smallboat,它会在错误的位置绘制我想要绘制的精灵。下面是相关的draw方法:
foreach (Enemy en in Enemy.enemies)
{
Texture2D spritetoDraw;
int rad;
if (en.GetType() == typeof(SmallBoat))
{
rad = 16;
spritetoDraw = enemyShip;
}
else
{
rad = 30;
spritetoDraw = ship_sprite;
}
spriteBatch.Draw(spritetoDraw, new Vector2(en.Position.X - rad, en.Position.Y - rad), Color.White);
}
这里是我跑步后敌船的起始位置,它与原点 0,0 略有偏移,因为在绘制方法中我减去了半径:
这是我的 Enemy 类,SmallBoat 子类位于底部。我创建了一个新列表,一旦将它们添加到我的主文件的 LoadContent 方法中,它就应该包含平铺层中的对象。我已经使用 Enemy.Remove() 检查了是否正在添加对象,它们是,所以我知道至少程序可以识别平铺文件中的对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace my_first_game
{
class Enemy
{
private Vector2 position;
protected int radius;
float circle = MathHelper.Pi * 2;
public float rotationAngle;
protected int health;
public static List<Enemy> enemies = new List<Enemy>();
public int Radius { get { return radius; } }
public Vector2 Position { get { return position; }}
public int Health { get { return health; } set { value = health; } }
public Enemy(Vector2 newpos)
{
newpos = position;
}
public void Update(GameTime gametime, Vector2 playerPos)
{
rotationAngle = circle % 2;
double dt = gametime.ElapsedGameTime.TotalSeconds;
Vector2 direction = new Vector2((float)Math.Cos(rotationAngle), (float)Math.Sin(rotationAngle));
Vector2 movedir = Position - playerPos;
movedir.Normalize();
position -= movedir;
}
}
class SmallBoat : Enemy
{
public SmallBoat(Vector2 newpos) : base(newpos)
{
//radius = 50;
}
}
}
我尝试过的:
1)。注释掉所有更新方法并运行,除了船不动外没有任何变化。
2)。绘制新矩形,擦除旧矩形,删除并创建对象层。
3)。如果我从对象层中删除一个矩形,那么一旦游戏运行,屏幕上绘制的船就会减少。因此我知道敌人被吸引到屏幕上,只是位置不对。
4)。在 draw 方法中将坐标更改为硬编码的固定数字,而不是 en.position.X 和 en.position.Y。这会将起始位置更改为所需位置,但它是硬编码的,并不能解决问题。
5)。重新排列 loadcontent 方法和 draw 方法中的元素,无济于事。
似乎列表被正确引用,问题是由于某种原因,平铺地图上的坐标没有转移。我的大部分代码都基于 Udemy Monogame 教程,并且 loadcontent 方法和 draw 方法基本相同。
热烈欢迎任何和所有帮助。如果有任何其他信息可以提供,请告诉我。
编辑:这是整个绘制方法。
protected override void Draw(GameTime gameTime)
{
controller.controller_update(gameTime);
spriteBatch.Begin();
if (controller.ingame == false)
{
spriteBatch.DrawString(font, "Welcome to Thallasaphobia. \n press Enter to Begin", new Vector2(100, 100), Color.White);
}
spriteBatch.End();
if (controller.ingame == true)
{
GraphicsDevice.Clear(Color.Black);
mapRenderer.Draw(mymap, cam.GetViewMatrix());
spriteBatch.Begin(transformMatrix: cam.GetViewMatrix());
animatedSprite.Draw(spriteBatch, new Vector2(480, 350));
//create a new rectangle around the ship_sprite
Rectangle ship_rectangle = new Rectangle(0, 0, ship_sprite.Width, ship_sprite.Height);
Vector2 origin = new Vector2(ship_sprite.Width / 2, ship_sprite.Height + 15);
foreach (Enemy en in Enemy.enemies)
{
Texture2D spritetoDraw;
int rad;
if (en.GetType() == typeof(SmallBoat))
{
rad = 16;
spritetoDraw = enemyShip;
}
else
{
rad = 30;
spritetoDraw = ship_sprite;
}
spriteBatch.Draw(spritetoDraw, new Vector2(en.Position.X - rad, en.Position.Y - rad), Color.White);
}
spriteBatch.Draw(ship_sprite, ship.Position, ship_rectangle, Color.White, ship.rotationAngle + MathHelper.Pi/2, origin, 1.0f, SpriteEffects.None, 1);
}
spriteBatch.End();
base.Draw(gameTime);
}
【问题讨论】:
-
请出示与绘制地图相关的代码。
-
@BradleyUffner 完成
-
如果 Kyle Schaub 的这门课程/教程为什么不问他udemy.com/user/kyle-schaub