Computer Science Canada Array subscripts |
Author: | Clayton [ Sun Aug 13, 2006 1:50 pm ] | ||
Post subject: | Array subscripts | ||
right now i am working on a game (see my post in [Turing Source Code]) and im having a problem with getting rid of any balls that have been clicked on. I am using classes and i have an array of my type "bouncy", in it is the instance field (pointer to my ball class) and a point_taken field (dont worry about it), basically what i am trying to do is have a ball disappear when a ball is clicked, then free the pointer reference to that object, thus saving memory (as i dont need that ball anymore), then resize my array to one less element (having pushed the ball to be "deleted" to the upper bounds of the array) then carry on with the rest of the program, however i am having trouble in my "delete_ball" procedure, i get the error "Array subscript is out of range" when i try to "push" the ball to be deleted to the upper bounds of the array. I dont know why i am getting this error, and it would be appreciated if you guys could look at my code and see if you can figure out why its happening, thx in advance for your help ![]()
|
Author: | Cervantes [ Sun Aug 13, 2006 4:39 pm ] |
Post subject: | |
'lp' reaches a max value of `upper (ball)'. So `lp +1', which is the subscript in the line producing the error, ranges to `upper (ball) + 1', which is out of range of the array. Solution: modify the range of your for loop to only reach `upper (ball) - 1'. |
Author: | Clayton [ Sun Aug 13, 2006 5:15 pm ] |
Post subject: | |
aha thank you very much Cervantes, that was a bit of an oopsie on my part, i thought i had put upper(ball)-1 oh well ![]() |
Author: | Clayton [ Sun Aug 13, 2006 5:57 pm ] | ||
Post subject: | |||
ok now im having another problem, the delete_ball proc is working fine, however, after you click two or three balls (sometimes even the first) Turing crashes with an Array subscript is out of range error, here is my code so far :
i have no idea why this is happening, any help would be appreciated ![]() |
Author: | Cervantes [ Mon Aug 14, 2006 7:40 am ] |
Post subject: | |
You deleted a ball from the array, so now the array has one less element. But the range doesn't change. Let's say your array was from 1 .. 5. You delete an element, so it is now 1 .. 4. But the for loop doens't re-evaluate `upper (ball)', so it is still going 1..5. Hence the error. You may wish to read this for some ideas. |
Author: | Clayton [ Mon Aug 14, 2006 10:21 am ] | ||
Post subject: | |||
thank you very much Cervantes, if you werent a mod id give you bits, but... anyways on to my next question, this is my current code so far:
now if you run that, and create a whole bunch of balls (using the enter key), you will notice that you dont have to just click on a ball, you can just hold down the button and move the mouse around and you can "click" on all of the balls, i would like to know how to fix this, i have so far tried using a combination of Mouse.ButtonMoved and Mouse.ButtonChoose but it isnt working, any help would be appreciated ![]() |
Author: | Cervantes [ Mon Aug 14, 2006 11:54 am ] |
Post subject: | |
Just use a variable to keep track of the mouse buttons previous state. So if the previous state says it the mouse button was up, and the current state says the mouse button is down, then you know the mouse was just clicked, and you should register that, removing a ball under the mouse if there is one. On the other hand, if the previous state says the mouse button was down, and the current state says the mouse button is down, then the mouse is being held down, and this should not be registered as a click. |
Author: | Clayton [ Mon Aug 14, 2006 12:07 pm ] | ||
Post subject: | |||
so have something like this?
|
Author: | Clayton [ Mon Aug 14, 2006 12:25 pm ] | ||
Post subject: | |||
okay i got that to work, kinda. for some strange reason, doing it this way, you can only click and clear a ball in the order they appeared on the screen, i dont know why this is happening, but i think it has something to do with the way im calling my check_clicked procedure now, heres my up to date code:
any help would be appreciated ![]() |
Author: | TheOneTrueGod [ Mon Aug 14, 2006 1:46 pm ] | ||||
Post subject: | |||||
I believe it is because of this:
If you click on ball(2), it checks ball 1, sets oldbutton to 1, realises you havn't clicked on ball 1, then moves on. Now it checks ball 2, but oldbutton = 1 and button = 1, so nothing happens. What I normally do is:
Also, You have a lot of global-ish variables there -- You should consider trying to eliminate them, and have the procedures run purely on parameters (Actually, for the "Clicked On" procedures, these should be functions that return booleans ![]() |
Author: | TheOneTrueGod [ Mon Aug 14, 2006 1:53 pm ] |
Post subject: | |
Sorry for the double post, but I just came across this post by you in the application section, and thought it fit perfectly here as a message to you ![]() Superfreak wrote: i noticed in your code you are using randint, i ask you : why? randint is a procedure that changes the value of your variable, why not use Rand.Int, a function? generally speaking, when you have a choice between a function or a procedure to do something, take the function |
Author: | Ultrahex [ Mon Aug 14, 2006 3:29 pm ] |
Post subject: | |
Just To Notify You, if you check here first i have supplied you with i think all the information your going to need i accidently posted in the turing source code area oh well ![]() anyhow http://www.compsci.ca/v2/viewtopic.php?t=13292 thats link to your post over there and my info is there with examples and all! peace |
Author: | Clayton [ Mon Aug 14, 2006 3:37 pm ] | ||
Post subject: | |||
well actually if you look at the originaly code the check_clicked procedure in my balls class was originally a fcn, however, when i added in the button,oldbutton variables to control clicking, i couldnt figure out immediately how to use it with a function without changing the draw_ variable from within the class outside of the class (a very, very bad practice ![]() as for my problem i figured it out, it was pretty much like you said TOTG, i played with it a bit and got it to work, my check_click procedure looks like this now (outside of the class):
and it works fine right now (even if it isnt the prettiest ![]() |
Author: | NikG [ Mon Aug 14, 2006 5:16 pm ] | ||||
Post subject: | |||||
SuperFreak82 wrote:
![]() It may work, but it's not as easy to understand. Do what Cervantes, TOTG, and even I have recommended in your other post:
|
Author: | Clayton [ Mon Aug 14, 2006 8:31 pm ] |
Post subject: | |
whats so hard to understand about mine? the way i see it mine is actually easier to understand because what is happening is more apparent ![]() |