Array subscript is out of range
Author |
Message |
Ledrif
|
Posted: Fri Nov 07, 2008 11:36 pm Post subject: Array subscript is out of range |
|
|
Hello, im in the middle of coding a game (Code follows) involving a flexable array(im new to it)
issue being im getting a "Array subscript is out of range" message when my collision occurs.
Turing: |
% --View--
View.Set("graphics:400;400,position:center;center,nobuttonbar,title:Space,offscreenonly")
% --Variables--
var ballmidx : int := 200
var ballmidy : int := 100
var ballspeedx : int := 3
var ballspeedy : int := 5
var paddle : int := 200
var paddlespeedleft : int := 10
var paddlespeedright: int := 10
var blackoutx: flexible array 1.. 0 of int
var blackouty: flexible array 1.. 0 of int
var chars: array char of boolean
% --Loop_Start--
loop
Input.KeyDown (chars )
% --Backdrop--
drawfillbox (10, 0, 390, 390, black)
drawfillbox (15, 200, 385, 385, gray)
drawfillbox (- 100, 0, 10, 400, blue)
drawfillbox (390, 0, 400, 500, blue)
drawfillbox (10, 390, 390, 500, green)
% --Player--
drawfillbox (paddle- 30, 10,paddle+ 30, 20, green)
% --Ball--
drawfilloval (ballmidx,ballmidy, 5, 5, red)
% --Movement--
% --Player--
if chars (KEY_LEFT_ARROW) then
paddle:=paddle-paddlespeedleft
end if
if chars (KEY_RIGHT_ARROW) then
paddle:=paddle+paddlespeedright
end if
% --Movement_Object
ballmidx:=ballmidx-ballspeedx
ballmidy:=ballmidy-ballspeedy
if ballmidy< 1 then
ballmidx:= 200
ballmidy:= 100
end if
View.Update
% --Crash--
% --Gray--
if whatdotcolour(ballmidx+ 6,ballmidy )= gray or whatdotcolour(ballmidx- 6,ballmidy )= gray then
ballspeedx:=-ballspeedx
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------%
new blackoutx , blackoutx (upper (blackoutx )) + 1
new blackouty , blackouty (upper (blackouty )) + 1
blackoutx (upper (blackoutx )) := ballmidx
blackouty (upper (blackouty )) := ballmidy
for i : 1 .. upper(blackoutx )
Draw.Oval(blackoutx (i ),blackouty (i ), 20, 20, black)
end for
%--------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end if
if whatdotcolour(ballmidx,ballmidy+ 6)= gray or whatdotcolour(ballmidx,ballmidy+ 6)= gray then
ballspeedy:=-ballspeedy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------%
new blackoutx , blackoutx (upper (blackoutx )) + 1
new blackouty , blackouty (upper (blackouty )) + 1
blackoutx (upper (blackoutx )) := ballmidx
blackouty (upper (blackouty )) := ballmidy
for i : 1 .. upper(blackoutx )
Draw.Oval(blackoutx (i ),blackouty (i ), 20, 20, black)
end for
%--------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end if
% --Green--
if whatdotcolour(ballmidx,ballmidy+ 6)= green or whatdotcolour(ballmidx,ballmidy- 6)= green then
ballspeedy:=-ballspeedy
end if
% --Blue--
if whatdotcolour(ballmidx+ 6,ballmidy )= blue or whatdotcolour(ballmidx- 6,ballmidy )= blue then
ballspeedx:=-ballspeedx
end if
% --Paddle--
if whatdotcolour(paddle+ 31, 15)= blue then
paddlespeedright:= 0
end if
if whatdotcolour(paddle- 31, 15)= blue then
paddlespeedleft:= 0
end if
if whatdotcolour(paddle+ 31, 15)= black then
paddlespeedright:= 10
end if
if whatdotcolour(paddle- 31, 15)= black then
paddlespeedleft:= 10
end if
delay(100)
View.Update
end loop
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
pavol
|
Posted: Sat Nov 08, 2008 3:50 am Post subject: Re: Array subscript is out of range |
|
|
I am not exactly sure why in the following code:
code: | new blackoutx , blackoutx (upper (blackoutx)) + 1
new blackouty , blackouty (upper (blackouty)) + 1 |
you have blackoutx(upper(blackoutx)) + 1. If what you are trying to do is increment the array size by 1, then the code:
code: | new blackoutx, (upper(blackoutx)+1) |
does the trick. Replacing all 4 instances of that in your program should make it collide and not produce and error. |
|
|
|
|
 |
Ledrif
|
Posted: Sat Nov 08, 2008 10:04 am Post subject: RE:Array subscript is out of range |
|
|
I see thank you, I learned of flexible arrays through reading other turing help posts, and this was the layout of his example.
Since I was unaware of coding layout, just of effect I was unsure what was causing the problem. |
|
|
|
|
 |
|
|