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.

DarkBASIC Discussion / Whats wrong with this code?

Author
Message
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 19th Nov 2007 07:52
[code/]
Load bitmap "leftface.bmp",1
get image 1,0,0,30,48
x1 = 170
y1 = 90
sprite 1,x1,y1,1
repeat
sprite 1,x1,y1,1
if rightkey() = 1
x1 = x1 + 1
endif
until rightkey() = 0


I know that there are tons of things wrong with it, and im completely new to programming and this forum in general so might as well say hi

When I run the program it stops unless and start the program while pressing the right key
Another thing is that when I press the right key everything the sprite dissapears, thanks ahead for your help, and anything else you would suggest would be great.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 19th Nov 2007 12:22
First, let me welcome you to the forums. I hope you have a lot of fun playing around with DarkBASIC

Second, let's look at your code...
This is what i would do...



I've added a few things - Sync on means that DB will update the screen instead of leaving it for Windows (which doesn't want to know about DB). Sync at the end of the Loop means that DB will update the screen and draw the sprite.

The main problem was that the code would see if it should increase x1 (which is what you wanted) and then check to see if the rightkey was pressed. If the rightkey wasn't pressed - and in the first 1/40th of a second of running, it couldn't possibly be - then the Loop would finish and DB would move on to whatever followed the repeat command. Since there was nothing, the program would end.

So, in order to keep the program running you need to use Do...Loop instead of Repeat...Until. To end the program, there's an opptional line of code you can add to the Loop



Shove that before the sync command of the main loop and if at any point you press space, the loop will exit and the program will end.

Hope this has been of help!

On our way 'ome, on our way 'ome...
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 19th Nov 2007 21:00
Thanks a lot! That helped with that part of the problem, but how should I do it for him moving around everywhere
[code/]
Sync On
Load bitmap "leftface.bmp",1
get image 1,0,0,30,48
x1 = 170
y1 = 90
sprite 1,x1,y1,1

Do
sprite 1,x1,y1,1
if rightkey() = 1
x1 = x1 + 1
endif
sync
Loop

Do
sprite 1,x1,y1,1
if leftkey() = 1
x1 = x1 - 1
endif
sync
Loop


this code doesnt work because it immediately goes into the first loop, and if I was to exit the loop while rightkey() = 0 then it would immediately exit out of the loop? What type of loop should I use to fix this problem?
Link102
20
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 20th Nov 2007 00:35 Edited at: 20th Nov 2007 00:39
sorry for the double post, browser is acting funny

Don't look at my sig!
Link102
20
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 20th Nov 2007 00:38 Edited at: 20th Nov 2007 00:40
Put the leftkey code in the first loop

sike so


btw:
if condition
code
endif

is the same as

if condition then code

Don't look at my sig!
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 20th Nov 2007 05:31
aha thx a lot
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 20th Nov 2007 22:04
Argghh Im kind of frustrated now! I tried to load in a new bitmap into darkbasic and for some reason it says that the file doesn't exist? Has anyone had that problem before. I cant figure it out.
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 24th Nov 2007 11:11
check the path and the file extension, or check for a typo in your filename. It's generally something small like that...

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 24th Nov 2007 23:52
A small tip:

1. Open up Windows Explorer and locate the file you are trying to load into DB.

2. Check that you are using the same path in DB as appears on the Address line at the top of the Windows Explorer window.

But...

It's infinitely better to copy all media files into the same directory as your DB program - then the Load lines don't need a path.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Nov 2007 01:04
Quote: "check the path and the file extension, or check for a typo in your filename. It's generally something small like that..."

Windows annoyingly hides file extensions, make them visible by opening a window and clicking
Tools > Folder Options > View
then un-check "Hide extensions for known file types"


"You must be someone's friend to make comments about them." - MySpace lied.
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 25th Nov 2007 03:43 Edited at: 25th Nov 2007 06:44
Thanks a lot guys! I got another question for you though so please get back to me as soon as you can

first of all here is the code(How do you do code boxes?)




just ignore the code between the rems. It compiles and everything but the image shows up as a white box? It works fine with just one sprite but when I try to do the second it shows up white.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th Nov 2007 06:43 Edited at: 25th Nov 2007 06:48
Quote: "How do you do code boxes?"


1. Paste the code
2. Highlight the code with the mouse
3. Click the code button

You are confusing Load Bitmap with Load Image. Make your images the correct size and use Load Image.

Your program is loading the bmp files into 'screens' (numbers 1 and 2), but as you aren't using Set Current Bitmap you are trying to grab them from screen (bitmap) 0.

You need to read the help file sections on Bitmaps and a few tutorials (see the stickies at the top)...

TDK_Man

1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 25th Nov 2007 20:45
I read some tutorials but they seem like they are more geared towards dbpro. I interchanged the load bitmap for load image, and since load image never atoumatically goes onto the screen I put show sprite 1. I thought I had stubbled onto a great tutorial, something on a 2d rpg, but I could find anything on loading the images and putting them into sprite?

Load image "jesusleft1.bmp",1
load image "jesusright1.bmp",2
get image 2,0,0,45,45
get image 1,0,0,45,65
x = 170
y = 90
sprite 1,x,y,1
sprite 2,x,y,2
show sprite 1
hide sprite 2
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th Nov 2007 21:33
Quote: "1. Paste the code
2. Highlight the code with the mouse
3. Click the code button"


You asked how to do it then didn't do it when you were told!

Load Image
Paste Image (or use image number in Sprite command). Why would you want to use Get Image when you've already 'got' it?

Quote: "I read some tutorials but they seem like they are more geared towards dbpro."


My tutorials are for DBC and tweaked a bit for DBP users.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Nov 2007 21:46
Quote: "Load image "jesusleft1.bmp",1
load image "jesusright1.bmp",2
get image 2,0,0,45,45
get image 1,0,0,45,65 "

You have already loaded images 1 and 2 so using GET IMAGE will overwrite them. Once you have loaded an image it exists in the program, you don't have to do anything else with it

Quote: "sprite 1,x,y,1
sprite 2,x,y,2
show sprite 1
hide sprite 2"

This looks like you are planning to hide sprite 1 and show sprite 2 when jesus moves. There is actually a much simpler way of showing animation.
A sprite is a 2D object, it can be assigned any image. Sprites are like shape-shifters, they take on the form of whatever image you assign to them.

Using a variable to assign the image means that it can be changed within the program. To begin with the jesus sprite will appear as "jesusleft1.bmp", but we could change the image variable to 2 when we press the rightkey(), and so jesus would now be facing right.


If your Jesus is symmetrical you could use one image and mirror it so that he is facing the other direction.

This halves the amount of images you need
We would still use the jesus_image variable for when we want to animate Jesus.

"You must be someone's friend to make comments about them." - MySpace lied.
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 26th Nov 2007 00:53
Thanks Obese that really helped a lot! And sorry about that TDK when I posted that I just remembered that you had told me how to put it into code boxes! lol *shoots myself*
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 26th Nov 2007 01:12
Umm I got kind of exited because I really thought that I finally got this, but I dont think that get image will work since im loading in an entire page and I have to cut out the sprite from there, but I still thought that this would work but for some reason it isnt, I just get that same white box.



Seriously is this really that complicated or am I just dumb as crap? What the hell is wrong with this code???
aluseus GOD
18
Years of Service
User Offline
Joined: 19th Mar 2007
Location: I\'m here. Now I\'m there. I keep moving
Posted: 26th Nov 2007 01:28
Your not dumb as crap, just inexperienced. This is how you would code it.



Or, if you are insistent on using bitmaps-



Of course, since I have no idea how large your second image is, thid doesn't load the second image.

Plz don't copy paste, look at how it's done.

alus.portbb.com go there.
Quote: A book. I hate books. book is stupid. I know that I need codes but I dont know the codes -zenicanin14 the stupidest user in the world
1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 26th Nov 2007 04:47
Believe me I didnt just copy paste the code, really looked at it and I see how it could work



Its still not doing what I expect though, I went through each line and saw what it did. when I ran it it showed bitmap 1, if I pressed right or left on the keyboard it would simply dissapear? I also dont see why I need the set current bitmap lines
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Nov 2007 08:36
OK, let's see if we can sort this out once and for all...

Load bitmap "jesusleft1.bmp",1

This will load the file "jesusleft1.bmp" into Bitmap (screen) 1. You cannot see it as you only ever see bitmap 0 (zero).

load bitmap "jesusright1.bmp",2

This will load the file "jesusright1.bmp" into Bitmap (screen) 2. You cannot see it as you only see bitmap 0 (zero).

Set Current Bitmap tells DB which bitmap you want to work with. Note that I said 'work with' not see!

After you use Set Current Bitmap 1, then the Get Image command (or any other screen command) will use bitmap 1.

So, you set the current bitmap and then use the Get Image command to grab your sprite image.

If your second image is loaded into bitmap 2 then you must use Set Current Bitmap 2 and then repeat the Get Image command with a different image number.

As I said before, this is a bit of a waste of time and energy when you can simply create and save the two images the correct size in the first place and then just use Load Image.

Bitmaps are intended for pages of sprites where the image you load has dozens of smaller images on it. If you only have one small image in your bmp, then it's pointless using Load Bitmap.

You are making some very basic errors - one of which will cause nesting errors. Indent your code properly and they will be easy to spot.

For example, take a look at your main Do..Loop:



Indented correctly you can easily see that you are only checking to see if the right cursor key is pressed while the left cursor key is pressed!

Sorry if they are boring, but you really do need to read a few tutorials to get a good knowledge of the fundamentals before you go any farther.

TDK_Man

1337 programm3r
17
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 27th Nov 2007 01:07
Ahh thank you TDK, My program pretty much works as I want it to now, I just wanted to that the reason I am using load bitmap, is because the sprite is on a paint file, then I moved it into the darkbasic folder. I tried loading up a scrap but it wouldnt work. Am I able to solve this? Thanks again you definitely cleared almost all of this up, I see how I need to set the bitmap before I cut out the image, I think that was most of the problem.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 28th Nov 2007 18:33
Quote: "I tried loading up a scrap but it wouldnt work. Am I able to solve this?"


Not sure what you mean by a 'scrap'.

What you can do is write a separate program which loads your Paint file into a bitmap, uses Get Image to create the smaller image for the sprite, then use:

Save Image "DifferentFilename.bmp",ImgNumUsed

...to save the image you have just grabbed to disk.

Repeat this for both large Paint files and you will have two smaller .bmp files which you can then use in your main program with the Load Image command - instead of loading bitmaps and cutting out the sprite images.

If you decide to stick with the method you are using at the moment, don't forget to use Set Current Bitmap 0 to switch back to the default screen then Delete Bitmap 1 and Delete Bitmap 2 to delete the two bitmap screens (because you don't need them again).

TDK_Man

Login to post a reply

Server time is: 2025-06-05 18:14:56
Your offset time is: 2025-06-05 18:14:56