Posted: 22nd Jun 2007 4:03
Hi,


I have a problem in my code. Basically, all I wanted it to do was have the block move back and forth until the enter key is pressed. Then it should show the next block and that blocks moves back and forth until th enter key is pressed. For some reason, I get an error in the code. Can anyone help me figure out what\'s wrong.

+ Code Snippet
`set display mode to 1280 by 1024 with 32 bit colour
set display mode 1280,1024,32

`go to subroutine declare variables
gosub declarevar

`make the camera and position it with the var camy
make camera 1
position camera 1,0,camy,-20


`make 4 object 1 main, 2 sides, and the base
make object cube lvl,10
make object cube right,10
make object cube left,10
make object cube base,10

`hid ethe two side object and make the base be alpha mapped
hide object left
hide object right
set alpha mapping on base, 50



make object cube 6,10
make object cube 7,10
make object cube 8,10
hide object 6
hide object 7
hide object 8
position object 6,0,10,20
position object 7,0,20,20
position object 8,0,30,20


`position all the object at appropriate positions
position object lvl,x#,0,20
position object right,30,sy,20
position object left,-30,sy,20
position object base,0,-10,20





do

`call the two subroutines to move forward and backward
gosub moveforward
gosub moveback



loop

`***************************************************
`******************Subroutines**********************
`***************************************************
declarevar:

   `declare object # of base
   base as integer
   base=4

   `declare object # of right hidden block
   right as integer
   right=2

   `declare object # of left hidden block
   left as integer
   left=3

   `declare variable x# as decimal (x is the x position of the current object)
   x# as float
   x#=0

   `declare variable for setting camera height
   camy as integer
   camy=10

   `declare variable for the current block
   lvl as integer
   lvl=5

   `declare the height or y position of current block
   y as integer
   y=0

   `declare the height or y position of side blocks
   sy as integer
   sy=0

return

`SUBROUTINE MOVE FORWARD
moveforward:

`repeat the following until
repeat

   `increase the x# variable upon every loop by 0.02(speed)
  x#=x#+0.02
   `reposition current object (lvl) with the new corrdinates
   position object lvl,x#,y,20

   `if enter is pressed go to subroutine nextlvl


   if returnkey()=1

         gosub nextlvl

   endif

`continue the above until current object(lvl) and side block 2 collide
until object collision (lvl,2)=1

return


`SUBROUTINE MOVE BACKWARD
moveback:

repeat

   x#=x#-0.02

   position object lvl,x#,y,20




   if returnkey()=1

         gosub nextlvl

   endif


until object collision (lvl,3)=1



return



nextlvl:

set alpha mapping on lvl, 50


y=y+10
lvl=lvl+1





show object lvl

position object lvl,0,y,20

position object right,30,10,20
position object left,-30,10,20


return


Any help would be much appreciated.

Data
Posted: 22nd Jun 2007 4:04
What does the error message say?
Posted: 22nd Jun 2007 4:05
It says Runtime Error 7008 - Object does not exist at line 160.
Posted: 22nd Jun 2007 4:14
until object collision (lvl,2)=1



Where is this object 2?
Posted: 22nd Jun 2007 4:19
my variable 'right' equals 2 so 'object right' is actually object 2. I did this because its easier to know what each object does.
Posted: 22nd Jun 2007 4:31
Oh, my bad. Maybe declaring it as a global might help.
Posted: 22nd Jun 2007 5:50
I declared it just a global but to no avail. Thanks for the suggestion though.
Posted: 22nd Jun 2007 6:17
Ok, I'm getting desperate now, but at least I have access to my PC with DBpro.

I found that the object that doesn't "exist" is lvl. I also noticed that "lvl" is located outside of the user input sub. So I bet if you created the object before going to the next sub, then you could modify it in sub "next level".

If that doesn't work, try making a global for lvl.

And if that doesn't work, then I've no idea.

EDIT: I did manage to find something, I took out the line "lvl=lvl+1" and it did... something. Maybe it was looking at lvl as a different number. Perhaps making lvl a constant would help.
Posted: 22nd Jun 2007 13:54
A variable can be used to create an object, but it is not really a good idea to use an incrementing variable that is named the same as your object reference variable.

Your code is trying to show object 2 (as lvl as increased in value,) but object 2 does not exist yet. You will either have to create another object, or use a different variable than the one that is used as your object reference.

If you add another make object command to line 159 your error will go away as proof.
Posted: 22nd Jun 2007 14:26
Just changed a few thing in your code, not sure if this is what you want:

+ Code Snippet
`set display mode to 1280 by 1024 with 32 bit colour (TOO BIG FOR ME)
set display mode 1024,768,32

`go to subroutine declare variables
gosub declarevar
global flag as boolean


`make the camera and position it with the var camy
make camera 1
position camera 1,0,camy,-200


`make 4 object 1 main, 2 sides, and the base
make object cube lvl,10
make object cube base,10

`hid ethe two side object and make the base be alpha mapped
set alpha mapping on base, 50

make object cube 6,10
make object cube 7,10
make object cube 8,10
hide object 6
hide object 7
hide object 8
position object 6,0,10,20
position object 7,0,20,20
position object 8,0,30,20


`position all the object at appropriate positions
position object lvl,x#,0,20
position object base,0,-10,20

do

   `move forward and backward
   if object position x(lvl)< -20 or object position x(lvl)> 20
      if move < 1
         inc move
         else
         dec move
      endif
   endif

   if move < 1
      x#=x#+0.02
      else
      x#=x#-0.02
   endif

   position object lvl,x#,y,20

   if returnkey()=1 and flag = 0

      set alpha mapping on lvl, 50

      y=y+10
      if lvl > 7 then lvl = 3
      lvl=lvl+1

      show object lvl

      position object lvl,0,y,20
      set alpha mapping on lvl, 100

      flag = 1

   endif

   if returnkey()=0 then flag = 0

loop

`***************************************************
`******************Subroutines**********************
`***************************************************
declarevar:

   `declare object # of base
   base as integer
   base=4

   `declare variable x# as decimal (x is the x position of the current object)
   x# as float
   x#=0

   `declare variable for setting camera height
   camy as integer
   camy=10

   `declare variable for the current block
   lvl as integer
   lvl=5

   `declare the height or y position of current block
   y as integer
   y=0

   `declare the height or y position of side blocks
   sy as integer
   sy=0

return
Posted: 22nd Jun 2007 14:35
you had part answered your own question in the first post, as its to do with the return key press, because the machine run very fast a quick tap of the return key looks like a quick tap to us but to the machine you have done it 100 times or more.

So the bit you need to look at is to do with return key, need a switch.

heres what i added to your code to see what was going on, this slow it down just enough to see whats happening.

+ Code Snippet
   `set display mode to 1280 by 1024 with 32 bit colour
set display mode 1280,1024,32

`go to subroutine declare variables
gosub declarevar

`make the camera and position it with the var camy
make camera 1
position camera 1,0,camy,-20


`make 4 object 1 main, 2 sides, and the base
make object cube lvl,10
make object cube right,10
make object cube left,10
make object cube base,10

`hid ethe two side object and make the base be alpha mapped
hide object left
hide object right
set alpha mapping on base, 50



make object cube 6,10
make object cube 7,10
make object cube 8,10
hide object 6
hide object 7
hide object 8
position object 6,0,10,20
position object 7,0,20,20
position object 8,0,30,20


`position all the object at appropriate positions
position object lvl,x#,0,20
position object right,30,sy,20
position object left,-30,sy,20
position object base,0,-10,20





do

`call the two subroutines to move forward and backward
gosub moveforward
gosub moveback



loop

`***************************************************
`******************Subroutines**********************
`***************************************************
declarevar:

   `declare object # of base
   base as integer
   base=4

   `declare object # of right hidden block
   right as integer
   right=2

   `declare object # of left hidden block
   left as integer
   left=3

   `declare variable x# as decimal (x is the x position of the current object)
   x# as float
   x#=0

   `declare variable for setting camera height
   camy as integer
   camy=10

   `declare variable for the current block
   lvl as integer
   lvl=5

   `declare the height or y position of current block
   y as integer
   y=0

   `declare the height or y position of side blocks
   sy as integer
   sy=0

return

`SUBROUTINE MOVE FORWARD
moveforward:

`repeat the following until
repeat

   `increase the x# variable upon every loop by 0.02(speed)
  x#=x#+0.02
   `reposition current object (lvl) with the new corrdinates
   position object lvl,x#,y,20

   `if enter is pressed go to subroutine nextlvl


   if returnkey()=1

         gosub nextlvl

   endif

`continue the above until current object(lvl) and side block 2 collide
until object collision (lvl,2)=1

return


`SUBROUTINE MOVE BACKWARD
moveback:

repeat

   x#=x#-0.02

   position object lvl,x#,y,20




   if returnkey()=1

         gosub nextlvl

   endif


until object collision (lvl,3)=1



return



nextlvl:

set alpha mapping on lvl, 50


y=y+10
lvl=lvl+1
rem added this bit <<<<<<<<<<<<<<<<<<<<<
print lvl
sync
wait key

rem <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

show object lvl

position object lvl,0,y,20

position object right,30,10,20
position object left,-30,10,20


return


Sasuke beat me to it, and put in the answer.
Posted: 22nd Jun 2007 14:39
You edited before I could catch you out HowDo.
Posted: 22nd Jun 2007 20:55
thanks for the help guys


Just a question, on Sasuke's example, I notice it does exactly what I want except that once it gets to a certain hieght the blocks from the bottom disappear and are used at the top, I know its a silly question but how would you adjust this so that tehy just continue stacking (basically, this is the beginning of a jenga like game so the blocks need to continue stacking one on top of the other)?
Posted: 23rd Jun 2007 5:27
What you want it to do is actually considerably less complicated than the other example because you do not need to worry about changing the value of lvl. You could simply keep lvl as object 1 the entire time, and every time you hit Return, it increases another value (2...3...4...etc) and creates a new object at the same position as your lvl object. Then, you would move your lvl object up.

****OR****

if you want to make lvl change and control different blocks, you could:
increase lvl by one and make a new object with this number. Make sure that you position it above the other blocks AFTER you create it, or you will get the Object Does Not Exist error because you havent created it yet.


Hope this was helpful and I didn't totally miss the point of your question / write an answer that is making a simple task complicated.
Posted: 23rd Jun 2007 12:34
I think I would be best to make all the cubes you need before hand and do the managing after.

An example:
+ Code Snippet
set display mode 1024,768,32

`Setup Camera
autocam off
position camera 0,0,100,-200

global flag as boolean

`Stack varibles
MaxStackHeight = 20   `This include the base cube
CubeSize = 10         `General cube size
NextCube = 1

TopCube as integer : TopCube = 1
make object cube TopCube,CubeSize

BaseCube as integer : BaseCube = 2
make object cube BaseCube,CubeSize
set alpha mapping on BaseCube, 50
show object BaseCube

for i = 3 to 3+MaxStackHeight
   make object cube i,CubeSize
   hide object i
   exclude object on i
next i

`position all the object at appropriate positions
position object TopCube,0,0,20
position object BaseCube,0,-10,20

do

   `move forward and backward
   if object position x(TopCube)< -20 or object position x(TopCube)> 20
      if move < 1
         inc move
         else
         dec move
      endif
   endif

   if move < 1
      x#=x#+0.02
      else
      x#=x#-0.02
   endif

   position object TopCube,x#,y,20

   if returnkey()=1 and flag = 0

      if NextCube < MaxStackHeight
         inc NextCube
         inc y,CubeSize

         set alpha mapping on TopCube+NextCube, 50
         exclude object off TopCube+NextCube
         show object TopCube+NextCube
         position object TopCube+NextCube,object position x(TopCube),object position y(TopCube),object position z(TopCube)

         position object TopCube,object position x(TopCube),object position y(TopCube)+y,object position z(TopCube)
      endif

      flag = 1

   endif

   if returnkey()=0 then flag = 0

loop


I must be getting close now, hope this is useful.
Posted: 24th Jun 2007 14:41
http://forum.thegamecreators.com/?m=forum_view&t=103556&b=10
Posted: 24th Jun 2007 16:50
Thanks for the help, and Sonics I understand what you mean, if you looked at my other posts they usually have decent titles but this one I honestly had no idea what to call it because I had no idea what the problem was.