Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / This is my code.Why won't it work

Author
Message
soapyfish
21
Years of Service
User Offline
Joined: 24th Oct 2003
Location: Yorkshire, England
Posted: 28th Dec 2003 15:22 Edited at: 28th Dec 2003 15:25
Hey all,I have another post on this page asking about scoring and no-one seemed to be able to help me(ignore this bit I just checked the post and Mentor told me what to do.) so I thought I'd tidy up my code a bit while I was working it out.I've tried using functions(for the first time) and now only part of my code works.The collision with other planes works fine but the bulet movement dosn't and my plane and the enemy plane always start at the left hand side of the screen. I was wondering if someone could tell me what was wrong with my code. Any help would be great. Thanks

"fireball"
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 28th Dec 2003 17:20 Edited at: 28th Dec 2003 17:22
without looking too hard (since if helps if you work these things out yourself ), you have forgot that you have to import your variables into a function, for example in player movement you have PLX as a variable, but the function doesn`t know what PLX is!, remember that inside a function it uses its own variables, they can have the same name as the variables in the main program, but hold their own values that can be toaly different, change your functions to gosubs and it will start to work again.
to pass a value to a function (in this example 53) do this

printnumber(53)
do
loop

function printnumber(number)
print number
endfunction

what you did was

number=53
printnumber()
do
loop

function printnumber()
print number
endfunction

but you never sent the values to the function, you might wonder why the function uses it`s own values, but imagine you have a section of code to move bullet sprites, you can write the function to just take the spritenumber, and it will move all kinds of bullet sprites without you having to worry about if you just used the same variable name in the function as you used in the main code, since they are allways treated seperatley.

Mentor.

System spec : Pentium 3.0Ghz, 512MB DDR, 2x160Gb HD (using icewave hd coolers ), DVD RW/CD RW (all modes), multimedia front panel, 6 way surround sound, ATI radeon 9800Pro 128mb.
soapyfish
21
Years of Service
User Offline
Joined: 24th Oct 2003
Location: Yorkshire, England
Posted: 28th Dec 2003 17:49
What can I say.A true master at work and nothing less.

"fireball"
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 28th Dec 2003 20:33 Edited at: 28th Dec 2003 20:35
As a follow up...

While it may be bad coding, sometimes you may really want to have a true global variable that is the same variable in your main code as in your function.

Back in the main code, you can declare a variable like this:

global cursorID as integer

The variable cursorID could then be referenced anywhere: main code, sub routines, or functions.

It's not something that you would want to use all the time. But, it is there if you need to use it. IIRC, you can not pass user defined types to functions. So, using a global for some UDTs is handy.

A third alternative might be using constants as well.
--
TAZ
soapyfish
21
Years of Service
User Offline
Joined: 24th Oct 2003
Location: Yorkshire, England
Posted: 28th Dec 2003 23:28
Thanks.

"fireball"
schoenhs
20
Years of Service
User Offline
Joined: 29th Nov 2003
Location:
Posted: 29th Dec 2003 01:26
in point of fact its not acually bad coding to have a global variable... its not good to make something like

global a = 5

function printa()
print a
endfunction

but there are plenty of instances where it saves many many lines of code and simplifies the code making it more readable to include a global variable
soapyfish
21
Years of Service
User Offline
Joined: 24th Oct 2003
Location: Yorkshire, England
Posted: 29th Dec 2003 01:33
k

"fireball"
soapyfish
21
Years of Service
User Offline
Joined: 24th Oct 2003
Location: Yorkshire, England
Posted: 29th Dec 2003 01:37
while your all feeling helpful could you help me find out why the score won't print to the screen in this code.Thanks.

"fireball"
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 29th Dec 2003 02:47 Edited at: 29th Dec 2003 02:47
your snippet lacks the player score gosub.

A real simple one would be like this.
It uses the text command to place the score from a variable playerscore in the top middle center of the screen.
the x is centered from the current screen width
the 10 is the y










As soon as you get multiplayer ideas create an array to store the
player scores or incorporate it into your typed array.

eg:

dim Player(4)
for i = 0 to 4 : Player(i) = i : next i

note: arrays start with a zero but for simplicity lets start at 1 for player 1 and 2 for player 2 and the neat thing for doing it this way is you can reserve the zero location for the computer and so incorporate computer moves into a list of player numbers.

when you need to reference a players score you just alter the number in the array location to show that particular score... easy and neat to code instead of a tonne of variables for each player.

learn arrays and types, and then learn types with arrays or typed arrays for even better multiple instance handling.


fun stuff to learn, dont think of it as a chore but new ways of handling data without huge amounts of code.

Login to post a reply

Server time is: 2024-11-25 22:43:43
Your offset time is: 2024-11-25 22:43:43