0

I have to allow 20 balls to move around the screen. I would like to know how do I use a loop so I would not have to type the codes out long. Currently, the codes I have are

for (int i = 0; i < ballSpeedXAxis.Length; i++)
        {
            ballSpeedXAxis[i] = 1;
        }

for (int i = 0; i < ballSpeedYAxis.Length; i++)
        {
            ballSpeedYAxis[i] = 1;
        } 

private void OnUpdate(object sender, object e)
{
Canvas.SetLeft(this.ball1, this.ballSpeedXAxis[1] + Canvas.GetLeft(this.ball1));
Canvas.SetTop(this.ball1, this.ballSpeedYAxis[1] + Canvas.GetTop(this.ball1));
Canvas.SetLeft(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetLeft(this.ball2));
Canvas.SetTop(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetTop(this.ball2));
...
Canvas.SetLeft(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetLeft(this.ball20));
Canvas.SetTop(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetTop(this.ball20));
}

ball1, ball2 ... ball3 are images name.

2
  • You've already got an array for ballSpeedAxis. Can't you have an array of ball objects and then have a for loop calling SetLeft & SetTop for each element in the two arrays? Commented Jul 25, 2013 at 3:21
  • 4
    It seems like you might want to create your own Ball class, with properties for the Image and SpeedX and SpeedY. And then have just one array to hold all the balls instead of arrays for each value. This class can (and should) encapsulate all the logic for the balls. For example two more properties PositionX and PositionY, and methods like UpdateSpeed(int speedX, int speedY), UpdatePosition() and Draw(Canvas canvas). Commented Jul 25, 2013 at 6:07

1 Answer 1

4

There are varying ways.. the most obvious being instead of this:

Image ball1;
Image ball2;
Image ball3;
// .. etc ...

You would put those in an array also:

Image[] balls = new Image[20];

..same with your speeds. Then you can change your update method to this:

private void OnUpdate(object sender, object e) {
    for (int i = 0; i < balls.Length; i++) {
        Canvas.SetLeft(balls[i], ballSpeedXAxis[i] + Canvas.GetLeft(balls[i]));
        Canvas.SetTop(balls[i], ballSpeedYAxis[i] + Canvas.GetTop(balls[i]));
    }
}

Others include putting the already created images into a List<Image>.. but that's a bit yuck.

Sign up to request clarification or add additional context in comments.

10 Comments

You should also add an example snippet to illustrate how to add the images into the array.
@PranavN. That is difficult for me to show.. because the way in which assets are loaded could differ greatly for the OP. For example, are they resources? Are they numbered files in a directory somewhere? I assume the OP can expand on my example to fill in that gap.
@SimonWhitehead Hi, I would like to know how do I add images into the array. I have tried balls = Image.FromFile("ball1","ball2",...,"ball20");, but I encountered the error "does not contain a definition for 'FromFile'". For your information, I am doing it for Windows Phone 7.1.
You need to add each image seperately. If they are all named like you showed, you could try: for (int i = 0; i < balls.Length; i++) { balls[i] = Image.FromFile("ball" + (i + 1)); }.
@Corak Hi, I could not implement 'FromFile' as I am doing it for Windows Phone 7.1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.