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.

2D All the way! / Losing Collision Detection when moving code to function

Author
Message
badkneecap
13
Years of Service
User Offline
Joined: 21st Jun 2010
Location:
Posted: 21st May 2011 00:09
I'm writing a 2D game to learn the DBP language. I originally wrote the code in 1 big loop. I then wanted to separate the code into functions so I could keep the logic clear. However, as soon as I do that, the collision detection stops working. I went from this



to this

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 21st May 2011 02:10 Edited at: 21st May 2011 02:14
Your problem is to do with scopes. Variables declared outside of a function will not be used by that function unless a) are globals or b) passed in as parameters.

Here is an example


The solution is to declare any variables that you wish for them to keep their values globally.

On an extra note, user-defined types and constants are global by default.

A clever person solves a problem, a wise person avoids it - Albert Einstein

Login to post a reply

Server time is: 2024-03-02 07:51:57
Your offset time is: 2024-03-02 07:51:57