oyyou's Profile User Rating: -----

Reputation: 27 Craftsman
Group:
Active Members
Active Posts:
192 (0.11 per day)
Joined:
26-April 10
Profile Views:
5,222
Last Active:
User is offline Nov 02 2014 09:13 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
0
Icon   oyyou has not set their status

Posts I've Made

  1. In Topic: How to make a jumping animation

    Posted 29 Oct 2014

    Nope. No. No.

    public class Animation
    {
      Texture2D texture;
      public Vector2 Position; // Because I'm too lazy to put in ctor
      int frameWidth;
      int frameHeight;
      int frameCount;
      int xPos;
      float timer = 0;
      public bool IsActive;
    
      public Animation(Texture2D texture, int frameWidth, int frameHeight)
      {
        this.texture = texture;
        this.frameWidth = frameWidth;
        this.frameHeight = frameHeight;
        this.frameCount = texture.Width / frameWidth;
      }
    }
    
    public void Update(GameTime gameTime)
    {
      if(!IsActive)
        return;
    
      timer += (float)gameTime.TotalElapsedTime.TotalSeconds; // Something like that. I don't have XNA on me right now
    
      if(timer >= 0.2f)
      {
        timer = 0;
        xPos += frameWidth;
        if(frameWidth * xPos > frameCount - 1)
        {
          xPos = 0;
        }
      }
    }
    
    public void Draw(SpriteBatch spriteBatch, bool right)
    {
      if(!IsActive)
        return;
    
      SpriteEffects effect = SpriteEffects.None;
      if(right)
        effect = SpriteEffects.FlipHorizontally;
    
      spriteBatch.Draw(texture, Position, new Rectangle(xPos, 0, frameWidth, frameHeight), Color.White, 0, new Vector2(0, 0), 1f, effect, 0);
    }
    
    


    Game Class
    Animation walk, jump;
    
    LoadContent
    {
      walk = new Animation(Content.Load<Texture2D>("WalkAnimation"), 10, 10);
      jump = new Animation(Content.Load<Texture2D>("JumpAnimation"), 10, 10);
    }
    
    Update
    {
      if(walking)
      {
        walk.IsActive = true;
        jump.IsActive = false;
      } else if(jumping)
      {
        walk.IsActive = false;
        jump.IsActive = true;
      }
    
      walk.Update(gameTime);
      jump.Update(gameTime);
    }
    
    Draw
    {
      spriteBatch.Begin();
      walk.Draw(spriteBatch);
      jump.Draw(spriteBatch);
      spriteBatch.End();
    }
    
    


    A crude implementation, but much better than whatever you've got going on.
    http://www.youtube.c...h?v=41xnVjJsh7M
  2. In Topic: DispatcherTimer - Seconds gone by

    Posted 28 Oct 2014

    View PosttlhIn`toq, on 28 October 2014 - 04:16 PM, said:

    Lets talk about some clean-up.

    A double named "Timer". Really? It isn't a timer is it? How about naming it "SecondsRemaining" so its named what its for?

    Each GameButton instance has its own DispatchTimer. Hmmm... That means they could all be working out of sync with each other if their timers start and stop independently. Is that intentional and needed?

    Personally I'd make one Program scoped timer and have it raise a "HeartBeat" event every 1000ms. That will give you a Heartbeat event every one 1 second that all other classes can sync on. Your GameButton class can subscribe to the event and just count down without having the overhead of its own timer. More important it should be a thread of its own so it isn't thrown off by lots of activity on a shared thread as you are now.

    This is WPF so don't carry over bad WinForms habits like this:
    border.Visibility = System.Windows.Visibility.Collapsed;
    


    Bind the Visibility to the CoolDown property using a converter. It should all happen in XAML and none of this should be happening in the code-behind. When CoolDown is true the control is visible, when CoolDown is false the control is Collapsed: All with no C# code behind.

    Same problem with this:
    textBlock.Text = string.Format("{0:N2}", (timer));
    

    You don't set the text in the code behind, you bind the text to a property such as "string TimerStatus". Then you update the property but you DO NOT directly affect the GUI items from the code behind: It totally violated the foundation concept of WPF binding to object values.

    I suggest you take some time to read up on WPF concepts. Because what you have here is badly formed WinForms design, that just happens to be using WPF to create the GUI - but in no way would this be considered an actual WPF application.


    Hey, man. I appreciate the advice, and I'm sure I will listen to some of it, but the stuff you are mentioning aren't big deals in the grand scheme of things.
    I've never managed to finish a project because I want to have 'perfect' code. Yes, I know I'm using some bad habits from forms when I could be binding, but I spent 3 days trying to create my button through a style, and for the life of my couldn't do it.

    By the end of the project I may come back and sort the little things out, but for now I just want to finish and hopefully (fingers crossed) release it.

    As for the "SecondsRemaining" in the button. I do want them working independently.
  3. In Topic: DispatcherTimer - Seconds gone by

    Posted 28 Oct 2014

    So I had a revelation while showering.
    The reason I had the ticks on so low was because I was trying to account for multiple buttons.
    Instead of being a dumb dumb, I should have had the dispatchTimer in the actual button class. Doy!

      public class GameButton : Button
      {
        private double timer;
        public bool OnCooldown;
    
        private GameView gameView;
        private int unitNumber;
    
        private TextBlock textBlock;
    
    
        private DispatcherTimer dispatcherTimer = new DispatcherTimer();
    
    
        public GameButton(GameView gameView, int unitNumber, string imageSource)
        {
          this.gameView = gameView;
          this.unitNumber = unitNumber;
    
          textBlock = new TextBlock();
    
          this.Click += GameButton_Click;
          dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
          dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
        }
    
        public void Update()
        {
          if (timer > 0)
          {
            textBlock.Text = string.Format("{0:N2}", (timer));
            timer -= 0.1d;
          }
          else
          {
            OnCooldown = false;
            border.Visibility = System.Windows.Visibility.Collapsed;
            dispatcherTimer.Stop();
            timer = 0;
          }
        }
    
        void GameButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
          if (OnCooldown)
            return;
    
          timer = 4;
          textBlock.Text = string.Format("{0:N2}", (timer));
    
          OnCooldown = true;
          border.Visibility = System.Windows.Visibility.Visible;
          gameView.RecruitUnit(unitNumber);
    
          dispatcherTimer.Start();
        }
    
        void DispatcherTimer_Tick(object sender, EventArgs e)
        {
          this.Update();
        }
      }
    
    


    I have now updated that and it's giving much better results already.

    Although it isn't 4 seconds like I would have hoped, I'm okay with it. There are plenty of games with cooldowns that look like seconds, but aren't (Dota 2 for example).

    I'm happen with what I've got.

    Thanks for the help, andrewsw :)
  4. In Topic: DispatcherTimer - Seconds gone by

    Posted 28 Oct 2014

    View Postandrewsw, on 28 October 2014 - 03:09 PM, said:

    If Timer has the value 4 and this represents seconds then I would change it to 4000 (to represent ms) and subtract 33 each time.

    BTW 33ms is a very short period of time. Attempting to update the UI (TextBox.Text) every 33ms is likely to be problematic or, at the least, cause flicker.


    Good news, doesn't appear to cause flicker.
    Bad news, it's way more than 4 seconds. (more like 7.3s)

    Hmm. Odd. I'm going to bed now, and will pick this up in the morning.

    If anybody is interested, my 2 classes are below

      public class GameButton : Button
      {
        public double Timer = 0;
        public bool OnCooldown;
    
        private GameView gameView;
        private int unitNumber;
    
        private Grid grid;
        private Image image;
        private Border border;
        private TextBlock textBlock;
    
    
        public GameButton(GameView gameView, int unitNumber, string imageSource)
        {
          this.gameView = gameView;
          this.unitNumber = unitNumber;
    
          grid = new Grid();
          image = new Image();
          border = new Border();
          textBlock = new TextBlock();
    
          this.Width = 100;
          this.Height = 100;
          this.Padding = new System.Windows.Thickness(0);
          this.VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;
          this.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
    
          //image.Source = new BitmapImage(new Uri(@"/WPF_TestApp;component/" + imageSource, UriKind.Relative));
          image.Source = new BitmapImage(new Uri(imageSource, UriKind.Relative));
    
          border.Background = new SolidColorBrush(Colors.Gray);
          border.Visibility = System.Windows.Visibility.Collapsed;
    
          textBlock.TextAlignment = System.Windows.TextAlignment.Center;
          textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
          textBlock.Foreground = new SolidColorBrush(Colors.LightGray);
    
          border.Child = textBlock;
    
          grid.Children.Add(image);
          grid.Children.Add(border);
    
          this.Content = grid;
    
          this.Click += GameButton_Click;
        }
    
        public void Update()
        {
          Timer -= 33d; // <-- What do? O:
          if (Timer > 0)
          {
            textBlock.Text = string.Format("{0:N2}", (Timer / 1000));// Timer.ToString("D2");
          }
          else
          {
            OnCooldown = false;
            border.Visibility = System.Windows.Visibility.Collapsed;
          }
        }
    
        void GameButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
          if (OnCooldown)
            return;
    
          OnCooldown = true;
          Timer = 4000;
          border.Visibility = System.Windows.Visibility.Visible;
        }
      }
    
    


      public partial class GameView : PhoneApplicationPage
      { 
        List<GameButton> gameButtons = new List<GameButton>();
    
        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    
        public GameView()
        {
          InitializeComponent();
    
          this.DataContext = new Playing_ViewModel();
    
          dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
          dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 33);
          dispatcherTimer.Start();
    
          gameButtons.Add(new GameButton(this, 1, "../Content/HoboOne.png"));
          gameButtons.Add(new GameButton(this, 4, "../Content/HoboFour_Icon.png"));
    
          foreach (GameButton gameButton in gameButtons)
          {
            stackPanel.Children.Add(gameButton);
          }
        }
    
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
          foreach (GameButton gameButton in gameButtons)
          {
            if (gameButton.OnCooldown)
            {
              gameButton.Update();
            }
          }
        }
      }
    
    
  5. In Topic: DispatcherTimer - Seconds gone by

    Posted 28 Oct 2014

    View Postandrewsw, on 28 October 2014 - 02:20 PM, said:

    Please describe what Timer is for? What does it represent? I assume it is just a Double but is it, for example, supposed to display an elapsed time? Elapsed since when?


    Okay, when I click a button, MyButton.Timer = 4. That 4 is to indicate the amount of seconds are left to cooldown (before the button can be pressed again).

    If you see, I deduct an amount from the timer each time the "Update" method in called.
    The update method is called every 33ms from when the button is clicked, and will be until "Timer" is less than 0.

    I'm sure what I'm looking for isn't at all complicated, it's when it comes to time my brain turns to mush.

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Programming Languages:
C#, C++, Java

Contact Information

E-mail:
Click here to e-mail me

Friends

Comments

Page 1 of 1
  1. Photo

    Shinobi411 Icon

    23 Feb 2013 - 19:52
    Hey, I just saw your post about looking for a project. I might have just the thing but are you looking to deal with strictly xna? My team and I are currently working between Unity and Indielib. If you'd like some more information feel free to email us at Ondayo.Studios@me.com That way we will be able to discuss in further detail if you are up to helping us out. We would greatly appreciate ...
Page 1 of 1