Hi, I wrote a bit of code to try and demonstrate using functions. I tried to comment things out to show you what it is doing. It is quite simple and does not have gravity or collision or anything. Hopefully this will help.
// function demo
// variables are LOCAL in functions, so make them GLOBAL
global score, count, ScoreDelay
global x# as float
global y# as float
global z# as float
// initialize the variables
score = 0 : count = 0
x# = 500.0 : y# = 45.0 : z# = 23.0
// make an object for us to look at
make object cube 1,10.0
color object 1,rgb(255,255,0)
position object 1,x#,y#,z#
// create a multi-colored 'floor' so we can see the movement
for i = 0 to 10
make object box i + 2,1000.0,10.0,1000.0
position object i + 2,500.0,-5.0,i * 1000.0
color object i + 2,rgb(rnd(128) + 127,rnd(128) + 127,rnd(128) + 127)
next i
// enable manual SYNCing
sync on : sync rate 60
// used to delay the score update - we can't see it as good if it updates too fast
ScoreDelay = timer()
repeat
// go to the function MoveCube and store the passed value in count
// pass the value of the object we want to move
count = MoveCube(1)
// if it is time to add to the score, jump to this function and store the result in the variable 'score'
if timer() > ScoreDelay then score = AddScore(rnd(100) )
// pass the object to follow to the MoveCam function
MoveCam(1)
// no parameters passed
Debug()
sync
until spacekey() = 1
end
function MoveCube(ObjToMove)
// store the value passed to the function in the ObjToMove variable
// it is a LOCAL variable, as we don't need it outside of the function
upk = upkey() : dnk = downkey() : left = leftkey() : right = rightkey()
if upk = 1 then move object ObjToMove,10.0
if dnk = 1 then move object ObjToMove,-10.0
if left = 1
position object ObjToMove,object position x(ObjToMove) - 5.0,object position y(ObjToMove),object position z(ObjToMove)
endif
if right = 1
position object ObjToMove,object position x(ObjToMove) + 5.0,object position y(ObjToMove),object position z(ObjToMove)
endif
inc count
endfunction count
function AddScore(rand)
// store the random value passed in the rand variable and add it to the score variable
inc score, rand
// delay it so that we can actually see the score increase (10 times per second)
ScoreDelay = timer() + 100
endfunction score
function MoveCam(ObjToFollow)
// store the object we want to follow in ObjToFollow (a local variable)
x# = object position x(ObjToFollow)
y# = object position y(ObjToFollow)
z# = object position z(ObjToFollow)
// position the camera at the x and z location of the cube and put it 45 units above it
position camera x#,y# + 45.0,z#
// orientate the camera to the same angles as the cube
set camera to object orientation ObjToFollow
// move the camera back
move camera - 100.0
// point the camera at the cube
point camera x#,y#,z#
endfunction
function Debug()
// this displays thes values so we can see them change
// set the text color to red, so it's a little easier to see
ink rgb(255,0,0),0
text 10,10,"FPS: " + str$(screen fps() )
text 10,100,"CAM x#: " + str$(camera position x(),1)
text 10,120,"CAM y#: " + str$(camera position y(),1)
text 10,140,"CAM z#: " + str$(camera position z(),1)
text 10,200,"score: " + str$(score)
text 10,220,"count: " + str$(count)
endfunction
So many games to code.....so little time.