13 Replies - 930 Views - Last Post: 22 March 2013 - 02:57 PM Rate Topic: -----

#1 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Moving object from side to side

Posted 21 March 2013 - 11:13 AM

 private void timer1_Tick(object sender, EventArgs e)
        {
            if (minTavla.X == 70)
            {
                minTavla.X += 10;
            }
            if (minTavla.X == 70)
            {
                minTavla.X += 10;
            }
            Invalidate();
        }


i cant figure out how to make my dartboard move from one side to another side i have painted it with drawellipse in a class and the name of my dartbord is "minTavla" please help me! (i want it to go from left to right and when it hits the wall it should go back to left and keep on going back and furth.

Thanks for helping!

This post has been edited by Atli: 21 March 2013 - 11:16 AM
Reason for edit:: Fixed the [code] tags and updated the title. Please use *descriptive* titles. "Problem" and "Question" are completely useless as titles.


Is This A Good Question/Topic? 0
  • +

Replies To: Moving object from side to side

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Moving object from side to side

Posted 21 March 2013 - 11:23 AM

The logic for this is pretty straight forward. There are only two conditions you need to check:

  • If the position of the left edge of the object is smaller than the position of the left side of the window/canvas (usually just 0), make it go right.
  • If the position of the right edge of the object is greater than the position of the right side of the window/canvas (usually it's width), make it go left.


To do that, you'll have to store the direction in which the object is moving in a variable somewhere. On each tick, move the object in that direction. Then check for the two conditions above.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Moving object from side to side

Posted 21 March 2013 - 11:23 AM

Your first if-statement is only checking for the exact value of 70 before increasing it to 80. The second if will never happen because, if the value was 70, it would now be 80. You should be looking at < and > conditions.

This post has been edited by andrewsw: 21 March 2013 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Re: Moving object from side to side

Posted 21 March 2013 - 11:37 AM

03           if (minTavla.X < 470)
04           {
05               minTavla.X += 10;
06           }
07           if (minTavla.X > 470)
08           {
09              minTavla.X -= 10;
10           }
11           Invalidate();


Hm i cant get it to move back to left side from right side now ?_? and how do i make it to move all the time with return?

This post has been edited by tlhIn`toq: 21 March 2013 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Moving object from side to side

Posted 21 March 2013 - 12:01 PM

Atli has already described how you would get it to alternate between moving left and moving right, and you are already using a timer so it would move all the time, unless you cancel the timer.

Currently, the object will either jump either side of 470 or, if it actually hits the exact figure of 470, it would stop.
Was This Post Helpful? 0
  • +
  • -

#6 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Re: Moving object from side to side

Posted 21 March 2013 - 12:17 PM

To do that, you'll have to store the direction in which the object is moving in a variable somewhere. On each tick, move the object in that direction. Then check for the two conditions above.

?? i dont get it :(
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Moving object from side to side

Posted 21 March 2013 - 01:13 PM

View PostMabe96, on 21 March 2013 - 07:17 PM, said:

To do that, you'll have to store the direction in which the object is moving in a variable somewhere. On each tick, move the object in that direction. Then check for the two conditions above.

?? i dont get it :(

What don't you get? Surely the logic is clear.

You have a class-level variable:

private bool movingRight = true;

then in your timer you keep checking if this value is true; if so, keep moving the element right-wards, until it is as far as it needs to go; then change this value to false and your code should move it left-wards (rather than right-wards).. until it reaches as far left as it can go..
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Moving object from side to side

Posted 21 March 2013 - 01:22 PM

Something like this (untested):

            if (minTavla.X < 0) {
                movingRight = true;
            } else if (minTavla.X > 470) {
                movingRight = false;
            }
            minTavla.X += (movingRight) ? 10 : -10;

Was This Post Helpful? 1
  • +
  • -

#9 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Re: Moving object from side to side

Posted 21 March 2013 - 11:54 PM

Thank you guys!
Was This Post Helpful? 0
  • +
  • -

#10 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Re: Moving object from side to side

Posted 22 March 2013 - 01:49 AM

How to make graphic hit other graphic? i got a dartboard made with 6 ellipses in graphic and then a dot (my shooting ammo) made in 1 ellipse but when i shoot the dot it dont stay on the dartboard when the dartboard move. It stays on the form?
please help and give me any hint
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,158
  • Joined: 05-May 12

Re: Moving object from side to side

Posted 22 March 2013 - 05:27 AM

When you adjust the position of the dartboard, you'll need to also adjust the position of past shots.

Alternately, you could render the graphic without any hits. After each hit, take a snapshot of the updated graphic and use that for succeeding repaints.
Was This Post Helpful? 1
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Moving object from side to side

Posted 22 March 2013 - 05:45 AM

Stop working on this project for a second.
How is it you have *any* code when you have have no plan for how you are going to deal with some of the most fundamental concepts that your game is based on?

Before you start banging on the keyboard you need to work out some of these things.
Make a small R&D project just for object collision.
Make a small R&D project just for animating.
Etc.
Learn each of the techniques your game will require individually.
Then form a plan and archetect a blueprint for your game.
THEN start typing.
Was This Post Helpful? 0
  • +
  • -

#13 Mabe96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 13

Re: Moving object from side to side

Posted 22 March 2013 - 09:29 AM

I just need help with the for-sats that make my dot move same coordinate as my board when i hit it
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Moving object from side to side

Posted 22 March 2013 - 02:57 PM

No; you need a lot more help than that. You just don't realize it.

One example:

Quote

if (minTavla.X < 470)

What's with the hardcoded limits of 470? If the board or the form get resized this will be a problem. Limits should be dynamic to the playing area. For example: If the moving board get within 10 pixels of the left edge, or moves more than 25% of its width to the left, or just keep moving until it comes in contact with a barrier object you draw. Dynamic (calculated) adjustment to its environment. This would allow the game to be more dynamic such as higher difficulty levels use a board that has been reduced in size and thus a smaller target.

Same problem with the hardcoded value for the movement amount/speed

Quote

minTavla.X += (movingRight) ? 10 : -10;

If this were a variable instead of a hardcoded 10, then you could adjust the speed/amount of movement depending on the game difficulty or video card performance criteria to produce a smoother movement.

All of that from just 12 lines of code you posted. You have no idea how to make two objects move together. You have no idea how to detect if one has a collision with the other.

It is an indication that you don't have any plan for the actual coding of this game. You have images in your head of what it should look like, and that's important. But you have no blueprint for the actual code to achieve your goal. You wouldn't build a house by randomly cutting boards and nailing them together, then ask "Hey, the walls are uneven and won't stand up. What do I do?". That's what you're doing here: Randomly putting some parts together in a rough idea of what you understand - hoping that along the way all the missing knowledge will just magically impart itself upon you, and it will all somehow work out in the end. That is *not* software engineering. That is not any engineering at all.

I'll make a last effort to urge you into doing smaller bits of experimentation on one piece of the puzzle at a time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1