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 / 2D RPG style movement

Author
Message
xxelixx
15
Years of Service
User Offline
Joined: 6th Dec 2008
Location: CodeLand
Posted: 20th Nov 2009 05:19
I was wondering if someone knew how to do 2D style movement. I haven't found any tutorials for it. Any help appreciated.
xguild
16
Years of Service
User Offline
Joined: 19th Oct 2008
Location:
Posted: 20th Nov 2009 11:09 Edited at: 20th Nov 2009 11:13
Here on the newbie forums, I sort of think newbies should help newbies as a way to learn, so while Im by no stretch of the imagination an expert (quite to the contrary), I have found that by helping other people I learn a lot on my own. So here goes

By RPG movement Im assuming your talking about something along the lines of Zelda (like a top down view in a 2d enviroment?).

I started working on something similiar and while I don't think this is THE way to do it, i think its a good start.



Ok so now we have set up the coade with an image being loaded into memory. I think (not 100% sure here) that we need to create some variables for keeping track of the angle.



This is the next line. I saw this used in a couple of examples. The Angle#=0 basicaly sets the starting position for what will be our sprite in the main loop. So Angle 0 should mean pointing North effectivly or up if you will.

Then we set X and Y at 100.0, This will set the starting position for the sprite that again we will load in the main loop.

Next we need to load the spite into position. The rest of the code should be within our loop so we have to add a DO at the start here.



So the command is Sprite, the number 1 is a reference to the sprite, so this is sprite 1 we are loading. Then we set the position (X and Y) using our variables instead of a fixed number since will be changing these when we start moving. finally the number 1 after the position is the reference to the image which is image 1.

The next thing is the rotation of the sprite. We want to tell the program that it should rotate the sprite on a 360 degree angle using our angle variable as a starting point.



The command is rotate sprite and a reference to the sprite number. Then the angle which we are going to use our variable for (angle#).

Now I think we need to set up the movement with the left and right keys so that the sprite can rotate.



The trick here is the "wrapvalue" command. We tell the program if you press the Leftkey then change the value of angle# to angle#+ new angle. So in this example I use 4.0 which just means it will add 4.0 to the 360 angle per click of the key. The higher this number the faster it will rotate. The same for the right key. Since there are two ifs we have to have 2 endifs. (Im not sure why that is since its a single if command with an Else if in it but thats how it works (tested it)

finally we add the sync command and close the loop so



The complete code should look like this.



I don't think we are done here.. the effect is a bit wierd as the sprite doesn't rotate around a center point. I will try to find out why it does that.

Newbie Alert
xguild
16
Years of Service
User Offline
Joined: 19th Oct 2008
Location:
Posted: 20th Nov 2009 11:45
I believe the trick to getting the sprite to rotate on its center is to identify the sprites dimensions to the program.

There are additional function to loading a sprite called sprite width and sprite height which define the sprites dimensions. I think the trick is to use these in the code but Im not 100% sure yet exactly where those functions should be. But once we have defined the diameter of the sprite I believe you can identify its center and it will rotate on that point.

Newbie Alert
xguild
16
Years of Service
User Offline
Joined: 19th Oct 2008
Location:
Posted: 20th Nov 2009 11:52
Ok I found it.

What you have to do is use a function called Offset Sprite.

In the code example if you look it up in help it gives a perfect example.



I tried it and it works, you place this line right after you load the sprite.

I think the offset sprite 1 identifies which sprite we are trying to find the center of . Then the sprite width (1) identifies sprite 1 and the /2 is the split. So you could use a number here depending on where you want the split to happen, I believe if you do a 2 its down the middle so if you do it for both height and width you should have a center.

I think!... it would be great if someone could explain that line of code a little better for me.

Newbie Alert
xguild
16
Years of Service
User Offline
Joined: 19th Oct 2008
Location:
Posted: 20th Nov 2009 12:06
In any case moving right along (this is fun!).

Ok so getting the sprite to move forward I thought was going to be easy but its a bit more complicated then I thought.

We have to make the sprite move in the direct thats its currently facing and I didn't see any simple commands for "move forward".

I tried

if upkey() move sprite 1, (5)

but that moves the sprite the fixed number 5 pixels forward and when you release puts it back. Not sure how to get it going forward.

Newbie Alert
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 21st Nov 2009 02:47
xguild

Please don't double/triple post. It's one of the admittedly minor rules on here along the lines of 'netiquette'.

To add something you forgot to your last post (if no-one else has posted in the meantime), just use the 'Edit Own' button over on the left.

xxelixx:

If by '2D style movement' you mean 2D scrolling, we've just done a DBC Programming Challenge on 2D scrolling which you may like to take a look at. Although it's for DBC, it should work in DBP with little or no alterations.

Check out the DarkBASIC Discussion board.

TDK

Login to post a reply

Server time is: 2024-11-23 22:23:27
Your offset time is: 2024-11-23 22:23:27