1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

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

Discussion in 'Programming & Webmastering' started by MrSeanKon, Dec 6, 2012.

  1. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    The first attachment includes a non ultimate code; imagine that you have to animate 10 cards => 10 different time dispatchers??? => this sucks! :laugh:
    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 :)

    Attached Files:

  2. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.91/day)
    Thanks Received:
    304
    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 :)
  3. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    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.
  4. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.91/day)
    Thanks Received:
    304
    I have already downloaded
    This is why i said 4 card animations
  5. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,343 (6.31/day)
    Thanks Received:
    3,362
    Location:
    IA, USA
    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:
    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 says thanks.
    Crunching for Team TPU
  6. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    Readers download the new attachment.

    Although I am new to OOP (object orient programming) I knew that. But I was too nervous and hurried for almost 2 days :confused: 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 :D

    Why?
    What's the problem?

    Attached Files:

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page