help with TYPES and FUNCTIONS

Previous topic - Next topic

Jonás Perusquía

Hello everyone, i need your help please :$

I can´t make this piece of code to work:

Code (glbasic) Select
IF SPRCOLL(1,self.x,self.y,0,_disparo.posX, _disparo.posY)

Code (glbasic) Select
TYPE _disparo
posX
posY

FUNCTION make: mouseX, mouseY
self.posX=mouseX;
self.posY=mouseY+15;
ENDFUNCTION

FUNCTION draw:
DRAWSPRITE 2, self.posX, self.posY
ENDFUNCTION

FUNCTION update:
self.posX=self.posX-3;
IF SPRCOLL(2,self.posX,self.posY,1,x,y)
         x=-100
         ENDIF
ENDFUNCTION
ENDTYPE

LOCAL disparos[] AS _disparo;
LOCAL ultimaPulsacion=GETTIMERALL();
LOCAL disp AS _disparo;
LOCAL frecuencia_disparos=150;
//##################################

TYPE _asteroide
x
y

FUNCTION hacer: X, Y
X=RND(-15);
Y=RND((sy-ay));
self.x=X;
self.y=Y;
ENDFUNCTION

FUNCTION dibujar:
ROTOSPRITE 1, self.x, self.y, ra
ENDFUNCTION

FUNCTION actualizar:
self.x=self.x+RND((1)+5);
                 ra=ra+2
          IF SPRCOLL(1,self.x,self.y,0,_disparo.posX, _disparo.posY)       // This is what i cannot make to work
          DIMDEL  asteroides[],contadorasteroides
          ENDIF
ENDFUNCTION
ENDTYPE

LOCAL asteroides[] AS _asteroide;
LOCAL ultimoasteroide=GETTIMERALL();
LOCAL appear AS _asteroide;
LOCAL frecuencia=200;



Can you please help me?
<HTML><BASIC><EC>
Be free and do good things

mentalthink

I'm not sure but you add this lines for the "Clase"...

This not it's into the Type it's a function after the ENDTYPE

FUNCTION all_Process:
   
   FOREACH item    item.dibujar()
           item.dibujar()
           item.pintar()
   NEXT

ENDFUNCTION


Another thing... be sure to initialize the array whit some index... I look something like disparos[], this it's equal to 0... then you have 0 disparos in your code...






Slydog

Code (glbasic) Select
IF SPRCOLL(1,self.x,self.y,0,_disparo.posX, _disparo.posY)

In the above line of code, you use the '_disparo' TYPE directly in place where you would use a variable instance of the '_disparo' TYPE.

For example, this is valid, since it doesn't use '_disparo' in the IF statement:
Code (glbasic) Select
LOCAL disp AS _disparo;
disp.posX = 10
disp.posY = 20
IF SPRCOLL(1, self.x, self.y,    0, disp.posX, disp.posY)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Jonás Perusquía

THANKS FOR YOUR HELP!

ITS NOW FIXED :D

the problem was that the declared TYPES as variables were LOCAL and i needed them GLOBAL
also, i was using _disparo TYPE as variable, thanks Slydog for pointing that out...

discovered other issues like an undeclared variable, position of one SPRCOLL, etc...


Thank you so much :3!
<HTML><BASIC><EC>
Be free and do good things

Jonás Perusquía

Ok, so here is the new code and i have another issue T.T
sorry to bothering you

The IF statement is like this
Code (glbasic) Select
IF SPRCOLL(1,self.x,self.y,0,disp.posX, disp.posY)
          DIMDEL  asteroides[],contadorasteroides
          ENDIF


However, the "contadorasteroides" variable is defined later in code and can´t be defined erlier, and i need your help to make it work :$

see:
Code (glbasic) Select

//This is inside a WHILE TRUE after the TYPES

IF ((ultimoasteroide+frecuencia)<(GETTIMERALL()))
ultimoasteroide=GETTIMERALL();
DEBUG ultimoasteroide+" | "+GETTIMER()+2000
appear.hacer(x,y);
DIMPUSH asteroides[], appear;
ENDIF


FOR contadorasteroides=0 TO LEN(asteroides[])-1
IF (contadorasteroides>0)
asteroides[contadorasteroides].actualizar();
asteroides[contadorasteroides].dibujar();
IF (sx < asteroides[contadorasteroides].x)
DIMDEL  asteroides[],contadorasteroides
ENDIF
ENDIF
NEXT

// THIS IS INSIDE A WHILE TRUE after the TYPES


This is the only thing its blocking me *.*
<HTML><BASIC><EC>
Be free and do good things

Slydog

#5
I'm not quite sure what your problem is in this case without seeing your entire code module.
And the variables names are not English, ha.  A bit of a stumbling block for me, but no problem!

A Google translate on 'contador' is 'counter'? -- So . . . the variable indicates an asteroid counter?
And 'disparo' translates to 'shot' -- So a bullet?

But, I'll take a stab at it blindly . . .
You are checking if two sprites collide, but it is not clear which sprites (ha other than sprite id 1 and 0).
Is this an asteroids clone?
If so, I assume there are multiple asteroids and multiple bullets on the screen at one time?

Is this trying to check if a bullet hits an asteroid, then delete that asteroid?
The first post included more code, but I didn't notice any game loop, so I'm not sure how these TYPEs are intended to work together.  If you only wanted to remove the LAST asteroid from the list you would use this:
Code (glbasic) Select
DIMDEL  asteroides[], -1

But I think you want to actually remove the current one being checked.  If so, you may have to add a new variable to your asteroid type, to indicate the array position, but then it may get crazy and other code may break when the ids are no longer continuous.

You also should move your collision detection outside of your asteroid TYPE, and do a loop such as:
Code (glbasic) Select
// Check each asteroid . . .
FOREACH asteroid IN asteroides[]
// and check if it is colliding with each bullet.
FOREACH disparo IN disparos[]
// If they are colliding . . .
IF SPRCOLL(1, asteroid.x, asteroid.y,   0, disparo.posX, disparo.posY)
// Remove asteroid
DELETE asteroid
// Remove bullet
DELETE disparo
NEXT
NEXT
NEXT


I hope this gets you closer!   Good luck - isn't game programming fun!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Jonás Perusquía

#6
Thanks for your help Slydog :3

i made this:

Code (glbasic) Select
FOR contadorasteroides=0 TO LEN(asteroides[])-1
FOR contadorDisparos=0 TO LEN(disparos[])-1
IF (SPRCOLL(1, asteroides[contadorasteroides].x, asteroides[contadorasteroides].y,   2, disparos[contadorDisparos].posX, disparos[contadorDisparos].posY))
DIMDEL  asteroides[],contadorasteroides
DIMDEL  disparos[],contadorDisparos
ENDIF
NEXT
NEXT



and it seems to work, but when reaching a numer of total "disparos" the game closes...  dont know why :/

EDIT:

debug mode:
Code (glbasic) Select
"C:\Users\JonasPm\Documents\GLBasic\DitraFall\DitraFall.gbas"(481) error : Out of DIM array

seems like it ends when the array is... over?

<HTML><BASIC><EC>
Be free and do good things

Jonás Perusquía

FIXED :D

just added a "restart" when sprites collide... see:

          contadorDisparos=0
          contadorasteroides=0

Code (glbasic) Select
FOR contadorasteroides=0 TO LEN(asteroides[])-1
FOR contadorDisparos=0 TO LEN(disparos[])-1
IF contadorasteroides>0 AND contadorDisparos>0
IF SPRCOLL(1, asteroides[contadorasteroides].x, asteroides[contadorasteroides].y,   2, disparos[contadorDisparos].posX, disparos[contadorDisparos].posY)
DIMDEL  asteroides[],contadorasteroides
DIMDEL  disparos[],contadorDisparos
contadorDisparos=0   //Added
contadorasteroides=0   //Added
ENDIF
ENDIF
NEXT
NEXT
<HTML><BASIC><EC>
Be free and do good things