Posted: 18th Jun 2007 20:16
Hi,
I wrote the following code to move a cube basically back and forth right over another cube. After I wrote it I said to myself there has to be a more efficient and hopefully shorter way to write something so simple. Also, because of the timer functions I used, I am having a lot of trouble doing anything in the main loop because the program goes through the repeat until loops and can't do anything else simultaneously. Any help would be much appreciated.


+ Code Snippet
camz = -20
`set Screen resolution to 1500 by 110 and set colour mode to 32 bit
`Change these settings to make text and sprites appear bigger or smaller in finer detail
`Change the 32 bit colour depending on the colour modes your computer supports
set display mode 1500,1100,32

`creates a 'camera' and positions it(x,y,z) to view objects
make camera 1
position camera 1,0,0,camz

`declares and sets the value for x#
x# as float
x#=0

`rem makes a cube and positions it
make object cube 1,1
position object 1,0,-2,10

`makes a second cube that will move and positions it with the variable x#
make object cube 2,1
position object 2,x#,-1,10








`main loop
do

`calls function to move the top block back and forth
moveblocks()




loop


function moveblocks()
x# as float
x#=7.0


T=Timer()


Seconds=2

Repeat


  Elapsed=(Timer()-T)/1000
  TimeLeft=Seconds-Elapsed
  Text 0,0,Str$(TimeLeft)+"  "
  x#=x#-0.01
  position object 2,x#,-1,10
  if returnkey()=1 then suspend for key


Until TimeLeft=0




T=Timer()



Seconds=2

Repeat

  Elapsed=(Timer()-T)/1000
  TimeLeft=Seconds-Elapsed
  Text 0,0,Str$(TimeLeft)+"  "
  x#=x#+0.01
  position object 2,x#,-1,10
   if returnkey()=1 then suspend for key


Until TimeLeft=0


`closes the function
endfunction



Thanks
Data
Posted: 18th Jun 2007 20:23
I would like to help.
I've tried your code.
But I don't get what its trying to do or what you're trying to achieve.

Cowbox
Soharix HQ
http://www.soharix.homestead.com/
Posted: 18th Jun 2007 21:13
Try this:
+ Code Snippet
sync on : sync rate 60
autocam off

global GlobalTimer as float
GlobalTimer = timer()

Lasttick as float
Lasttick = 0

speed as float
speed = 0.5

make object cube 1,10
position object 1,35,0,128

position camera 0,0,0

do

   x# = speed

   if (timer_seconds() - Lasttick > 2.0)
      Lasttick = timer_seconds()
      if n > 0
         dec n
            else
         inc n
      endif
   endif

   if n > 0
      position object 1,object position x(1)+x#,object position y(1),object position z(1)
         else
      position object 1,object position x(1)-x#,object position y(1),object position z(1)
   endif

   text 0,0,""+str$(timer_seconds())
   text 0,20,""+str$(object position x(1))

   sync
loop

function timer_seconds()
   time# = (timer()-GlobalTimer)/1000.0
endfunction time#

function move_object(id,x#,y#,z#)
   position object id,x#,y#,z#
endfunction


Hope that helps.
Posted: 18th Jun 2007 21:42
thanks Sasuke that helps