移动计时器图片 [英] Move images with timer

查看:93
本文介绍了移动计时器图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个代码,移动特定位置的图像与定时图片框



但由于图像不我要的道路上移到我的代码不能正常工作。



我要什么是:

   - 由点(10,Y0)创建图像。 
- 水平移动左右的时间达到点(500,Y0)。
- 移动垂直向下或向上到达点(500750),并在此停留例7秒。
- 然后,再移到右水平和去点(700750),并在此停留例8秒。
- 那么,移动再次水平右走点(750750)。
- 在这点上,如果(完整==真)`pictureBox`必须隐藏和回(10,Y0)
- 如果为(完整==假),则
如果(最多== true)而
- 垂直向上到达点(750900)与和
- 水平移动向右转(900900),并停在这里例如为10秒。
- 那么,那么,将再次水平向左走点(500900)。
- 那么,移动垂直向下到达点(500750)。
,否则如果(下== true)而
- 移动垂直向下到达点(750600)与和
- 水平移动向右转(900600),并停在这里例如为10秒。
- 那么,那么,将再次水平向左走点(500,600)。
- 那么,移动垂直向上到达点(500750)。



我已经写了一些代码,但正如我所说,这是不正确移动...



一件事:我怎样才能实现?等待! (当端起exapmle(900600),我们必须等待10秒或等待,直到外面的东西让图像移动)。



请帮我...



下面是我的代码至今:

 私人INT K = 0; 

无效timer_Tick(对象发件人,EventArgs五)
{
k的序列++;

INT X = p.Location.X;
INT Y = p.Location.Y;

如果(K< = 250)
p.Location =新的点(x + 2,y)的;

,否则如果(K< = 400)
{
//如果我们都动起来点(500750)
如果(Y< 750)
p.Location =新的点(x,y + 1);

//如果我们动点的下跌(500750)
如果(Y> 750)
p.Location =新的点(x,y - 1);
}

// ***在这里等着。

,否则如果(K< = 500)
p.Location =新的点(x + 2,y)的;

// ***在这里等着。

,否则如果(K< = 550)
p.Location =新的点(x + 2,y)的;

,否则如果(K&下; = 700)
{
如果(完整==真)
p.Location =新点(X,Y + 1)的;
,否则
p.Location =新的点(x,y - 1);
}

,否则如果(K< = 800)
p.Location =新的点(x + 2,y)的;

// *****在这里等着

,否则如果(K< = 1200)
p.Location =新的点(x - 2,Y );

,否则如果(K< = 1400)
{
如果(完整==真)
p.Location =新的点(x,y - 1) ;
,否则
p.Location =新的点(x,y + 1);
}
,否则
timer1.Stop();
}

}

私人无效的button1_Click(对象发件人,EventArgs五)
{
timer1.Start();
timer1.Interval = 15;
timer1.Tick + =新的EventHandler(timer_Tick);
}


解决方案

说实话,因为我M不能完全肯定它实际上做什么,我不打算提供一个PictureBox解决方案。动画,定时等,在我的经验,从不轻易实现使用计时器,这主要是因为您使用的定时器的那种不保证他们开火什么时候他们应该 - 因为他们需要Windows消息泵是空定时器消息打通。这并不是说这是不可能的,或任何东西,的确是我的动画第一部作品也与计时器完成。



如果你正在写的代码是为了满足一些 运动计算机动画(即家庭作业吗?),那么你就必须学会这是试图解决问题,或有人更慷慨的比我无疑会给你答案的一部分。



就我而言,做到这一点,而不是在WPF中,并使用其的动画故事板和功能。



这样你可以指定你的时间表和路径(线性路径与WPF简单),然后你可以使用的决定的事件。管理实际动画则不再是你的问题 - 你只需指定事情需要多长时间他们应该采取



最后的结果很可能是远优于在屏幕上为好,因为WPF采用了适当的渲染循环类似于​​那种在游戏中使用的东西。



很抱歉,如果这个答案是没有帮助的。


I've written a code to move images from specific locations with timer and pictureBox.

But my code doesn't work properly because images don't move on the path I want to.

what I want is:

- create images from points (10,Y0).
- move horizontally  right to reach  point (500,Y0).
- move vertically down or up to reach point (500,750) and stop here for example 7 second.
- then, move again horizontally right and go to point (700,750) and stop here for example 8 second.
- then, move again horizontally right and go to point (750,750).
- at this point, if (complete == true) the `pictureBox` must hide and back to (10,Y0)
- if (complete == false) then
        if (up == true) 
             - move vertically up to reach point (750,900) and and  
             - move horizontally right and go to (900,900) and stop here for example 10 second.   
             - then, then, move again horizontally left and go to point (500,900).
             - then, move vertically down to reach point (500,750).
        else if (down == true)
             - move vertically down to reach point (750,600) and and  
             - move horizontally right and go to (900,600) and stop here for example 10 second.   
             - then, then, move again horizontally left and go to point (500,600).
             - then, move vertically up to reach point (500,750).

I've written some codes but as I said, It's doesn't move correctly...

one more thing: how I can implement the waits??! (when reach for exapmle (900,600) we must wait 10 second or wait until something outside allow the image to move).

Please help me...

Here is my codes so far:

    private int k = 0;

    void timer_Tick(object sender, EventArgs e)
    {
        k++;

        int x = p.Location.X;
        int y = p.Location.Y;

        if (k <= 250)
            p.Location = new Point(x + 2, y);

        else if (k <= 400)
        {
             // if we are moving up of point (500,750)
            if (y < 750)
                p.Location = new Point(x, y + 1);

            // if we are moving down of point (500,750)
            if (y > 750)
                p.Location = new Point(x, y - 1);
        }

        // *** wait HERE.

        else if (k <= 500)
            p.Location = new Point(x + 2, y);

        // *** wait HERE.

        else if (k <= 550)
            p.Location = new Point(x + 2, y);

        else if (k <= 700)
        {
            if (complete == true)
                p.Location = new Point(x, y + 1);
            else
                p.Location = new Point(x, y - 1);
        }

        else if (k <= 800)
            p.Location = new Point(x + 2, y);

        // ***** wait HERE

        else if (k <= 1200)
            p.Location = new Point(x - 2, y);

        else if (k <= 1400)
        {
            if (complete == true)
                p.Location = new Point(x, y - 1);
            else
                p.Location = new Point(x, y + 1);
        }
        else
           timer1.Stop();
    }

}

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
        timer1.Interval = 15;
        timer1.Tick += new EventHandler(timer_Tick);
    }

解决方案

To be honest, because I'm not entirely sure what it's actually doing I'm not going to offer a picturebox solution. Animation, timing etc is, in my experience, never easy to implement using timers, primarily because the kind of timers you're using don't guarantee that they fire exactly when they should - since they require the windows message pump to be empty for the timer message to get through. It's not that it's impossible or anything, indeed my first work with animation was also done with timers.

If the code you're writing is to satisfy some exercise in computer animation (i.e. homework?) then you will have to learn this as part of trying to solve the problem or somebody more generous than I will no doubt give you the answer.

For my part, do this instead in WPF and use its animation and storyboard functionality.

This way you can specify your timelines and paths (linear paths are simple with WPF) and then you can use events for the decisions. Managing the actual animation then is no longer your problem - you just specify where things need to be and how long they should take.

The final result is likely to be far superior on screen as well, since WPF uses a 'proper' rendering loop akin to the kind of thing used in games.

Sorry if this answer isn't helpful.

这篇关于移动计时器图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆