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 / enemy sprite moves with player as map scrolls

Author
Message
sskethan
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Oshawa, ON. Canada
Posted: 11th Dec 2011 21:01
The enemy sprite moves along the screen with my player as the map scrolls. How do i get the enemy to stay at the assigned coordinates as the map scrolls? I have tried setting sprite priority but that does not change anything. Here is the code i have so far. Any comments on the code structure and layout would be great aswell. I have had DBP for a long time but never really got into it until now.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th Dec 2011 10:51 Edited at: 12th Dec 2011 10:52
Quote: "How do i get the enemy to stay at the assigned coordinates as the map scrolls?"

The same way as everything else.

With scrolling games (I'm actually building one right now in AGK) I like to emply 'virtual' x and y values as well as their real ones. The virtual x and y values for each object would keep track of their position relative to the rest of the world while the real x and y values are the places they are drawn on the screen. The virtual x and y values will be effective off screen (when the respective sprites aren't being drawn) and the real x and y's when they are being drawn onto the screen. Just skimming through you're code now, it doesn't look like your using UDTs (user defined types). I recommend using them because they may make things far easier for you especially in this kind of game.

EricB
13
Years of Service
User Offline
Joined: 3rd Nov 2011
Location:
Posted: 12th Dec 2011 13:04
I also see a bug in the code above.
You set map height at 40
yet the data segment is only 30 high. I would assume this will result in an error regarding which image is pasted within your drawmap function.
sskethan
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Oshawa, ON. Canada
Posted: 12th Dec 2011 15:07
Hodgey
Thank you for the insight. I will look into UDTs.

EricB
I don't understand. I have 40 data lines down and 40 across.
EricB
13
Years of Service
User Offline
Joined: 3rd Nov 2011
Location:
Posted: 12th Dec 2011 16:08
Quote: "EricB
I don't understand. I have 40 data lines down and 40 across. "


I can't count it seems. I counted three times and came up with 39.
sskethan
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Oshawa, ON. Canada
Posted: 18th Dec 2011 03:13
Sorry, but im still having trouble understanding how to get the eneny to stay in its own postion while the screen scrolls. I setup the enemies own position but it still scrolls with the map.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 22nd Dec 2011 15:05
Here's how I make 2D tile-based map scrolling:

Set up a virtual 'camera', which isn't really a camera at all, but just a set of coordinates for where the player is currently looking on the map. I call these variables CamX and CamY.

Offset the draw positions of *everything* by these values. As you scroll the camera to the right, increase CamX. As you scroll left, decrease it. Scrolling down increases CamY and up decreases it. Whenever you draw a map tile, the player, or an enemy, subtract CamX and CamY from the coordinates you draw to, like this:

PASTE IMAGE 1, PlayerX - CamX, PlayerY - CamyY

That way, as your camera scrolls to the right, you simply draw everything further to the left.

You can make your camera follow your player by simply throwing in something like this somewhere in your mainloop:

CamX = PlayerX - SCREEN WIDTH()/2
CamY = PlayerY - SCREEN HEIGHT()/2

That'll keep the camera centred on your character.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 25th Dec 2011 18:29
Couldn't you just do an if statement? Like, if enemyIdle = 1. Then if it's true, don't offset by the scrolling. And it'll just stay wherever it should. I didn't really read the code, but that should work.

-------------------------------------------------------------
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 31st Dec 2011 09:49 Edited at: 31st Dec 2011 09:55
Regarding DATA, just a tip here, but if you end up with large quantities (like 40 rows x 40 columns), might be better to place it in (an) external file(s). This is also more managable because you can have two windows open at once: Your code editor (DBPro Editor/etc) and your map editor (Notepad?). That way you don't have to scroll down to your map data every time you want to change something.
"So whats to stop some idiot messing with my maps?" - encryption using an in-program encryption algorithm. If you don't know how to write one, I'm sure there are tons of open-source dll's available offering fast, compact encryption that will be more secure than a "quick job".

As for scrolling, I think everyone's basically resolved that one. Still, if you want simplicity over manageability, there's the old-fashioned way:

While this isn't the best method (and it only works if your code is compatible), it is a quick and easy way of doing things. Consider these thing "cheat codes for the Game of Life".

EDIT: Best just inform you that the above method uses a lot of off-screen drawing and it also requires boundaries to be imposed (such as not scrolling if the player is in front of a wall, etc). It is just a skeleton idea. I used it for BG entities in an experimental game of mine and it worked with no slow-downs but it depends largely on just how big your levels are and how many entities you have. Just to let you know.

72 97 112 112 121 32 72 111 108 105 100 97 121 115 32 112 112 108 33

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 31st Dec 2011 18:10
If I was gonna do scrolling, I would probably use agent's idea, I never thought of doing that before.

-------------------------------------------------------------
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 1st Jan 2012 07:57
Just a thought that plagues me around here sometimes: Why do people who have no clue what they're talking about pretend that they do by posting code that doesn't do anything close to what they've been asked to assist with, and then offer 'tips' about matters completely unrelated to the question at hand?

It doesn't help the original poster to do things like this. It doesn't make you look intelligent or knowledgeable. It makes you look foolish, and only serves to confuse the person who asked the question in the first place.

sskethan doesn't want encryption, and he doesn't want open source dll's to scramble his data. He doesn't want to merely move his sprite (which is all your code would do, if it even compiled - which it doesn't). He wants to scroll an entire map.

He's already been shown how to do this. The question has been answered. If you have an alternative way of doing what he's asked, that is different from my method, by all means post away. But your solution doesn't do what he asked it to do, and it doesn't even compile. You haven't used your ENDIF's correctly, you're using clearly mismatched variables in your FOR...NEXT loops and you're throwing around 'tips' that are obviously well above his level of understanding (and quite clearly, your own).

A general notice to all users of this forum: Please don't do this, guys. It just plain sucks. We are supposed to be a community of helpful peers. Post only if you have something that will help, and whatever you do, if you don't understand the question or the answer, keep it to yourself. Posts like this just confuse the poor guy, and if he's posting a question here then he's already confused enough.

</rant>... I just hate this kind of thing...

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Jan 2012 21:42
@Agent:
Quote: "... unrelated to the question at hand ..."


I merely offered sskethan advice for managing data better because it came up earlier in the thread when EricB mentioned a problem regarding sskethan's DATA. Just thought it may make OP's life easier when handling/modifying DATA to have it in a different file. Yes I went off-topic with encryption, but logically if you provide someone with a method of doing something, you may as well make a brief mention of any major cons and suggest a workaround. Besides, I like when somebody goes off-topic in a thread I start so long as a gain knowledge from it. Last time I checked, the acquisition of knowledge is fundamental to our growth as people, as beings.

Quote: " ...You haven't used your ENDIF's correctly, you're using clearly mismatched variables in your FOR...NEXT loops... "


What mismatched FOR...NEXT variables? The idea of scrolling involves moving both the enemies and background. That would mean (assuming you numbered your sprites correctly) from say the first background sprite TO the last enemy sprite... OR maybe I'm just going crazy though

As for my ENDIF's being used incorrectly, what's wrong with truncating small code to 1 line? It looks tidy if it's something short and simple and it uses 1 line as apposed to 3.

Quote: " ... But your solution doesn't do what he asked it to do, and it doesn't even compile... "


Doesn't compile? Compiles fine on my version of DBPro. Now I won't flame you for making an untrue statement because it is more than within the bounds of possibility that the reason it doesn't compile is we have different versions of DBPro. So I won't dive in head first all aggressively.

As for it not doing what he asked for, I'll grant you it's possible I'm misunderstanding the question. I thought he wanted a scrolling system for, say, a 2d platformer. If this is not what OP requested and I misunderstood, well sorry and I'll give myself a good lashing later (or 10000 lines to write out: "I must read and interpret the question correctly before replying"). Maybe I'll let you choose

Anyhow, here's something that really plagues me:

Why do people who have not adequately read a post (ie "skim" it) and make an assumption that they know what the other person is talking about? Why do people feel a need to be nasty and aggressive towards another whose views they disagree with?

Is an attempt at belittling another some form of enhancing one's own perception of one's self? Surely a truly mature and evolved human who stumbles upon a fool would not belittle that fool but instead try to enlighten him.

I shall leave you with this, that perhaps you may simmer rather than boil: I have come across many "fools" on forums and have either ignored their posts or tried to enlighten them slowly, bit by bit (pardon the pun) instead of showing them aggression. But I am not perfect though. I have "reacted" to ppl and shown hostility in an unwarranted situation too. In fact my very first thread got me in trouble because I posted a link to a game I'd made which was "unfit" for this forum. I got into a debate about it initially but after calming down, I realised I'd been wrong to quickly take offense and I appologised. So, whether you appologise or not, I forgive you all the same for your aggressive rant at me.

If you feel you wish to continue this argument, perhaps we should do it outside of the TGC forums. If not, I would prefer that we retain a polite relationship with one another as forum members.

Guys, a note about flaming, in Agent's own words:

Quote: "A general notice to all users of this forum: Please don't do this, guys. It just plain sucks. We are supposed to be a community of helpful peers. Post only if you have something that will help..."

...and not something just to flame or rant.

Let's all be friends, kay

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 2nd Jan 2012 06:49
Quote: "I have "reacted" to ppl and shown hostility in an unwarranted situation too."


Perhaps I was in a bad mood yesterday. I apologise. I also rarely snap at people for the same reasons you've outlined.

Today I was going through some threads on this forum and came across some of your other posts. I found myself simmering as soon as I saw your name, thinking to myself, "I wonder if this idiot is going to start talking irrelevant tripe filled with bad advice and incorrect code again" and then, upon reading the post, found myself agreeing with it. Kind of kicked myself in the head for that, cause I realised I'd been a dope.

It is a pet hate of mine that people who don't know what they're doing try to make themselves look more knowledgeable by trying to help where they really have no business, which is why I reacted to what looked to be just that. I really really hate it when people do that, but I can see now that isn't the case.

Sorry mate. I was out of line. I wasn't even right - there's nothing wrong with your IF...ENDIF structure at all.

I think I can see where you were going, counting from the first background sprite to the last enemy sprite, but it still seems like a weird way of doing business I separate my image numbers into categories, see: 1-999 = background sprites, 1000-2000 = NPC sprites, etc. But there may not be 1000 of each; I just leave room for that in case of future expansion. That means there's often big empty gaps between image numbers, so you can't just iterate from 0-65000 to get all the images cause most of them aren't there.

Of course you were simplifying, but I needed an immature reason to save face So there!

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 2nd Jan 2012 08:47
Quote: "I apologise. "

Cool, water under the bridge
I have a feeling that things would've turned out differently if I'd posted the scrolling code (in verbose form) first and then the hint about data. Meh, nobody's perfect; if they were we this board wouldn't exist.

sskethan
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Oshawa, ON. Canada
Posted: 3rd Jan 2012 16:32
Thank you everyone for your help. It appears the the scroll features I am being offered is saying that the user would be pressing left, right, up and down and the map will scroll in the opposite direction the character travels. The problem i am having is that I have it setup to press only 1 key to move the character forward (W) and the left and right keys rotate (A)(D) as the character reaches the assigned max scroll the map scrolls.

Agent
Would your method work if i left the scrolling i currently use now and use your method for the enemies.

I'm at work right now and can not test right now.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 29th Jan 2012 06:54
Been a while since I've checked this thread.

I'm not entirely sure what you're asking here. Do you have a game in which the enemies are stuck in the same spot on the screen no matter where the camera is looking? If that's the case, just don't offset their draw positions by the camera position. That will allow you to scroll the world *behind* the enemies, without moving the enemies on the screen. This seems strange to me, though... It'd be like wearing a pair of shades in real life, that have a stain on them. No matter where you look, that stain stays in the same spot in your field of vision. It'd be similar to a HUD in a first person shooter game that shows your health, armor and ammo. Is that the effect you're trying to achieve?

Regardless, it doesn't make any difference the method you use to determine when the map should scroll in any particular direction. As long as you offset the location to which you are drawing everything (tiles, player, enemies, bullets, etc) relative to the position of the camera, you can adjust the values of CamX and CamY as you see fit and the camera will pan to wherever you set those two variables. If there are any objects that you want to treat like stains on a pair of shades, don't offset their draw positions when you render them to the screen and they'll stay floating in front of the camera, no matter where you look.

If you use a player whose movement depends upon rotation and forward/backward movement, as opposed to absolute directional control (Up/Down/Left/Right), you can just set the camera position to equal the player's position, less half the screen dimensions.

In other words, even if your character moves at irregular angles, you can use something like this:

CamX = PlayerX - SCREEN WIDTH()/2
CamY = PlayerY - SCREEN HEIGHT()/2

Inserting that into your main loop will keep the camera centred on the topleft corner of your player sprite at all times, no matter where it goes or at what strange angles it may move in.

You might then increase the camera variables by half the player sprite dimensions, so that the camera is fixed to the centre of the player, instead of its topleft corner:

CamX = PlayerX - SCREEN WIDTH()/2 + IMAGE WIDTH(1)
CamY = PlayerY - SCREEN HEIGHT()/2 + IMAGE HEIGHT(1)

In case it isn't obvious, you'll need to switch the parameter accepted by the IMAGE WIDTH and IMAGE HEIGHT commands from 1 to the image number to which you've loaded your player sprite.

Login to post a reply

Server time is: 2024-11-22 06:39:46
Your offset time is: 2024-11-22 06:39:46