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 / Multiple objects

Author
Message
Silence
12
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 1st Nov 2012 19:25
Hello! I am having a tiny problem with my code. More or less confusion to be exact. I have three cubes on my screen and I when I hit "s" I want it to switch which cube I can control. How does one do this?



this is what I have so far but the controls dont swich to a different cube
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Nov 2012 01:19
Bad way of writing it. What if you had 100 cubes? Would you really want to rewrite the control routine for every single one?

But as to why yours doesn't work is because your inkey$ is never reached. Your first block contains a do-loop, which means unless you explicitly escape the loop, nothing outside of it will ever do anything.


Look through this code example:


Let's say your three objects are numbered 1 through 3. By using a variable to represent the object number, we can just increment its value whenever you press "S". When you press the "S" key, the computer can register it many times before you release it, cause the action to happen repeatedly. We don't want that kind of behavior so we use vkFlag to prevent that. It's just another variable, what we call a flag. You can only press S if the flag is down. When you press S it raises the flag. When you release the key, the flag goes back down. This is so if you hold down the key, it won't continuously keep switching objects on you.

inc obj_id adds 1 to the value of obj_id. We only want the value to be between 1 and 3, so if it goes over 3, set it back to 1. This will make your object selection loop around from 1 to 3. So if you're controlling object 3, pressing the S key will now control object 1 again.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Silence
12
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 2nd Nov 2012 02:00
Thank you very much for you code. But it's not quite what I wanted. This code makes all three cubes move at the same time and when I hold "S" it will move a different cube each time. What I needed was that as soon as the program started it will move object 1 with the control button and when I hit "S" once it should move object 2 and so on.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 2nd Nov 2012 05:21 Edited at: 2nd Nov 2012 05:28
elaborated on the same notions phaelax offered:



adjust to suit your needs.

oops: fixed it to set the WasReleased flag specifically for the s key. all better now

Virtual Nomad @ California, USA . DBPro V7.7
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Nov 2012 08:26
Quote: "as the program started it will move object 1 with the control button and when I hit "S" once it should move object 2 and so on."

Uhh isn't that what mine does? You just said you wanted all 3 to move at the same time, now you're stating one at a time.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 2nd Nov 2012 12:01 Edited at: 2nd Nov 2012 12:03
Quote: "I am having a tiny problem with my code."

Quote: "

"

Okay, OP, you have some solutions but you need to understand why your code doesn't work so we'll go through this.
Firstly, you've a DO..LOOP with a set of instructions. Fine. But your condition for switching cubes is outside the DO..LOOP so essentially you'll keep looping and never reach that IF INKEY$() = "s" part. Secondly, and this wouldn't be apparent straight away be you are calling GOSUB above your label. This is pointless because the program executes from top to bottom, meaning it'll go to your label anyway. This method of using a label is wrong however, as Phaelax pointed out, for the reasons he stated (as well as the fact that GOTO/GOSUB should be avoided by newcomers).

@Phaelax:
I think you made a typo:
Quote: "
"

You said if inkey$() != 's'... should be ==. I do that too and then the next day I'm like "why won't it work!?" when I compile, lol
My most recent blunder was yesterday evening when I was comparing to string buffers and I had = instead of ==. I read over the code about 5 times and didn't spot it till I ran through it outputting the results for every line. The minute I saw the assignment pop up on screen I thought "Noooo, I checked the code!"
Also, forgot to update the "A1Y#" variable at the end of the controls codeblock.

Just gonna post the corrected code for OP's education:


Anyway, OP, hope this is of use.

RP Functions Library v1.0

Silence
12
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 2nd Nov 2012 16:14
o: thank you to all for your help. And i apologized. i could have stated my problem better. This code works amazingly! I love it!

Login to post a reply

Server time is: 2024-11-21 18:15:58
Your offset time is: 2024-11-21 18:15:58