# Using namespaces and time dispatcher for images animation (WPF C#)



## MrSeanKon (Dec 6, 2012)

The first attachment includes a non ultimate code; imagine that you have to animate 10 cards => 10 different time dispatchers??? => this sucks! 
But I uploaded it just to show you how we can bypass this problem by reusing code.
Yeap the solution is an extra namespace. Someone may say _why don't you write an extra class inside the major (main) namespace?_
Well it is possible but I prefer to avoid it. Cos we must use static functions and variables and this is an extra headache.
Download the files and let's discuss


----------



## GSquadron (Dec 6, 2012)

Ok, you mean adding namespaces will add animation to the cards?
And you had only 4 card animations, which means 4 namespaces?
Nice work though

Why don't you add a function for the namespaces?
Or make a single namespace for 10 namespaces


----------



## MrSeanKon (Dec 6, 2012)

No, total namespaces are two. The main (its name is Game) and the secondary (its name is paixnidi => παιχνίδι = means game and pronounced paixnidi).
Try to download the files first. Have you got Visual Studio?
If not, it is not a problem => open with Notepad MainWindow.xaml.cs files.


----------



## GSquadron (Dec 6, 2012)

I have already downloaded
This is why i said 4 card animations


----------



## FordGT90Concept (Dec 6, 2012)

Uh...there's absolutely no reason to use two namespaces in your case.  I moved "attempt" class into the "Game" namespace, removed all references to "paixnidi" and it works fine:


Spoiler: code





```
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;


namespace Game
{
    class attempt
    {
        int delay;
        double x, y, X, Y;
        Image card;
        DispatcherTimer Timer;

        public void move(int lag, double startX, double startY, double endX, double endY, Image karta)
        {
            delay = lag;
            x = startX;
            y = startY;
            X = endX;
            Y = endY;
            card = karta;
            Timer = new DispatcherTimer();
            Timer.Tick += new EventHandler(tick);
            Timer.Interval = new TimeSpan(0, 0, delay);
            Timer.Start();
        }
        void animation()
        {
            if (delay == 0)
                delay = 1;
            ThicknessAnimation myanimation = new ThicknessAnimation(new Thickness(x, y, 0, 0), new Thickness(X, Y, 0, 0),
                  TimeSpan.FromSeconds(delay));
            card.BeginAnimation(Image.MarginProperty, myanimation);
        }
        void tick(object sender, EventArgs e)
        {
            Timer.Stop();
            Timer = null;
            if (delay > 1)
                delay = 1;
            animation();
        }
    }
  public partial class MainWindow:Window
  {
      public MainWindow()
      {
         InitializeComponent();         
      }      
      private void button1_Click(object sender, RoutedEventArgs e)
      {
         attempt card1=new attempt();
         attempt card2=new attempt();
         attempt card3=new attempt();
         attempt card4=new attempt();
         
         card1.move(0,image1.Margin.Left,image1.Margin.Top,image5.Margin.Left,image5.Margin.Top,image1);
         card2.move(1,image2.Margin.Left,image2.Margin.Top,image6.Margin.Left,image6.Margin.Top,image2);
         card3.move(2,image3.Margin.Left,image3.Margin.Top,image7.Margin.Left,image7.Margin.Top,image3);
         card4.move(3,image4.Margin.Left,image4.Margin.Top,image8.Margin.Left,image8.Margin.Top,image4); 
         button1.Visibility=Visibility.Hidden;
      }      
   }
}
```



Namespaces are only to avoid collisions of same-named classes.  For example, you might have zero-terminated String class under a network namespace while having a standard String class under a text namespace:
Text.String
Network.String

Different classes, same name.


You should only have one dispatcher handle all animations.  If more than one animation needs to be done, they need to be done in order.  Use a System.Collections.Generic.Queue<> for example.


----------



## MrSeanKon (Dec 6, 2012)

> Uh...there's absolutely no reason to use two namespaces in your case.  I moved "attempt" class into the "Game" namespace, removed all references to "paixnidi" and it works fine.


Readers download the new attachment.



> Namespaces are only to avoid collisions of same-named classes. For example, you might have zero-terminated String class under a network namespace while having a standard String class under a text namespace:
> Text.String
> Network.String
> 
> Different classes, same name.


Although I am new to OOP (object orient programming) I knew that. But I was too nervous and hurried for almost 2 days  and I would to solve my issue as soon as possible. That's why I searched to many pages *and this one* confused me enough, because it says about static functions...
I should not hurry 



> You should only have one dispatcher handle all animations. If more than one animation needs to be done, they need to be done in order.  Use a System.Collections.Generic.Queue<> for example.


Why?
What's the problem?


----------

