Object reference not set to an instance of an object.

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
hoi allemaal,

heb dit:

Code:
if (mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (preState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
                {

                }
                else
                {//Function for moving, scaling, rotating the objects.
                    if (CanEdit())
                    {
                        int xM = mouseState.X - preState.X;
                        int yM = mouseState.Y - preState.Y;
                        if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)
                        {
                            if (f == null)
                                MessageBox.Show("Is null");
                            new CharDef();
                            charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList[selPart].location +=
                                //charDef.GetFrame(selFrame).GetPart(selPart).location +=
                                new Vector2((float)xM / 2.0f, (float)yM / 2.0f);//Speed where u can move the objects
                        }
                    }
                }
            }

en de classe:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
//Classe for save and open files(This means, what u have made already)
namespace xCharEdit.Character
{
    public class CharDef
    {
        List<Animation> animation;
        //public Frame[] frame;
        public String path;

        public int headIdx;
        public int torsoIdx;
        public int legsIdx;
        public int weaponIdx;

        public CharDef()
        {
            animation = new List<Animation>();
            //for (int i = 0; i < animation.Length; i++)
                //animation[i] = new Animation();
            /*frame = new Frame[512];
            for (int i = 0; i < frame.Length; i++)
                frame[i] = new Frame();*/
            Animation a = new Animation(666);
            animation.Add(a);
            path = "char";
        }//End method

        public Animation GetAnimation(int idx)
        {
            //new CharDef();
            //new Animation();
            return (idx >= 0 ) ? animation[idx] : null;
        }

        public void SetAnimation(int idx, Animation _animation)
        {
            animation[idx] = _animation;
        }

        public List<Animation> GetAnimationArray()
        {
            return animation;
        }

        /*public Frame GetFrame(int idx)
        {
            return frame[idx];
        }

        public void SetFrame(int idx, Frame _frame)
        {
            frame[idx] = _frame;
        }

        public Frame[] GetFrameArray()
        {
            return frame;
        }*/

        /*public void WriteBackup()
        {
            Write(@"data/" + path); //+ ".zmx");
            Write(@"../../../../CharacterEditor/Debug/char" + path);//  data/chars/" + path); //+ ".zmx");
        }

        public void Write()
        {
            Write(@"data/" + path); //+ ".zmx");
        }

        public void Write(String writePath)
        {
            BinaryWriter b = new BinaryWriter(File.Open(writePath,
                FileMode.Create));

            b.Write(path);
            b.Write(headIdx);
            b.Write(torsoIdx);
            b.Write(legsIdx);
            b.Write(weaponIdx);

            for (int i = 0; i < animation.Length; i++)
            {
                b.Write(animation[i].name);

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    b.Write(keyframe.frameRef);
                    b.Write(keyframe.duration);
                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        b.Write(script[s]);
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {
                b.Write(frame[i].name);

                for (int j = 0; j < frame[i].GetPartArray().Count; j++)
                {
                    Part p = frame[i].GetPart(j);
                    b.Write(p.idx);
                    b.Write(p.location.X);
                    b.Write(p.location.Y);
                    b.Write(p.rotation);
                    b.Write(p.scaling.X);
                    b.Write(p.scaling.Y);
                    b.Write(p.flip);
                }
            }

            b.Close();

            Console.WriteLine("Saved.");
        }//End method Save

        public void Read()
        {
            BinaryReader b;
            try
            {
                b = new BinaryReader(File.Open(@"data/" + path + ".zmx",
                    FileMode.Open, FileAccess.Read));
            }
            catch (Exception e)
            {
                return;
            }

            path = b.ReadString();
            headIdx = b.ReadInt32();
            torsoIdx = b.ReadInt32();
            legsIdx = b.ReadInt32();
            weaponIdx = b.ReadInt32();
            

            for (int i = 0; i < animation.Length; i++)
            {   
                animation[i].name = b.ReadString();

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    keyframe.frameRef = b.ReadInt32();
                    keyframe.duration = b.ReadInt32();

                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        script[s] = b.ReadString();
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {   
                frame[i].name = b.ReadString();

                for (int j = 0; j < frame[i].GetPartArray().Length; j++)
                {
                    Part p = frame[i].GetPart(j);
                    p.idx = b.ReadInt32();
                    p.location.X = b.ReadSingle();
                    p.location.Y = b.ReadSingle();
                    p.rotation = b.ReadSingle();
                    p.scaling.X = b.ReadSingle();
                    p.scaling.Y = b.ReadSingle();
                    p.flip = b.ReadInt32();
                }
            }

            b.Close();

            Console.WriteLine("Loaded.");
        }*/
    }
}

maar krijg bij deze regel:

Code:
 if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)

de volgende fout:

Object reference not set to an instance of an object.

Iemand een idee?

Alvast bedankt.
 
Dit kan aan veel dingen liggen

Code:
if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)

Misschien is f null
of
is er niks geselecteerd
als er niks geselecteerd is dan heeft "f.AnimationBox.SelectedIndex" een waarde van -1 .Als die waarde in de onderstaande methode wordt ingevoerd dan wordt er null geretourneerd.
Dus een leeg animation object dat nog niet geïnstantieerd is
Code:
public Animation GetAnimation(int idx)
        {
            //new CharDef();
            //new Animation();
            return (idx >= 0 ) ? animation[idx] : null;
        }

Dus zonder meer code is het moeilijk te zeggen wat de oorzaak is maar als ik zou moeten gokken dan zou ik zeggen dat het aan de Selectedindex ligt.
 
hoi

ja, heb nu dit:

index The name 'index' does not exist in the current context.

BIj deze regel:

Code:
                        if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)
 
Hoi

hIERBIJ dan nog de volledige classes:

Game1

Code:
//#define SCREENSHOTMODE

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using xCharEdit.Character;
using xCharEdit;
using System.IO;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
//using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HelperClasses;

namespace CharacterEditor
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D arialText;
        //Text text;
        public CharDef charDef;

        List<Texture2D> headTex;
        List<Texture2D> torsoTex;
        List<Texture2D> legsTex;
        List<Texture2D> weaponTex;
        List<Texture2D> manTex;


        Texture2D nullTex;
        //Texture2D iconsTex;

        const int FACE_LEFT = 0;
        const int FACE_RIGHT = 1;

        public int selPart;
        public int selFrame = 0;
        public int selAnim = 0;
        int selKeyFrame;

        int frameScroll;
        int animScroll;
        int keyFrameScroll;

        bool mouseClick;

        MouseState mouseState;
        MouseState preState = Mouse.GetState();

        KeyboardState oldKeyState;
        private IntPtr Handle;

        bool playing;
        int curKey;
        float curFrame;

        #region Const
        const int AUX_SCRIPT = 0;
        const int AUX_TRIGS = 1;
        const int AUX_TEXTURES = 2;

        int auxMode = AUX_SCRIPT;
        int trigScroll = 0;

        const int EDITING_NONE = -1;
        const int EDITING_FRAME_NAME = 0;
        const int EDITING_PATH = 1;
        const int EDITING_ANIMATION_NAME = 2;
        const int EDITING_SCRIPT = 3;

        public const int TRIG_PISTOL_ACROSS = 0;
        public const int TRIG_PISTOL_UP = 1;
        public const int TRIG_PISTOL_DOWN = 2;
        public const int TRIG_WRENCH_UP = 3;
        public const int TRIG_WRENCH_DOWN = 4;
        public const int TRIG_WRENCH_DIAG_UP = 5;
        public const int TRIG_WRENCH_DIAG_DOWN = 6;
        public const int TRIG_WRENCH_UPPERCUT = 7;
        public const int TRIG_WRENCH_SMACKDOWN = 8;
        public const int TRIG_KICK = 9;
        public const int TRIG_ZOMBIE_HIT = 10;

        public const int TRIG_BLOOD_SQUIRT_UP = 11;
        public const int TRIG_BLOOD_SQUIRT_UP_FORWARD = 12;
        public const int TRIG_BLOOD_SQUIRT_FORWARD = 13;
        public const int TRIG_BLOOD_SQUIRT_DOWN_FORNWARD = 14;
        public const int TRIG_BLOOD_SQUIRT_DOWN = 15;
        public const int TRIG_BLOOD_SQUIRT_DOWN_BACK = 16;
        public const int TRIG_BLOOD_SQUIRT_BACK = 17;
        public const int TRIG_BLOOD_SQUIRT_UP_BACK = 18;

        public const int TRIG_BLOOD_CLOUD = 19;
        public const int TRIG_BLOOD_SPLAT = 20;

        public const int TRIG_CHAINSAW_DOWN = 21;
        public const int TRIG_CHAINSAW_UPPER = 22;
        public const int TRIG_ROCKET = 23;
        public const int TRIG_FIRE_DIE = 24;
        #endregion
        int selScriptLine = 0;

        //int editingText = EDITING_NONE;

        const bool screenShotMode = true;
        MainForm f;


        public Game1(IntPtr handle, MainForm form)
        {
            this.f = form;
            this.Handle = handle;
           // Mouse.WindowHandle = this.Handle;

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            System.Windows.Forms.Control.FromHandle((this.Window.Handle)).VisibleChanged += new EventHandler(Game1_VisibleChanged);
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            charDef = new CharDef();
            // Make mouse visible
            this.IsMouseVisible = false;


#if SCREENSHOTMODE
                graphics.PreferredBackBufferWidth = 1280;
                graphics.PreferredBackBufferHeight = 720;
                graphics.ApplyChanges();
#endif

            base.Initialize();
        }

       /*private bool drawButton(int x, int y, int idx, bool mouseClick)
       {
            return drawButton(x, y, idx, mouseClick, 1.0f);
        }*/

        private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = this.Handle; ;
        }

        /*private bool drawButton(int x, int y, int idx, bool mouseClick, float scale)
        {
            bool r = false;

            Microsoft.Xna.Framework.Rectangle sRect = new Microsoft.Xna.Framework.Rectangle(32 * (idx % 8),
                32 * (idx / 8), 32, 32);
            Microsoft.Xna.Framework.Rectangle dRect = new Microsoft.Xna.Framework.Rectangle(x, y,
                (int)(32f * scale),
                (int)(32f * scale));

            if (dRect.Contains(mouseState.X, mouseState.Y))
            {
                dRect.X -= 1;
                dRect.Y -= 1;
                dRect.Width += 2;
                dRect.Height += 2;
                if (mouseClick)
                    r = true;
            }
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            //spriteBatch.Draw(iconsTex, dRect, sRect, Microsoft.Xna.Framework.Graphics.Color.White);
            spriteBatch.End();

            return r;
        }*/

        private void Game1_VisibleChanged(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Control.FromHandle((this.Window.Handle)).Visible == true)
                System.Windows.Forms.Control.FromHandle((this.Window.Handle)).Visible = false;
        }

        /*private void DrawCursor()
        {
            Microsoft.Xna.Framework.Rectangle rect = new Microsoft.Xna.Framework.Rectangle();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            Color tColor = new Color();

            tColor = Color.White;
            //if (GetCanEdit()) tColor = Color.Yellow;

            rect.X = 0;
            rect.Y = 0;
            rect.Width = 32;
            rect.Height = 32;

            //spriteBatch.Draw(iconsTex, new Vector2((float)mouseState.X,
                //(float)mouseState.Y), rect,
               // tColor, 0.0f, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.0f);

            spriteBatch.End();
        }*/

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            arialText = Content.Load<Texture2D>(@"gfx/arial");
            //iconsTex = Content.Load<Texture2D>(@"gfx/icons");
            nullTex = Content.Load<Texture2D>(@"gfx/1x1");
            //text = new Text(arialText, spriteBatch);

            manTex = new List<Texture2D>();
            legsTex = new List<Texture2D>();
            torsoTex = new List<Texture2D>();
            headTex = new List<Texture2D>();
            weaponTex = new List<Texture2D>();

            // laden data



            for (int i = 0; i < (Directory.GetFiles(@"Content/gfx/", "legs*")).Length; i++)
                legsTex.Add(Content.Load<Texture2D>(@"gfx/legs" + (i + 1).ToString()));

            for (int i = 0; i < (Directory.GetFiles(@"Content/gfx/", "torso*")).Length; i++)
                torsoTex.Add(Content.Load<Texture2D>(@"gfx/torso" + (i + 1).ToString()));

            for (int i = 0; i < (Directory.GetFiles(@"Content/gfx/", "head*")).Length; i++)
                headTex.Add(Content.Load<Texture2D>(@"gfx/head" + (i + 1).ToString()));

            for (int i = 0; i < (Directory.GetFiles(@"Content/gfx/", "weapon*")).Length; i++)
                weaponTex.Add(Content.Load<Texture2D>(@"gfx/weapon" + (i + 1).ToString()));

            //Own content
            //for (int i = 0; i < manTex.Length; i++)
            // manTex[i] = Content.Load<Texture2D>(@"gfx/man" + (i + 1).ToString());

            //for (int i = 0; i < legsTex.Length; i++)
            // legsTex[i] = Content.Load<Texture2D>(@"gfx/objects" + (i + 1).ToString());


            //End content

            // TODO: use this.Content to load your game content here
            drawPallete();
            //DrawPartList();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                //this.Exit();
            //Zorgt voor actual annimating.
//            UpdateKeys();

            Animation animation = charDef.GetAnimation(selAnim);
            KeyFrame keyframe = animation.GetKeyFrame(curKey);

            if (playing)
            {
                curFrame += (float)gameTime.ElapsedGameTime.TotalSeconds * 30.0f;

                if (curFrame > (float)keyframe.duration)
                {
                    curFrame -= (float)keyframe.duration;
                    curKey++;
                    keyframe = animation.GetKeyFrame(curKey);

                    if (curKey >=
                        animation.getKeyFrameArray().Count)
                        curKey = 0;
                }
            }
            else
                curKey = selKeyFrame;

            /*if (keyframe.frameRef < 0)
                curKey = 0;*/


            mouseState = Mouse.GetState();


            if (mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (preState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
                {

                }
                else
                {//Function for moving, scaling, rotating the objects.
                    if (CanEdit())
                    {
                        int xM = mouseState.X - preState.X;
                        int yM = mouseState.Y - preState.Y;
                        if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)
                        {
                            try
                            {
                                CanEdit();

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message + "\n");
                            
                            }

                            //if (f == null)
                                //MessageBox.Show("Is null");
                            //new CharDef();
                            charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList[selPart].location +=
                                //charDef.GetFrame(selFrame).GetPart(selPart).location +=
                                new Vector2((float)xM / 2.0f, (float)yM / 2.0f);//Speed where u can move the objects
                        }
                    }
                }
            }
            else
            {
                if (preState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                {
                    mouseClick = true;
                }
            }

            if (mouseState.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (preState.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                {
                    if (CanEdit())
                    {
                        int yM = mouseState.Y - preState.Y;
                        charDef.GetAnimation(selAnim).KeyFrame[selFrame].frameRef.GetPart(selPart).rotation +=
                        //charDef.GetFrame(selFrame).GetPart(selPart).rotation +=
                            (float)yM / 100.0f;
                    }
                }
            }

            if (mouseState.MiddleButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                if (preState.MiddleButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                {
                    if (CanEdit())
                    {
                        int xM = mouseState.X - preState.X;
                        int yM = mouseState.Y - preState.Y;

                        charDef.GetAnimation(selAnim).KeyFrame[selFrame].frameRef.GetPart(selPart).scaling +=
                            new Vector2((float)xM * 0.01f, (float)yM * 0.01f);
                    }
                }
            }//End funtion for moving the objects.

            preState = mouseState;

            base.Update(gameTime);
        }//End method

/*        private void UpdateKeys()
        {
            KeyboardState keystate = new KeyboardState();

            keystate = Keyboard.GetState();

            Microsoft.Xna.Framework.Input.Keys[] currentkeys = keystate.GetPressedKeys();
            Microsoft.Xna.Framework.Input.Keys[] lastkeys = oldKeyState.GetPressedKeys();
            bool found = false;

            for (int i = 0; i < currentkeys.Length; i++)
            {
                found = false;

                for (int y = 0; y < lastkeys.Length; y++)
                {
                    if (currentkeys[i] == lastkeys[y]) found = true;
                }
                if (found == false)
                {
                    PressKey(currentkeys[i]);
                }
            }

            oldKeyState = keystate;
        }

        private void PressKey(Microsoft.Xna.Framework.Input.Keys key)
        {
#if SCREENSHOTMODE
            if (key == Keys.End)
                return;
#endif
            String t = "";
            switch (editingText)
            {
                case EDITING_FRAME_NAME:
                    t = charDef.GetAnimation(selAnim).KeyFrame[selFrame].frameRef.name;
                    break;
                case EDITING_ANIMATION_NAME:
                    t = charDef.GetAnimation(selAnim).name;
                    break;
                case EDITING_PATH:
                    t = charDef.path;
                    break;
                case EDITING_SCRIPT:
                    t = charDef.GetAnimation(selAnim).GetKeyFrame(selKeyFrame).GetScript(selScriptLine);
                    break;
                default:
                    return;
            }

            if (key == Microsoft.Xna.Framework.Input.Keys.Back)
            {
                if (t.Length > 0) t = t.Substring(0, t.Length - 1);
            }
            else if (key == Microsoft.Xna.Framework.Input.Keys.Enter)
            {
                editingText = EDITING_NONE;
            }
            else
            {
                t = (t + (char)key).ToLower();
            }

            switch (editingText)
            {
                case EDITING_FRAME_NAME:
                    charDef.GetFrame(selFrame).name = t;
                    break;
                case EDITING_ANIMATION_NAME:
                    charDef.GetAnimation(selAnim).name = t;
                    break;
                case EDITING_PATH:
                    charDef.path = t;
                    break;
                case EDITING_SCRIPT:
                    charDef.GetAnimation(selAnim).GetKeyFrame(selKeyFrame).SetScript(selScriptLine, t);
                    break;
            }
        }*/

        //Begin method DrawCharacter
        private void DrawCharacter(Vector2 loc, float scale, int face, Frame frame,
            bool preview, float alpha)
        {
            Rectangle sRect = new Rectangle();
            //Frame frame = charDef.GetFrame(frameIdx);

            if (frame != null)
            {
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

                for (int i = 0; i < frame.GetPartArray().Count; i++)
                {
                    Part part = frame.GetPart(i);
                    if (part.idx > -1)
                    {

                        sRect.X = ((part.idx % 64) % 5) * 64; //100;//64;//%5
                        sRect.Y = ((part.idx % 64) / 5) * 64;//100;//64;// /5                    
                        sRect.Width = 80; //80; //64; //Zorgt voor grote van object(X-as) die je kan selecteren!!
                        sRect.Height = 75;//75;//64; //Zorgt voor grote van object(Y-as) die je kan selecteren!!
                        if (part.idx >= 192)
                        {
                            sRect.X = ((part.idx % 64) % 4) * 100;//80;
                            sRect.Y = ((part.idx % 64) / 4) * 100;//64;
                            sRect.Width = 80;
                        }

                        float rotation = part.rotation;


                        Vector2 location = part.location * scale + loc;
                        Vector2 scaling = part.scaling * scale;
                        if (part.idx >= 128) scaling *= 1.35f;//1.35f;//128

                        if (face == FACE_LEFT)
                        {
                            rotation = -rotation;
                            location.X -= part.location.X * scale * 2.0f;
                        }

                        /*if (part.idx >= 1000 && alpha >= 1f)
                        {
                            spriteBatch.End();

                            /*text.Color = Color.Lime;
                            if (preview)
                            {
                                text.Size = 0.45f;
                                text.DrawText((int)location.X,
                                    (int)location.Y,
                                    "*");
                            }
                            else
                            {
                                text.Size = 1f;
                                text.DrawText((int)location.X,
                                    (int)location.Y,
                                    "*" + GetTrigName(part.idx - 1000));
                            }
                            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                        }
                        else
                        {*/
                        //Texture2D texture;//THis function is for the sprite maps, like: head1, head2..etc

                        Texture2D texture = null;


                        int t = part.idx / 128;
                        switch (t)
                        {
                            case 0:
                                texture = headTex[charDef.headIdx];
                                break;
                            case 1:
                                texture = torsoTex[charDef.torsoIdx];
                                break;
                            case 2:
                                texture = legsTex[charDef.legsIdx];
                                break;
                            case 3:
                                texture = weaponTex[charDef.weaponIdx];
                                break;
                            default:
                                texture = null;
                                break;
                        }//End function spriteMap!!
                        //If u clcik on the objet the color of the object will chane
                        Color color = new Color(new
                            Vector4(1.0f, 1.0f, 1.0f, alpha));
                        if (!preview && selPart == i) color = new
                            Color(new Vector4(1.0f, 1.0f, 1.0f, alpha));

                        bool flip = false;

                        if ((face == FACE_RIGHT && part.flip == 0) ||
                            (face == FACE_LEFT && part.flip == 1)) flip = true;

                        if (texture != null)
                        {

                        }
                        spriteBatch.Draw(texture, location, sRect,
                                color, rotation, new Vector2(
                                (float)sRect.Width / 2f, 32.0f),
                                scaling, (flip ?
                                SpriteEffects.None : SpriteEffects.FlipHorizontally),
                                1.0f);

                    }

                }
                spriteBatch.End();
            }//End fucntion

        }



/*        private void SwapParts(int idx1, int idx2)
        {
            if (idx1 < 0 || idx2 < 0 ||
                idx1 >= charDef.GetFrame(selFrame).GetPartArray().Count ||
                idx2 >= charDef.GetFrame(selFrame).GetPartArray().Count)
                return;

            Part t = new Part();
            Part i = charDef.GetFrame(selFrame).GetPart(idx1);
            Part j = charDef.GetFrame(selFrame).GetPart(idx2);

            t.idx = i.idx;
            t.location = i.location;
            t.rotation = i.rotation;
            t.scaling = i.scaling;
            t.flip = i.flip;

            i.idx = j.idx;
            i.location = j.location;
            i.rotation = j.rotation;
            i.scaling = j.scaling;
            i.flip = j.flip;

            j.idx = t.idx;
            j.location = t.location;
            j.rotation = t.rotation;
            j.scaling = t.scaling;
            j.flip = t.flip;
        }*/

        /*private void CopyFrame(int src, int dest)
        {

            Frame keySrc = charDef.GetFrame(src);
            Frame keyDest = charDef.GetFrame(dest);

            keyDest.name = keySrc.name;
            for (int i = 0; i < keyDest.GetPartArray().Count; i++)
            {
                Part srcPart = keySrc.GetPart(i);
                Part destPart = keyDest.GetPart(i);

                destPart.idx = srcPart.idx;
                destPart.location = new Vector2(srcPart.location.X,
                    srcPart.location.Y);
                destPart.rotation = srcPart.rotation;
                destPart.scaling = new Vector2(srcPart.scaling.X,
                    srcPart.scaling.Y);
                destPart.flip = srcPart.flip;

            }
        }*/

        private bool CanEdit()
        {
            if (mouseState.X > 0 && mouseState.Y > 0 && mouseState.X <
                f.panel1.Height && mouseState.Y < f.panel1.Width)
                return true;
            else
                return false;
        }
        //Ouput Text messages 
        private string GetTrigName(int idx)
        {
            switch (idx)
            {

                case TRIG_WRENCH_UP:
                    return "wrench up";
                case TRIG_WRENCH_UPPERCUT:
                    return "wrench uppercut";
                case TRIG_KICK:
                    return "kick";
                case TRIG_ZOMBIE_HIT:
                    return "zombie hit";

                case TRIG_BLOOD_SQUIRT_UP:
                    return "squirt up";
                case TRIG_BLOOD_SQUIRT_UP_FORWARD:
                    return "squirt upforward";
                case TRIG_BLOOD_SQUIRT_FORWARD:
                    return "squirt forward";
                case TRIG_BLOOD_SQUIRT_DOWN_FORNWARD:
                    return "squirt downforward";
                case TRIG_BLOOD_SQUIRT_DOWN:
                    return "squirt down";
                case TRIG_BLOOD_SQUIRT_DOWN_BACK:
                    return "squirt downback";
                case TRIG_BLOOD_SQUIRT_BACK:
                    return "squirt back";
                case TRIG_BLOOD_SQUIRT_UP_BACK:
                    return "squirt upback";

                case TRIG_BLOOD_CLOUD:
                    return "blood cloud";
                case TRIG_BLOOD_SPLAT:
                    return "blood splat";

                case TRIG_CHAINSAW_DOWN:
                    return "chain down";
                case TRIG_CHAINSAW_UPPER:
                    return "chain up";
                case TRIG_ROCKET:
                    return "rocket";
                case TRIG_FIRE_DIE:
                    return "firedie";
            }
            return "";
        }//End method

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            //if (mouseClick)
              //  editingText = EDITING_NONE;

           
            Frame currentFrame = charDef.GetAnimation(selAnim).KeyFrame[selFrame].frameRef;
            if (selFrame > 0)
                DrawCharacter(new Vector2(400f, 450f), 2f, FACE_RIGHT,
                   currentFrame, false, 0.2f);

            if (selFrame < charDef.GetAnimation(selAnim).KeyFrame.Count)
                DrawCharacter(new Vector2(400f, 450f), 2f, FACE_RIGHT,
                    currentFrame, false, 0.2f);

            DrawCharacter(new Vector2(400f, 450f), 2f, FACE_RIGHT, currentFrame,
                false, 1.0f);

                
            
            mouseClick = false;
            base.Draw(gameTime);
        }

        //This is to draw the objects in the pallete
        #region Draw item pallete to screen
        public void drawPallete(/*String[] names*/)
        {
            for (int j = 0; j < 4; j++)
            {
                //int i = 0;
                ImageList im = new ImageList();
                List<Texture2D> currentList = null;

                switch (j)
                {
                    case 0:
                        currentList = headTex;
                        f.ItemTabControl.TabPages.Add("Heads");
                        break;
                    case 1:
                        currentList = torsoTex;
                        f.ItemTabControl.TabPages.Add("Torso");
                        break;
                    case 2:
                        currentList = legsTex;
                        f.ItemTabControl.TabPages.Add("Legs");
                        break;
                    default:
                        currentList = weaponTex;
                        f.ItemTabControl.TabPages.Add("Weapons");
                        break;
                }

                FlowLayoutPanel l = new FlowLayoutPanel();
                l.AutoScroll = true;

                //paletteView.Add(l);
                //f.ItemTabControl.TabPages


                f.ItemTabControl.TabPages[j].Controls.Add(l);
                l.Dock = DockStyle.Fill;

                foreach (Texture2D sd in currentList)
                {

                    if (sd == null)
                        continue;

                    for (int i = 0; i < 25; i++)
                    {
                        Rectangle sRect = new Rectangle((i % 5) * 64,
                            (i / 5) * 64, 64, 64);
                        Button b = new Button();
                        System.Drawing.Bitmap bmp = GetButtonImage(sRect, sd);
                        b.Image = resizeImage(bmp, new System.Drawing.Size(64, 64));

                        b.Padding = new Padding(0);

                        b.Size = new System.Drawing.Size(64, 64);
                        b.Tag = i;
                        // click event
                        b.Click += new EventHandler(AddCharSegment);
                        //
                        l.Controls.Add(b);
                        //i++;
                    }
                }
            }
        }

        private System.Drawing.Bitmap GetButtonImage(Microsoft.Xna.Framework.Rectangle tile, Microsoft.Xna.Framework.Graphics.Texture2D tileTex)
        {
            int[] data = new int[tile.Width * tile.Height];
            tileTex.GetData<int>(0, tile, data, 0, tile.Width * tile.Height);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tile.Width, tile.Height);
            for (int x = 0; x < tile.Width; x++)
            {
                for (int y = 0; y < tile.Height; y++)
                {
                    System.Drawing.Color bitmapColor = System.Drawing.Color.FromArgb(data[y * tile.Width + x]);
                    bitmap.SetPixel(x, y, bitmapColor);
                }
            }
            return bitmap;
        }

        private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, System.Drawing.Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            System.Drawing.Bitmap b = new System.Drawing.Bitmap(destWidth, destHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (System.Drawing.Image)b;
        }

        private void AddCharSegment(object sender, EventArgs e)
        {
            int index = (int)((Button)sender).Tag;
            int selFrame = 0;
            charDef.GetAnimation(selAnim).KeyFrame[selFrame].frameRef.partList.Add(new Part(index + 64 * 1));
            //charDef.frame[selFrame].partList.Add(new Part(index + 64 * 1));
            //charDef.frame[selFrame].partList[selPart].idx = 
            //DrawPartList();
            //charDef.SetFrame(index
        }
        #endregion

        
        public void DrawPartList()
        {
            
            f.FrameList.Items.Clear();
            for (int i = 0; i < charDef.GetFrame(selFrame).GetPartArray().Count; i++)
            {

                String line = "-";
                int idx = charDef.GetFrame(selFrame).GetPart(i).idx;
                if (idx < 0)
                    line = "-";
                else if (idx < 64)
                    line = "head" + idx.ToString();
                else if (idx < 74)
                    line = "torso" + idx.ToString();
                else if (idx < 128)
                    line = "arms" + idx.ToString();
                else if (idx < 192)
                    line = "legs" + idx.ToString();
                else if (idx < 1000)
                    line = "weapon" + idx.ToString();
                else
                    line = GetTrigName(idx - 1000);
                f.FrameList.Items.Add(line);
            }
              
        }//End method
          
    }
}

en de classe: Chardef:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
//Classe for save and open files(This means, what u have made already)
namespace xCharEdit.Character
{
    public class CharDef
    {
        List<Animation> animation;
        public Frame[] frame;
        public String path;

        public int headIdx;
        public int torsoIdx;
        public int legsIdx;
        public int weaponIdx;

        public CharDef()
        {
            animation = new List<Animation>();
            //for (int i = 0; i < animation.Length; i++)
                //animation[i] = new Animation();
            /*frame = new Frame[512];
            for (int i = 0; i < frame.Length; i++)
                frame[i] = new Frame();*/
            Animation a = new Animation(666);
            animation.Add(a);
            path = "hallo";
        }//End method

        public Animation GetAnimation(int idx )
        {
            //for (int i = 0;i < animation.Count;i++)
                //new CharDef();
                //new Animation();
                return (idx >= 0) ? animation[idx] : null;
            return null;

            if (idx == 0)
                MessageBox.Show("is null");

            //try
            //{
                // problem is null, and ProblemProperty cannot be called
                // on a null reference
                //int animation = getAnimation.ProblemProperty;
            //}
            //catch (NullReferenceException nre)
            //{
                //Console.WriteLine(
                    //"Cannot read ProblemProperty, problem is null.\n" +
                   // nre.Message);
           // }




        }

       

        public void SetAnimation(int idx, Animation _animation)
        {
            animation[idx] = _animation;
        }

        public List<Animation> GetAnimationArray()
        {
            return animation;
        }

        public Frame GetFrame(int idx)
        {
            return frame[idx];
        }

        /*
        public void SetFrame(int idx, Frame _frame)
        {
            frame[idx] = _frame;
        }

        public Frame[] GetFrameArray()
        {
            return frame;
        }*/

        /*public void WriteBackup()
        {
            Write(@"data/" + path); //+ ".zmx");
            Write(@"../../../../CharacterEditor/Debug/char" + path);//  data/chars/" + path); //+ ".zmx");
        }

        public void Write()
        {
            Write(@"data/" + path); //+ ".zmx");
        }

        public void Write(String writePath)
        {
            BinaryWriter b = new BinaryWriter(File.Open(writePath,
                FileMode.Create));

            b.Write(path);
            b.Write(headIdx);
            b.Write(torsoIdx);
            b.Write(legsIdx);
            b.Write(weaponIdx);

            for (int i = 0; i < animation.Length; i++)
            {
                b.Write(animation[i].name);

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    b.Write(keyframe.frameRef);
                    b.Write(keyframe.duration);
                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        b.Write(script[s]);
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {
                b.Write(frame[i].name);

                for (int j = 0; j < frame[i].GetPartArray().Count; j++)
                {
                    Part p = frame[i].GetPart(j);
                    b.Write(p.idx);
                    b.Write(p.location.X);
                    b.Write(p.location.Y);
                    b.Write(p.rotation);
                    b.Write(p.scaling.X);
                    b.Write(p.scaling.Y);
                    b.Write(p.flip);
                }
            }

            b.Close();

            Console.WriteLine("Saved.");
        }//End method Save

        public void Read()
        {
            BinaryReader b;
            try
            {
                b = new BinaryReader(File.Open(@"data/" + path + ".zmx",
                    FileMode.Open, FileAccess.Read));
            }
            catch (Exception e)
            {
                return;
            }

            path = b.ReadString();
            headIdx = b.ReadInt32();
            torsoIdx = b.ReadInt32();
            legsIdx = b.ReadInt32();
            weaponIdx = b.ReadInt32();
            

            for (int i = 0; i < animation.Length; i++)
            {   
                animation[i].name = b.ReadString();

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    keyframe.frameRef = b.ReadInt32();
                    keyframe.duration = b.ReadInt32();

                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        script[s] = b.ReadString();
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {   
                frame[i].name = b.ReadString();

                for (int j = 0; j < frame[i].GetPartArray().Length; j++)
                {
                    Part p = frame[i].GetPart(j);
                    p.idx = b.ReadInt32();
                    p.location.X = b.ReadSingle();
                    p.location.Y = b.ReadSingle();
                    p.rotation = b.ReadSingle();
                    p.scaling.X = b.ReadSingle();
                    p.scaling.Y = b.ReadSingle();
                    p.flip = b.ReadInt32();
                }
            }

            b.Close();

            Console.WriteLine("Loaded.");
        }*/
    }
}
 
hi

Code:
f.AnimationBox.SelectedIndex = -1

dit is wat ik zie bij deze regel:

Code:
                        if (charDef.GetAnimation(f.AnimationBox.SelectedIndex).GetKeyFrame(f.FrameList.SelectedIndex).frameRef.partList.Count > selPart)

Maar hoe los je nu dit op?
 
hi

Dit is de code van de AnimationBox:

Code:
 private void ChangeAnimation(object sender, EventArgs e)
        {
            if (AnimationBox.SelectedItem != null)
            {
                game.selAnim = AnimationBox.SelectedIndex;
                FrameList.DataSource = ((Animation)AnimationBox.SelectedItem).KeyFrame;
            }
        }
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan