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 functions

Author
Message
Don3784
7
Years of Service
User Offline
Joined: 15th Apr 2013
Location:
Posted: 19th Apr 2013 04:28
ok, trying to create a function for moving the character and collision detection. It gives me a syntax error when I try to use the function. I am fairly new to dbpro so please excuse my ignorance but some help with this would be great.

FUNCTION Charmove(char, char2, char3)

load image char,1
load image char2,2
load image char3,3
sprite 1,xpos,ypos,1
sprite 2,xpos-2,ypos,2
sprite 3,xpos+1,ypos-2,3
sprite 4,xpos+26,ypos,2
sprite 5,xpos+1,ypos+52,3
left = sprite collision(2,0)
up = sprite collision(3,0)
right = sprite collision(4,0)
down = sprite collision(5,0)
If Upkey()=1 and up = 0 then ypos = ypos - 4
if downkey() = 1 and down = 0 then ypos = ypos + 4
if rightkey() = 1 and right = 0 then xpos = xpos + 4
if leftkey() = 1 and left = 0 then xpos = xpos - 4
endfunction
LBFN
13
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 19th Apr 2013 20:07
I would suggest the following:
1. There is no need to load images every time you execute the function. Load them once somewhere else in the code.
2. It appears that you have created sprites simply for collision checking purposes. Perhaps you could save the x and y positions of the main sprite, check for collision and restore the old coordinates if indeed a collision occurs. I don't know what type of game you are making; perhaps you could describe what you want to do in more detail.
3. Depending upon the size of the sprites, you are likely always going to be colliding with another sprite. The only way I can even get it to move is down using 32 X 32 sprites.
4. The syntax error is caused by the LOAD IMAGE commands in the function. If you remove them as I suggested, it should run. You have to list the filename and path in quotes in the LOAD IMAGE command.

The following code may be helpful to you:



This code creates images (colored boxes) and puts sprites 2-5 on the screen randomly and places sprite 1 over to the right side. You can move sprite 1 around and when it collides with one of the other sprites, it will not move into it.

If this is totally off base from what you wanted, let me know.

So many games to code.....so little time.
Don3784
7
Years of Service
User Offline
Joined: 15th Apr 2013
Location:
Posted: 20th Apr 2013 03:22
actually that helps alot and please forgive me if i sound like a newb but i will explain my reasoning for the code i am making a top down 2d game in the likes of zelda so my background is an image and not a sprite so no collision with that(how long this will work for me now that i am moving on to try to make a camera). sprites 2 thru 5 are actually invisible and are used as to detect which side of my sprite has a collision(before I did that he would stop as when I hit an object and never move again). and once again forgive me i still have much to learn
LBFN
13
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Apr 2013 22:19
Hi there,

I just provided some code to another person on the forum that would likely help you too. The thread is here:
http://forum.thegamecreators.com/?m=forum_view&t=204884&b=7

You could try that code (made by pizzaman / purple pickle) to see if it will work for your purposes. The code makes some graphics, but you could place Zelda style graphics in place of the ones generated in the code. Collision is excellent and you do not need to line the sides of one sprite with others just to check for collision.

There is absolutely nothing wrong with being new. I think people get flamed when they try to get others to do everything for them and are lazy. You just don't know how to do things yet, so keep at it. Hopefully the code will help you learn.

So many games to code.....so little time.
Don3784
7
Years of Service
User Offline
Joined: 15th Apr 2013
Location:
Posted: 23rd Apr 2013 02:56
thanks man that code is awesome and just how i want it to work it the perfect base for what i plan to do. it also seems much more versitle than what i was doing. hopefully i can learn enough of it to make it my own. i don't want to feel like i cheated to make my game. but thank you so much for this it is turning out to be a big help.
LBFN
13
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 23rd Apr 2013 06:48
I don't consider it 'cheating' to use code that has been freely offered on the forums. I do understand, however, wanting to make something on your own without reliance on anyone else. There is an entire board here that is made up of snippets of code. The idea is that you can evaluate what someone else has done and see if it would work for you in your situation. Typically, you dissect it to figure out how they accomplished what they did. You then modify it the way you want. Then when you get better and learn a few things, you post up a snippet or two to help others. It makes for a good community.

Anyway, good luck and happy coding.

So many games to code.....so little time.

Login to post a reply

Server time is: 2020-08-03 16:37:17
Your offset time is: 2020-08-03 16:37:17