Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:38:48+00:00 2026-06-01T11:38:48+00:00

I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;

  • 0

I have this code:

        public class Area
{
    Texture2D point;
    Rectangle rect;
    SpriteBatch _sB;
    GameTimer _gt;
    int xo, yo, xt, yt;
    //List<Card> _cards;

    public Area(Texture2D point, SpriteBatch sB)
    {
        this.point = point;
        this._sB = sB;
        xt = 660;
        yt = 180;
        xo = 260;
        yo = 90;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        rect = new Rectangle(660, 180, 80, 120);
        spriteBatch.Draw(point, rect, Color.White);

        _gt = new GameTimer();
        _gt.UpdateInterval = TimeSpan.FromSeconds(0.1);
        _gt.Draw += OnDraw;
    }

    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        this.pass(xo, yo);
        if (xo != xt) xo += (xt > xo) ? 10 : -10;
        if (yo != yt) yo += (yt > yo) ? 10 : -10;
    }

    public void pass(int x, int y)
    {
        rect = new Rectangle(x, y, 80, 120);
        _sB.Draw(point, rect, Color.Black);
    }
}

So, I can’t understand what’s wrong. And It’s my first project with XNA, and because of it there can be stupid mistake 🙂

P.S. Sorry. There is a rectangle with coordinates (xt,yt), and I need the animation to move the rectangle to (xo,yo)

P.P.S. I added the full class with corrections, because I don’t understand my mistake.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:38:50+00:00Added an answer on June 1, 2026 at 11:38 am

    You are drawing the entire animation in one frame.. .you should call Pass with diferent x,y from OnDraw…

    EDITED:

    1) You don’t need the timer, the draw method in game class is by default called 60 frames per second…

    2) The Seconds parameter should be calculated as (float) gametime.ElapsedTime.TotalSeconds;

    float time;
    int xt=660, yt=180;
    int xo=260, yo=90;
    
    public void Draw(SpriteBatch spriteBatch, float Seconds)
    {
        rect = new Rectangle(660, 180, 80, 120);
        spriteBatch.Draw(point, rect, Color.White);
    
        this.pass(xo, yo, spriteBatch);
        time+= Seconds;
        if (time>0.3)
        {
            if (xo!=xt) xo+= (xt>xo) ? 10 : -10;
            if (yo!=yt) yo+= (yt>yo) ? 10 : -10;
            time = 0;
        }
    }
    
    public void pass(int x, int y, spritebatch sb)
    {
        rect = new Rectangle(x, y, 80, 120);
        sb.Draw(point, rect, Color.Red);
    }
    

    As you should know this animation will move in a rough mode… if you want to move your sprite smoothly… you can use a Vector2 for your positions and a float for your speed;

    Vector2 Origin = new Vector2(260, 90);
    Vector2 Target = new Vector2(660, 180);
    Vector2 Forward = Vector2.Normalize(Target-Source);
    float Speed = 100; // Pixels per second
    float Duration = (Target - Origin).Length() / Speed;
    float Time = 0;
    
    public void Update(float ElapsedSecondsPerFrame)
    {
       if (Time<Duration)
       {
          Time+=Duration;
          if (Time > Duration) {
              Time = Duration;
              Origin = Target;
          }
          else Origin += Forward * Speed * ElapsedSecondsPerFrame;
       } 
    }
    
    public void Draw()
    {
        rect = new Rectangle((int) Origin.X, (int) Origin.Y, 80, 120);
        sb.Draw(point, rect, Color.Red);   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: public class Window extends JFrame { public Window(){ ... JButton
I have this code that describes a service: public class navigation_web extends Service {
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
I have this code for my soundboard, public class soundboardActivity extends Activity { View
I have this sample code for async operations (copied from the interwebs) public class
I have class where I draw image. This is code: public class CanvasdrawActivity extends
Best to describe this in code... I have this public class A<T> { public
I have this code : class Event{}; class CustomEvent:public Event{}; class Handler { public:
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I have this ActionFilter public class AppOfflineFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.