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 / Help with a simple run on stages; 2D

Author
Message
Pepzi
13
Years of Service
User Offline
Joined: 22nd Feb 2011
Location:
Posted: 29th Sep 2011 14:44
It's me again!
I would like to ask if this would be fine:

gosub setup `the props, images, sounds etc.

gosub Stage1
gosub Stage2
gosub Stage3

Stage1:
`Stage1 codes
Do
...
Loop

Collision:
`Collision with a portal ( when the player reach the portal )
gosub Stage2 `I think the problem would start here?
Return
Return `Return of whole Stage1


Stage2:
`Stage2 codes same with Stage1
Return


Stage3
`Stage3 codes same with Stage1 and Stage2
Return


every stage has subroutines, or I would say subroutines within a subroutine? is that ok? like


Stage1:

subroutine0:
`codes
return

subroutine1:
`codes
return

return `return of the whole Stage1

Is this correct?

I thinking it should be gosub since in the future when I put scores, it needs to be returned right? Will I have a future problem when I do that?

Or any other, better way? Sorry, still a newbie, Thanks you!!

New to darkbasic
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 29th Sep 2011 17:42
I'm afraid you can't nest subroutines like that. The compiler just sees return as a "jump back to where gosub was called from" and ignores any labels it sees on the way. For example:


This code follows something similar to your own code layout - but the "return" after StageOne simply triggers the return to where Gosub Subroutine was called from.

You can do what you want though. Structure your code like this:

It's fine to call GoSub from a subroutine, and you can carve your program up into nice, easy to use chunks. Just be careful on the order of them within the file. As DBP reads through the code file from first line to last, I don't think it can jump to a Subroutine it hasn't yet read.

Hope this helps!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 29th Sep 2011 19:03
Here is some alternative code for you to consider:



This uses the SELECT / ENDSELECT commands along with CASE in order to create effective branching.

Pepzi
13
Years of Service
User Offline
Joined: 22nd Feb 2011
Location:
Posted: 4th Oct 2011 13:15
Thanks sir Robert and sir LFBN

By the way sir LFBN what do you mean by load level 1,
does it mean gosub/goto?

New to darkbasic
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Oct 2011 15:29
You're welcome

Quote: "By the way sir LFBN what do you mean by load level 1,
does it mean gosub/goto?"


My thought was to just load in the levels (or as you say, stages) here. Let's suppose you are making a platformer that has ten different levels. Once you check the portal and determine that the player is at the end of a level, you could increase the value of the variable 'level' and call this function to display it.

Some coders will branch to a function or subroutine that holds all of the stuff needed for an individual level and I sense that you are asking about doing it that way. You could, of course, but it really depends upon personal preference. I normally will control the game from the main game loop and will use a function like this simply to load in the 2D tile map, sprites, etc. necessary to build the level. It's not like the game will branch here and stay here until I complete level 1, for example. My thought was to return to the main game loop.

I never use the GOTO command as it makes for poor programming practice in my view and will cause you to create code that is hard to debug. I strongly suggest you use GOSUBs or FUNCTIONs in your coding. There are those that insist on using GOTO in their games; more power to them, but you are learning and I would be remiss to ignore the reference without saying something.

Pepzi
13
Years of Service
User Offline
Joined: 22nd Feb 2011
Location:
Posted: 4th Oct 2011 15:33
I'm having a problem
Its that when loading the next stage, the platforms/sprites from the previous stage goes/can be seen/stacks with the next stage. The problem is already there before I applied the structure.
I applied similar structure from sir LFBN, its just that the Sprite Collision to my sprite portal loads the next stage

New to darkbasic
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Oct 2011 15:58
You will need to keep track of the platforms/sprites of each stage. Then you can simply delete/hide them before you place the platforms/sprites of the next stage.

Pepzi
13
Years of Service
User Offline
Joined: 22nd Feb 2011
Location:
Posted: 6th Oct 2011 10:42
Thanks! I remembered Andrew Vanbeck's tutorial where he used hide object so I tried hide sprite and it worked!

New to darkbasic

Login to post a reply

Server time is: 2024-11-27 19:20:35
Your offset time is: 2024-11-27 19:20:35