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 / make object function

Author
Message
farmkid4
15
Years of Service
User Offline
Joined: 23rd Sep 2009
Location: on my pc :)
Posted: 6th Oct 2010 19:09
i am trying to make a basic level editor. Right know im am working on making a cube and my function isnt doing what i want. it makes 10 cubes when i want it to make one cube.


help on how to improve this would be great.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Oct 2010 19:45 Edited at: 6th Oct 2010 19:46
This returns the first free object index:



"everyone forgets a semi-colon sometimes." - Phaelax
farmkid4
15
Years of Service
User Offline
Joined: 23rd Sep 2009
Location: on my pc :)
Posted: 6th Oct 2010 20:21 Edited at: 6th Oct 2010 20:22
thanks for replying. im not having trouble making a free object right now its either makes 1 or more than 1 cube every time i tell it to make one. this is the code that only makes one once and wont make another till i end the program and restart it.


this code makes a couple of cubes every time i tell it to make one.

this is harder than i thought it would be.

farmkid4
15
Years of Service
User Offline
Joined: 23rd Sep 2009
Location: on my pc :)
Posted: 6th Oct 2010 20:35
i think i found the problem.
this is the code i was using to make a cube


For testing i made it print the value of a and watched it when i clicked and found that if i held down the mouse button it just kept making cubes. Is there a way to stop this?

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Oct 2010 21:31
There are some problems with your code.

Firstly, there's no need for the function. All it does is make sure the object doesn't exist before it creates it, and if it does, it won't create it, and you'll later be left confused why this is happening. If you use the FreeObject function I gave you, it will always create a cube with a free object number, and it means you don't have to increment 'a' every time you want to make a new object. The whole point of functions is that you can group bits of code together, so you're not taking advantage of that.

As for your problem, this is because if there is a constant collision between the two sprites, and you're holding down the mouse, you'll always be calling the 'makeobjectcube' function. The way to deal with this is to only make it for the first frame of having the mouse pressed. To do this, you create a boolean variable, 'clicked'. When the mouse is not being clicked, set it to 0. When you want to create a new cube, only create it when clicked is 0, and then set clicked to 1. Take 5 minutes to think about how this works, then figure out the code for it. This is key to good programming.

"everyone forgets a semi-colon sometimes." - Phaelax
luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 7th Oct 2010 07:01
Boolean variables used for checking are flags.You need a flag to determine certain condition in your code and prevent another to happen if there isn't the right flag for it.Example:

mouseclick = 0 clickflag = 0 >> nothing happend(don't need to declare this)
mouseclick = 1 clickflag = 0 >> cube is made, clickflag inc to 1 so if you hold mouse this condition for making the cube is no longer valid
mouseclick = 1 clickflag = 1 >> skip the cube making part(you don't need to declare this condition)
mouseclick = 0 clickflag = 1 >> dec clickflag to 0 once the mouse button is released

Coding is My Kung Fu!
And My Kung Fu is better than Yours!

Login to post a reply

Server time is: 2024-11-24 11:03:12
Your offset time is: 2024-11-24 11:03:12