### Author Topic: problem with shots  (Read 2528 times)

#### bricky91

• Mc. Print
• Posts: 47
##### problem with shots
« on: 2008-Jan-30 »
Hello guys, i'm new here! oh, and i'm italian, so my english isn't very good ^^
however, i've started using GL Basic a few days ago, but i have some experience with game maker, using only scripts(without objects), and it is quite similar.
i made a pong game by myself, and it wasn't difficult, so i decided to make a shooter... but i have a problem... i wrote this code:
Code: (glbasic) [Select]
GOSUB init

main:
GOSUB muovi
GOSUB mostra
GOTO main

// ------------------------------------------------------------- //
// ---  INIT  ---
// ------------------------------------------------------------- //
SUB init:

//settiamo variabili e quant'altro*******************************************************************+

//giocatore
plx=320
ply=380

//colpi
n=0
puoisparare=60

ENDSUB // INIT

// ------------------------------------------------------------- //
// ---  MUOVI  ---
// ------------------------------------------------------------- //
SUB muovi:

//giocatore********************************************************************************************

//movimento

IF KEY(203) AND plx>8
plx=plx-4
ENDIF

IF KEY(205) AND plx<630
plx=plx+4
ENDIF

IF KEY(200) AND ply>10
ply=ply-2
ENDIF

IF KEY(208) AND ply<470
ply=ply+2
ENDIF

//sparo

IF puoisparare>0
puoisparare=puoisparare-1
ENDIF

IF KEY(30) AND puoisparare=0
n=n+1
colpox[n]=plx
colpoy[n]=ply
puoisparare=60
ENDIF

//muoviamo i colpi verso l'alto e distruggiamoli se fuori*****************************************
FOR i=1 TO n
colpoy[i]=colpoy[i]-6
NEXT

ENDSUB // MUOVI

// ------------------------------------------------------------- //
// ---  MOSTRA  ---
// ------------------------------------------------------------- //
SUB mostra:

//disegna la navetta del giocatore
DRAWRECT plx-8,ply-10,16,20,RGB(0,255,0)

//disegna i colpi
FOR i=1 TO n
DRAWRECT colpox[i]-2,colpoy[i]-2,4,4,RGB(0,255,0)
NEXT

PRINT puoisparare,100,100
//mostra tutto
SHOWSCREEN

ENDSUB // MOSTRA
the main variables are:

plx= player x position
ply=  player y position
n= shot number(first, second, third,...)
puoisparare= if the value of this variable is 0, then you can shoot.

SUBS
muovi = here are the keyboard controls
mostra= here is all the graphic
init= give the variables the start values

the problem become when i try to shoot... i press the A button, and when the variable puoisparare is equal to 0 the window is closed and the game stop... why does it happen?? what am i doing wrong?

thanks all, bye!

#### Ian Price

• Prof. Inline
• Posts: 4159
• On the shoulders of giants.
##### problem with shots
« Reply #1 on: 2008-Jan-31 »
You haven't DIMensioned COLPOX[] or COLPOY[]

Colpox and colpoy are arrays - you are going out of bounds in one or other (or both) arrays by increasing the n value. If you add -

Code: (glbasic) [Select]
DIM colpox[100000]
DIM colpoy[100000]
at the beginning of your code, you can see the bullet.

Obviously you need to ensure that these still don't go out of bounds, by resetting their value to zero once they are of no use (hit something or off screen etc.)

I came. I saw. I played.

#### AndyH

• Dr. Type
• Posts: 383
##### problem with shots
« Reply #2 on: 2008-Jan-31 »
Try turning on Debugging and see if you get an error message.  Should hopefully shed some light as to the problem.

Also didn't notice any declarations for your arrays - colpox[n] etc.

#### Ian Price

• Prof. Inline
• Posts: 4159
• On the shoulders of giants.
##### problem with shots
« Reply #3 on: 2008-Jan-31 »
Beat ya!
I came. I saw. I played.

#### Moru

• Prof. Inline
• Posts: 1792
##### problem with shots
« Reply #4 on: 2008-Jan-31 »
Still, run the game with debuggin on while developing, saves lots of tears!

#### bricky91

• Mc. Print
• Posts: 47
##### problem with shots
« Reply #5 on: 2008-Jan-31 »
Quote from: iprice
You haven't DIMensioned COLPOX[] or COLPOY[]

Colpox and colpoy are arrays - you are going out of bounds in one or other (or both) arrays by increasing the n value. If you add -

Code: (glbasic) [Select]
DIM colpox[100000]
DIM colpoy[100000]
at the beginning of your code, you can see the bullet.

Obviously you need to ensure that these still don't go out of bounds, by resetting their value to zero once they are of no use (hit something or off screen etc.)

thanks very much! now it's all clear... i must write the arrays at the beginning... thanks again