sorry U.U need help again...

Previous topic - Next topic

Hb!

Here's something i cannot achieve, I want to create an animation each time the key(30) is pressed instead of "as long as key(30) =true",  .. I dont know if I explained myself in the right way, my english isnt good at all, heres the code, what am i doing wrong?
how would you do it?
Code (glbasic) Select
FOR a=1 to 5
IF KEY(30)
DRAWSPRITE a, -20, 255
SHOWSCREEN
ELSE
DRAWSPRITE 1, -20, 255
SHOWSCREEN
ENDIF
That one up there is the desired effect, but how do I make I true every time the key is pressed?
("while true" doesnt give me the desired animation because it repeats it as long as the key(30) is still true).

bigsofty

Something like this...?

Code (glbasic) Select
IF KEY(30)
 MOD(currentframe,5) // 0-4= running man for example
 currentframe=currentframe+1
ELSE
 currentframe=10 // 10=idle man stopped
ENDIF

DRAWSPRITE currentframe, -20, 255
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Hb!

yop, thanks.. tsk, Does anyone know of any page of tutorials? another aside from the one included in Gl? 'cause I think I really need to understand many things, im still having problems .even with the book I got, some facts are still not clear for me, especially with Dim related topics (yes I know it makes something like a checkerboard and its used to store variables, but I still cannot understand how to use it) I really hate bothering people, but sometimes I cant stop doing it. U.U... well Ill keep working anyways, My dream has to become true  one day or another.. Im gonna fight for it.

bigsofty

Hehe, your not bothering anyone bud! Just ask away... oh and dont worry about asking about any specific code or command.

Now, the best way I learned is not to try and force a unfamiliar command into your new game. The best way is to stop, read the docs and then write a very small test program that allows you understand the syntax and the command.

For example dim...

Consider this program...


Code (glbasic) Select
GLOBAL myvar
myvar=3
PRINT "result="+myvar, 10, 10
SHOWSCREEN
KEYWAIT
This is pretty obvious, create a var, put a value in it and print it. Here's the same program using arrays...

Code (glbasic) Select
DIM myvar[1]
myvar[1]=3
PRINT "result="+myvar[0], 10, 10
SHOWSCREEN
KEYWAIT
This is kinda silly but it does illustrate how arrays and vars are just the same, a place to store values. In this case I've create a list of the length of 1, to store a single value.

*Note* Oh why put '[1]' in the declaration but use
  • in the reference? The [1] is the length of the list (or array), the actual list starts 0, so
  • is the first item in the list.

Now what if I wanted to store 4 values, I could use 4 variable but this is where arrays start to make sense...


Code (glbasic) Select
DIM myvar[4]
myvar[0]=3
myvar[1]=5
myvar[2]=6
myvar[3]=7
PRINT "result array 0="+myvar[0], 10, 10
PRINT "result array 1="+myvar[1], 10, 20
PRINT "result array 2="+myvar[2], 10, 30
PRINT "result array 3="+myvar[3], 10, 40
SHOWSCREEN
KEYWAIT
Now a 2 dimentional array, the list is replaced by a grid of variables, each one capable of holding a value. As its a grid each variable needs a unique 'X' and 'Y' co-ordinate to locate the individual items.

Code (glbasic) Select
DIM myvar[2][2] // 2 by 2 grid = 2*2 = 4 total variables
myvar[0][0]= 3 // 0,0 top left of 2 by 2 grid
myvar[1][0]= 5 // 1,0 top right of 2 by 2 grid
myvar[0][1]= 6 // 0,1 bottom left of 2 by 2 grid
myvar[1][1]= 7 // 1,1 bottom right of 2 by 2 grid
PRINT "result of array 0,0=" + myvar[0][0], 10, 10
PRINT "result of array 1,0=" + myvar[1][0], 10, 20
PRINT "result of array 0,1=" + myvar[0][0], 10, 30
PRINT "result of array 1,1=" + myvar[1][1], 10, 40
SHOWSCREEN
KEYWAIT
Now why use a 2 by 2 array (Grid), why not 'DIM mayvar[4]', well logically some data structures 'fit' into the idea of 2 dimentional array better than others...

3 by 3, Tic,Tac,Toa games
12 by 12, Chess
16 by 16, Battleships
A platform game tile grid, say 100 by 100 for example
RTS game... etc...

All individual tiles are easily refrenced and examined with two numbers, the X and Y co-ordinates of the array.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

bigsofty

Quote from: bigsoftySomething like this...?

Code (glbasic) Select
IF KEY(30)
 MOD(currentframe,5) // 0-4= running man for example
 currentframe=currentframe+1
ELSE
 currentframe=10 // 10=idle man stopped
ENDIF

DRAWSPRITE currentframe, -20, 255
Ha!, just noticed...

These two lines...

Code (glbasic) Select
MOD(currentframe,5) // 0-4= running man for example
 currentframe=currentframe+1
should be reversed... to avoid currentframe overflowing

Code (glbasic) Select
currentframe=currentframe+1
 MOD(currentframe,5) // 0-4= running man for example
doh!
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Hb!

Thanks again, im gonna start testing all dims, yop youre right, its better to study command by command, and then once you manage them very well you start creating the game. That's exactly what im doing, Ive been testing all the time, that means that I havent started my game yet.Finally im able to understand the usage of dim... Im gonna start making tests right away!!!