I am following CodeingMadEasy tutorial 6 (ImageEffect)
My code has no any error but it not working (not fading) like the tutorial.
This is my Class;
ImageEffect.cs
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;
using Microsoft.Xna.Framework.Content;
namespace Game1
{
public class ImageEffect
{
protected Image image;
public bool IsActive;
public ImageEffect()
{
IsActive = false;
}
public virtual void LoadContent(ref Image Image)
{
this.image = Image;
}
public virtual void UnloadContent()
{
}
public virtual void Update(GameTime gameTime)
{
}
}
}
FadeEffect.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using System.Diagnostics;
namespace Game1
{
public class FadeEffect : ImageEffect
{
public float FadeSpeed;
public bool Increase;
public FadeEffect()
{
FadeSpeed = 1;
Increase = false;
}
public override void LoadContent(ref Image Image)
{
base.LoadContent(ref Image);
}
public override void UnloadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
Debug.WriteLine("FadeEffect Update");
base.Update(gameTime);
if (image.IsActive)
{
if (!Increase)
image.Alpha -= FadeSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
image.Alpha += FadeSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (image.Alpha < 0.0f)
{
Increase = true;
image.Alpha = 0.0f;
}
else if (image.Alpha > 1.0f)
{
Increase = false;
image.Alpha = 1.0f;
}
}
else
image.Alpha = 1.0f;
}
}
}
Image.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
namespace Game1
{
public class Image
{
public float Alpha;
public string Text, FontName, Path;
public Vector2 Position, Scale;
public Rectangle SourceRect;
Dictionary<string, ImageEffect> effectList;
public string Effects;
public bool IsActive;
[XmlIgnore]
public Texture2D Texture;
Vector2 origin;
ContentManager content;
RenderTarget2D renderTarget;
SpriteFont font;
public FadeEffect FadeEffect;
void SetEffect<T>(ref T effect)
{
if (effect == null)
{
effect = (T)Activator.CreateInstance(typeof(T));
}
else
{
(effect as ImageEffect).IsActive = true;
var obj = this;
(effect as ImageEffect).LoadContent(ref obj);
}
effectList.Add(effect.GetType().ToString().Replace("Game1.", ""), (effect as ImageEffect));
}
public void ActivateEffect(String effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = true;
var obj = this;
effectList[effect].LoadContent(ref obj);
}
}
public void DeactivateEffect(string effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = false;
effectList[effect].UnloadContent();
}
}
public Image()
{
Path = Effects = String.Empty;
Text = String.Empty;
FontName = "Fonts/Arival";
Position = Vector2.Zero;
Scale = Vector2.One;
Alpha = 1.0f;
SourceRect = Rectangle.Empty;
effectList = new Dictionary<string, ImageEffect>();
}
public void LoadContent()
{
content = new ContentManager(
ScreenManager.Instance.Content.ServiceProvider, "Content");
if (Path != String.Empty)
Texture = content.Load<Texture2D>(Path);
font = content.Load<SpriteFont>(FontName);
Vector2 dimensions = Vector2.Zero;
if (Texture != null)
dimensions.X += Texture.Width;
dimensions.X += font.MeasureString(Text).X;
if (Texture != null)
dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
else
dimensions.Y = font.MeasureString(Text).Y;
if (SourceRect == Rectangle.Empty)
SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);
renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice,
(int)dimensions.X, (int)dimensions.Y);
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
ScreenManager.Instance.SpriteBatch.Begin();
if (Texture != null)
ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.End();
Texture = renderTarget;
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
//Start Image Effect
SetEffect<FadeEffect>(ref FadeEffect);
if(Effects != String.Empty)
{
string[] split = Effects.Split(':');
foreach(string item in split)
ActivateEffect(item);
}
}
public void UnloadContent() {
content.Unload();
foreach (var effect in effectList)
DeactivateEffect(effect.Key);
}
public void Update(GameTime gameTime)
{
foreach (var effect in effectList)
{
if (effect.Value.IsActive)
effect.Value.Update(gameTime);
}
}
public void Draw(SpriteBatch spriteBatch)
{
origin = new Vector2(SourceRect.Width / 2,SourceRect.Height/2);
spriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White * Alpha,
0.0f, origin, Scale, SpriteEffects.None, 0.0f);
}
}
}
I have debug it.
And i found effect.Value.IsActive from following method is always false. So this method never call effect.Value.Update(gameTime);
public void Update(GameTime gameTime)
{
foreach (var effect in effectList)
{
if (effect.Value.IsActive)
effect.Value.Update(gameTime);
}
}
I am new MonoGame dev. I have no idea about it.
Anyone have the same issue and how to fix it?