problems with arrays

hi this is my set of codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
bool fireCannonBall(int cannon_x, int cannon_y,int targets[], int target_x[],int target_y[], int no_targets)
{
	int obj_no1 = 0;
	int obj_no = 0;
	int i = 0;
	int ball_x = 0;
	int ball_y = 0;
	int target_width = 40;
	int target_height = 40;
	int j = 0;

	//equation for ball coordinates
	ball_x = cannon_x + 20;
	ball_y = cannon_y - 1;
	//display cannon
	obj_no = displayBMP("cannon.bmp", cannon_x, cannon_y);
	obj_no1 = drawCircle(5, ball_x, ball_y);
	
	do
	{
		
		//Move to top of screen
		if (up())
		{
			for (i = ball_y; i>=0; i--)
			{
				//drawing circle
				ball_x = cannon_x + 20;
				moveObject(obj_no1, ball_x, ball_y--);

				for (j = 0; j < no_targets; j++)
				{
					if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
					(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
					{
						removeObject(targets[j]);
						removeObject(obj_no1);
					}
				}
			}
		}
		//Smove right
		if (right())
		{
			cannon_x++;
			moveObject(obj_no,cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}
  
		//move left
		if (left())
		{
			cannon_x--;
			moveObject(obj_no, cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}

	}while(true);
}


NOW MY PROBLEM IS THAT THE PROGRAM SHOW A CANNON WITH A CANNONBALL AND WHENEVER I PRESS THE UP ARROW, IT SHOULD SHOOT THE CANNONBALL HITTING THE TARGETS THAT ARE IN THE SCREEN, THE PROBLEM IS THAT WHENEVER I SHOOT IT MAKE DISAPPEAR THE TARGET BUT AFTER THAT IT KEEPS RUNNING INSIDE THE LOOP UNTIL TOUCH 0 IN THE Y COORDS. IT SHOULD STOP DOING THE FOR LOOP WHEN IT TOUCH THE TARGETS AND THEN RETURN THE CANNONBALL TO THE CANNON TO BE ABLE TO MOVE LEFT OR RIGHT AGAIN AND KEEP SHOOTING... CAN SOMEBODY HELP ME. I TRIED WITH BREAK; IN THE IF STATEMENT BUT KEEP DOING IT, AND IF THERE IS MORE THAN ONE OBJECT IN THE Y COORDS THAT THE BALL WILL TOUCH IT WILL MAKE ALL OF THEM DISAPPEAR BECAUSE IT DOES NOT STOP WHEN IT TOUCH THE FIRST ONE.... THANK YOU
I don't know if you realize this, but all caps is really the textual equivalent of screaming, and it is quite annoying.
sorry i did not mean that
closed account (D80DSL3A)
The for loop starting on line 25 has no means of exiting from when there is a hit. i will continue to decrease to 0. This may be the cause of your problem. Here is an idea for fixing this problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool Hit = false;//  set true when a hit occurs

for (i = ball_y; i>=0; i--)
{
	//drawing circle
	ball_x = cannon_x + 20;
	moveObject(obj_no1, ball_x, ball_y--);

	for (j = 0; j < no_targets; j++)
	{
		if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
		(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
		{
			removeObject(targets[j]);
			removeObject(obj_no1);
                        Hit = true;// time for cannonball to stop falling
		}
	}
        if( Hit )
                break;// exit the for loop
}

Hope this helps.
thank you that works just nice but now i have still another problem and it is that i need to after break that loop be able to keep moving the cannon left or right with the cannonball on it, but i try with 1 do while, with 2 do-while and nothing i do not know how to do because if i make it come back to the cannon whenever i press the up arrow again it will shoot from the position where it hit the target and not from the cannon, and if not going to do it by itself, i have to hold the up arrow and it should not be like that if somebody can help me thank you...this is the code fixed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
bool fireCannonBall(int cannon_x, int cannon_y,int targets[], int target_x[],int target_y[], int no_targets)
{
	int obj_no1 = 0;
	int obj_no = 0;
	int i = 0;
	int ball_x = 0;
	int ball_y = 0;
	int target_width = 40;
	int target_height = 40;
	int j = 0;
	bool Hit = false;//  set true when a hit occurs

	//equation for ball coordinates
	ball_x = cannon_x + 20;
	ball_y = cannon_y - 1;
	//display cannon
	obj_no = displayBMP("cannon.bmp", cannon_x, cannon_y);
	obj_no1 = drawCircle(5, ball_x, ball_y);
	
	do
	{
		//Move to top of screen
		if (up())
		{
			for (i = ball_y; i>=0; i--)
			{
				//drawing circle
				ball_x = cannon_x + 20;
				moveObject(obj_no1, ball_x, ball_y--);

				for (j = 0; j < no_targets; j++)
				{
					if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
					(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
					{
						removeObject(targets[j]);
						removeObject(obj_no1);
						Hit = true;// time for cannonball to stop falling
					}
				}
				if( Hit )
                break;// exit the for loop
			}
		}

		//move right
		if (right())
		{
			cannon_x++;
			moveObject(obj_no,cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}
  
		//move left
		if (left())
		{
			cannon_x--;
			moveObject(obj_no, cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}

	}while(true);
}
closed account (D80DSL3A)
Glad that worked somewhat. I had meant for bool Hit = false; to be just before the for loop, not at the top of your function on line 11. This is so that Hit is reset to false on the next iteration of the do loop. You may also need to reset the ball position after a hit. Perhaps the following would work (adapting your code from line 22 to 44). Go ahead and leave line 11 as you have it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Move to top of screen
if (up())
{
        Hit = false;// leaving declaration on line 11 your code.
        ball_y = cannon_y - 1;// reset ball_y for next shot
	for (i = ball_y; i>=0; i--)
	{
		//drawing circle
		ball_x = cannon_x + 20;
		moveObject(obj_no1, ball_x, ball_y--);

		for (j = 0; j < no_targets; j++)
		{
			if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
			(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
			{
				removeObject(targets[j]);
				removeObject(obj_no1);
				Hit = true;// time for cannonball to stop falling
			}
		}
		if( Hit )
                break;// exit the for loop
	}
}


I'm kinda guessing though since I don't know the details of how your code functions.
thank you that make it works pefect but now i have a different problem... when i shot a target then in my next shot the cannonball will not go up more than the coords of the target that i just shot.. for example if my target was in y = 150... next time if i shoot again will not go less than 150 stop and come back i know that the ball_y = cannon_y - 1 is to reset it but it is not doing that and i cannot find the problem... the other thing that i modified in my code is that i need to return how many shots i did that why i put before breaking after if (hit) is true that statement but i do not know how to make it return that value because it is a bool and i need to return is true or false... if the score is 10 the program should get out of that function that is why i put score if score == 1 break break... thank you i really appreciate it... this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
bool fireCannonBall(int cannon_x, int cannon_y,int targets[], int target_x[],int target_y[], int no_targets)
{
	int obj_no1 = 0;
	int obj_no = 0;
	int i = 0;
	int ball_x = 0;
	int ball_y = 0;
	int target_width = 40;
	int target_height = 40;
	int j = 0;
	int score = 0;
	int shots = 0;
	bool Hit = false;//  set true when a hit occurs

	//equation for ball coordinates
	ball_x = cannon_x + 20;
	ball_y = cannon_y - 1;
	//display cannon
	obj_no = displayBMP("cannon.bmp", cannon_x, cannon_y);
	obj_no1 = drawCircle(5, ball_x, ball_y);
	
	do
	{
		//Move to top of screen
		if (up())
		{
			Hit = false;// 
			ball_y = cannon_y - 1;// reset ball_y for next shot
			shots = shots+1;
			

			for (i = ball_y; i>=0; i--)
			{
				//drawing circle
				ball_x = cannon_x + 20;
				moveObject(obj_no1, ball_x, ball_y--);

				for (j = 0; j < no_targets; j++)
				{
					if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
					(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
					{
						removeObject(targets[j]);
						removeObject(obj_no1);
						obj_no1 = drawCircle(5, ball_x, cannon_y);
						Hit = true;// time for cannonball to stop falling
					}
				}
				if( Hit )
				{
					score = score + 1;
					if (score == 10)
					{
						break; break;
					}
                break;// exit the for loop
				}
			}
		}

		//move right
		if (right())
		{
			cannon_x++;
			moveObject(obj_no,cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}
  
		//move left
		if (left())
		{
			cannon_x--;
			moveObject(obj_no, cannon_x, cannon_y);
			moveObject(obj_no1, cannon_x+20, cannon_y);
		}

	}while(true);
}
closed account (D80DSL3A)
I was going to continue working on your problem since your last post contains some further questions, but the thread is marked as solved. Does this mean you solved the remaining issues yourself?
yes man you help me a lot and i really appreciate it...
Topic archived. No new replies allowed.