平铺对象层将位置绘制到错误的位置

Kid*_*idd 0 c# xna monogame tiled

我知道这个问题对那些熟悉平铺和单一游戏的人来说非常具体,所以我会尽量做到尽可能彻底.我已经在这个问题上工作了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
    }
Run Code Online (Sandbox Code Playgroud)

我创建了一个TiledMapObject数组,它从tilesmap对象层获取项目.它还会检查自定义属性名称.如果它是"类型",您可以看到在平铺地图图片的左下角标记,它将获取该对象并将其添加到平铺地图上的位置处的数组(至少它应该如此).

在draw方法中,我创建了一个临时的Texture2D变量,它引用了上面提到的敌人列表(现在填充了平铺地图中的两个元素).如果键入Smallboat,它会绘制我想要绘制的精灵,它只是在错误的位置绘制.这是相关的绘制方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

这里是敌人的起始位置,一旦我跑,它稍微偏离原点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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了什么:
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);
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

虽然我真的没有办法测试这个,但我相信问题出在你的构造函数中.您目前有这个代码:

public Enemy(Vector2 newpos)
{
   newpos = position;
}
Run Code Online (Sandbox Code Playgroud)

你应该有:

public Enemy(Vector2 newpos)
{
   position = newpos;
}
Run Code Online (Sandbox Code Playgroud)

由于"position"是Enemy类的属性,因此我们想要更新的值,而不是newpos,这只是一个参数.这样可以解释为什么你的敌人从原点开始,因为它的位置永远不会被初始设定.