2 Replies - 393 Views - Last Post: 03 December 2012 - 05:23 PM Rate Topic: -----

#1 Ccl1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 12

Nick Gravelyn's Tile Engine Editor Error

Posted 03 December 2012 - 04:25 PM

Hi,

I am following the Nick Gravelyn online Tile Engine tutorial and have gotten as far as the TileEditor. When I try to run the game, I am met with an error which reads:

"Field 'TileEditor.Form1.spriteBatch' is never assigned to, and will always have its default value null c:\users\user\documents\visual studio 2010\Projects\TileGame\TileEditor\Form1.cs"

The only thing that is displayed is a white screen with a red X. The code is below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;

using TileEngine;




namespace TileEditor
{
    public partial class Form1 : Form
    {
        SpriteBatch spriteBatch;

        Camera camera = new Camera();

        TileLayer tileLayer = new TileLayer(new int[,]
        {
            { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3,},
            { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3,},
            { 1, 1, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3,},
            { 0,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, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3,},
            { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3,}
        });

        public GraphicsDevice GraphicsDevice
        {
            get { return tileDisplay1.GraphicsDevice; }
        }

        public Form1()
        {
            InitializeComponent();
            tileDisplay1.OnInitialize += new EventHandler(tileDisplay1_OnInitialize);
            tileDisplay1.OnDraw += new EventHandler(tileDisplay1_OnDraw);
        }

        public Texture2D Texture2DFromFile(string fileName)
        {
             Texture2D texture;
             FileStream stream = new FileStream(fileName, FileMode.Open);
             texture = Texture2D.FromStream(GraphicsDevice, stream);

             stream.Close();
             return texture;
        }

        void tileDisplay1_OnInitialize(object sender, EventArgs e)
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            tileLayer.AddTexture(Texture2DFromFile("Tiles/se_free_dirt_texture"));
            tileLayer.AddTexture(Texture2DFromFile("Tiles/se_free_grass_texture"));
            tileLayer.AddTexture(Texture2DFromFile("Tiles/se_free_ground_texture"));
            tileLayer.AddTexture(Texture2DFromFile("Tiles/se_free_mud_texture"));
            tileLayer.AddTexture(Texture2DFromFile("Tiles/se_free_road_texture"));

        }
        
        
        
        void tileDisplay1_OnDraw(object sender, EventArgs e)
        {
            tileLayer.Draw(spriteBatch, camera);
        }
    }
}


I cannot figure out what exactly I am doing wrong here, so any advice would be helpful. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Nick Gravelyn's Tile Engine Editor Error

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: Nick Gravelyn's Tile Engine Editor Error

Posted 03 December 2012 - 05:02 PM

Have you set breakpoints on your two event handlers to make sure that they are being called?

Have you ensured that the tiles being loaded exist and are in the correct path?
Was This Post Helpful? 0
  • +
  • -

#3 Ccl1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 12

Re: Nick Gravelyn's Tile Engine Editor Error

Posted 03 December 2012 - 05:23 PM

I had forgotten that since this is a WinForm app, the .jpg extension had to be added to the path. Seems to be working now. Thanks for your reply.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1