problem with Pic.Free
Author |
Message |
DemonZ
|
Posted: Fri Nov 17, 2006 1:32 am Post subject: problem with Pic.Free |
|
|
I have a problem when using Pic.Free that I do not even understand, I have a bunch of landmines drawn on the screen by using an array and making the coordinates random for each of them, and I have a collision detection that when the tank hits a mine, an explosion picture is drawn and that mine that blew up is freed, problem is everytime I run the program it says it cannot draw the rest of the pics of the mines because the pic was "freed", heres the part of the code im having problem with.
code: |
for i : 1 .. num_mines
Pic.Draw (landmine (i), LMx (i), LMy (i), picMerge) % Drawing Landmines on battlefield
if Math.Distance (LMx (i), LMy (i), P1x, P1y) < LMrad * 2 then
Pic.Draw (explosion_1, LMx (i), LMy (i), picMerge)
num_mines := num_mines - 1
Pic.Free (landmine (i))
exit
end if
end for
|
but when I change it to this it erases all the landmines before the collision even happens
code: |
for i : 1 .. num_mines
Pic.Draw (landmine (i), LMx (i), LMy (i), picMerge) % Drawing Landmines on battlefield
if Math.Distance (LMx (i), LMy (i), P1x, P1y) < LMrad * 2 then
Pic.Draw (explosion_1, LMx (i), LMy (i), picMerge)
Pic.Free (landmine (i))
exit
end if
end for
num_mines := num_mines - 1
|
can someone please explain to me what is happening, on the first code it will say it cannot draw the pic because it was freed, but on the second code it will free all the pics before I even get a collision, help is appreciated, thanks |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Fri Nov 17, 2006 3:26 am Post subject: (No subject) |
|
|
what i think your doing is declaring your pic to one variable. that variable is called "mine". With this one pic, your drawing multiple variables of that "pic" hence the array your using.
When you Pic Free, i dont think it free's that array var of the pic, but rather the parent var of this, thus you cant draw anything after.
***i think lol, im not all togther atm, its late, and other things*** |
|
|
|
|
![](images/spacer.gif) |
DemonZ
|
Posted: Fri Nov 17, 2006 4:47 pm Post subject: (No subject) |
|
|
no im quite certain that when u use pic.free it will not free the parent var because I tried this on a new file, I had an array of 1 to 2 that stored pics, I used Pic.Free for pic 1, and I Pic.Draw pic 2, and it worked fine. so there must be another reason why this is happening. |
|
|
|
|
![](images/spacer.gif) |
Nova
|
Posted: Fri Nov 17, 2006 10:20 pm Post subject: (No subject) |
|
|
What you're probably doing is freeing a pic in the middle of the array but shortening the array off the end. This means that the freed pic is still part of the list
What you need to do is move the landmines up one in the array to fill the hole.
Note: Using a flexible array would be much better/easier |
|
|
|
|
![](images/spacer.gif) |
DemonZ
|
Posted: Fri Nov 17, 2006 11:41 pm Post subject: (No subject) |
|
|
How do you use flexible arrays, because I dont know how to, but I understand what ur saying about the problem, because it is freeing the right array pic but is shortening the list, if u can show me a flexible array example or explain it to me that would be great. |
|
|
|
|
![](images/spacer.gif) |
DemonZ
|
Posted: Sat Nov 18, 2006 3:48 am Post subject: (No subject) |
|
|
ok so I read up on the tutorial on flexible arrays (thank god I found it) and have done abit of reworking with my code, but now I have another problem, my array appears to go out of range, and I think its because Im using 2 for loops, the first one for calculation, and the second one for moving landmine up one and deleting it, problem is I dont know why it is going out of range or how to fix it, heres the new code though, any help is appreciated.
code: |
for i : 1 .. upper (landmine)
Pic.Draw (landmine (i), LMx (i), LMy (i), picMerge) % Drawing Landmines on battlefield
if Math.Distance (LMx (i), LMy (i), P1x, P1y) < LMrad * 5 then
landmineToBeRemoved := i
for r : landmineToBeRemoved + 1 .. upper (landmine)
landmine (r - 1) := landmine (r)
end for
new landmine, upper (landmine) - 1
Pic.Draw (explosion_1, LMx (i), LMy (i), picMerge)
else
end if
end for
|
|
|
|
|
|
![](images/spacer.gif) |
DemonZ
|
Posted: Sat Nov 18, 2006 4:03 am Post subject: (No subject) |
|
|
sorry for double posting, but problem solved all I did was make the landmines x and y coordinates flexible as well (LMx, LMy) and it works fine now, thanks for all the support though, and the useful thought of "flexible arrays" for they make my life alot easier. anyway heres how I fixed it for people that are curious.
code: |
for i : 1 .. upper (landmine)
Pic.Draw (landmine (i), LMx (i), LMy (i), picMerge) % Drawing Landmines on battlefield
if Math.Distance (LMx (i), LMy (i), P1x, P1y) < LMrad * 5 then
Pic.Draw (explosion_1, LMx (i), LMy (i), picMerge)
for r : i + 1 .. upper (landmine)
landmine (r - 1) := landmine (r)
LMx (r - 1) := LMx (r)
LMy (r - 1) := LMy (r)
end for
new landmine, upper (landmine) - 1
new LMx, upper (LMx) - 1
new LMy, upper (LMy) - 1
exit
end if
end for
|
|
|
|
|
|
![](images/spacer.gif) |
|
|