AS3 platformer, making a TileMap scroll.

Started by
14 comments, last by Leon322 8 years, 10 months ago

Problem solved, thanks people!

!!!!

Advertisement

Hello'

Put all gameObject in a container, when the player moves to the right, you have to move the container to the left to keep the player on the center of your screen

hi, i dont know much about flash and containers, can you explain a little more?

Having briefly reviewed your code...

Make a TileMapLayerView class. This should subclass Sprite. You tell it what TileMap and asset library it reads from, and tell it what its viewport size is. It has a scrollX/Y value, and as you scroll around, it instantiates tile movieclips, adds them as child movies of itself, and moves them around. If your map is dynamic, you'll need a means of letting the TileMapLayerView class receive events regarding map changes.

Make a SpriteLayerView class. This should subclass Sprite. You add/remove sprites (abstract, game object sprites, not Flash Sprites) on it, tell it what its viewport is. It has a scroll X/Y value. As you scroll around, it adds and removes the game-object sprites matching animation MovieClips to and from the stage, and moves them around. It will need to listen to change events on those sprites (as they change animations, world-space position, flip, size, etc) to update their on-stage positions.

Now you can implement different kinds of maps and views, separately. You can create parallax scrolling by having multiple layer views of maps which "repeat" on the X or Y axes, and giving them different scroll multipliers. You can have multiple sprite layers, and have them interact with multiple map layers.

EDIT: I just found out I don't have time to write up any example code tonight. Sorry. But I'll hit this thread again after work tomorrow and add more if you are still having problems.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

Wyrframe, i'm sorry but i can't see it yet :S

That might be because my work PC's hard drive died over the weekend, and I just put in two ten-hour days just bringing my development environment back up to production-ready capability. :(

Nevertheless, here you go.

https://docs.google.com/uc?id=0B8yLWMNaFJ2KNnV0dmdUcG5Jdzg&export=download

Find within: a CS6 and CS5 FLA (identical, just different versions), and three AS files.

* main(-cs5).fla - the main movie, which includes all the graphic assets used. It has only one line of AS3 source in it, because that's how I roll.

* Main.as - the AS source for the main movie. Creates a MapData and a MapLayerView, then runs an idle animation (panning around the map)

* MapData - a simple container. Here, it is used to parse strings as map data, look up tile values in a table and add instantiate them into a map array, and then provide bounds-checked lookup of those tiles later on.

* MapLayerview - a movieclip which can be told its tile size, viewport dimensions, and scroll position. Using this information, it adds/removes tile assets to itself as child movies and repositions itself to keep a well-contained scroll view. Note that it has a corresponding asset in the FLA; remove that purple debug dot for production (yes, this leaves it completely blank).

In the main movie, see that I have positioned a 400x300px frame in the middle of the movie. I did this deliberately so you can see the tiles being added/removed around the periphery of the viewport. You will likely use the entire movie as your viewport in real usage. Notice that I have animated tile assets, how they are loaded from the FLA library, and how I provide them to the MapData system, without MapData ever needing to be aware of precisely what the data it stores (read: loose coupling is good for you).

Let me know if you have specific questions.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

Improvements you can still make:
* Change setDimensions(Number,Number) to setBounds(Rectangle), use the provided T/L instead of the MapLayerView's original x/y

* Finish revalidateAll(); have it only remove tiles no longer in the view, and only add tiles freshly added to the view. Remember that you might have no overlap at all if the view "snaps" from one side of a playfield to another.

* Extend the add functionality; right now, it detects DisplayObject map data and adds them to the map. Augment this to include checks for, for example, SpawnAware map "tiles", and trigger them (so that some creature/group/object is added to the world only when it pans into view).

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

Improvements you can still make:
* Change setDimensions(Number,Number) to setBounds(Rectangle), use the provided T/L instead of the MapLayerView's original x/y

* Finish revalidateAll(); have it only remove tiles no longer in the view, and only add tiles freshly added to the view. Remember that you might have no overlap at all if the view "snaps" from one side of a playfield to another.

* Extend the add functionality; right now, it detects DisplayObject map data and adds them to the map. Augment this to include checks for, for example, SpawnAware map "tiles", and trigger them (so that some creature/group/object is added to the world only when it pans into view).

hi!; this is what i have so far, as you can see, the scrolling still doesnt work, i mean, it "works" but the main problem is that the coordinates are wrong

This problem is for hardcores D:

Well, with a completely featureless map (just a perimiter, with nothing inside and no tile variety), and me not using FlashDevelop (so I can't edit the map and re-compile), I'm not going to trawl 64kB of source by hand looking for possible problems. Give me a hand, here.

a) How do you attach and position objects on the stage,

b) How do you attach and position tiles on the stage,

c) and what isn't working the way you intend it to?

We're here because we like to help. That doesn't mean you can hurl 10 MB of source and assets at us and ask us to work our magic. :)

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

'k, found a few things.

CPlayer's constructor looks for the CGame singleton, queries a getContainer():DisplayObjectContainer method on it, and adds CPlayer::mMC to that container. Every frame, you then copy the player's world-space position to the mMC movieclip which represents the player in the world. This is not only horrible design, but it is why your player doesn't stay on-camera. You're moving the sprite around on the screen directly, and not letting the sprite's position be controlled by something which is aware of the camera. For this to work the way you are currently structuring your code, you have to have your CPlayer be aware of your CCamera, and subtract the camera offset from CPlayer::mMC position, just like you do for the tilemap already. And if that isn't setting off alarm bells in your head, it should be.

Look to my design again. The tile map has no concept of a camera. It has no concept of screen space, stage, or even game. It doesn't even know it happens to be storing movie clips in my particular, specific example. The tile-map-viewer has a concept of a scroll position, and of having a viewport, but it gets told these things by an outside source. It doesn't know what a player is, it doesn't know what a sprite is, and it doesn't care. The only object which knows about all these things is the "Main", which in my design should be equivalent to your CLevelState (I'm reading that as Level-state, as opposed to Menu-state or GameOverScreen-state, right? it's an application root state machine).

Refactor your code to reduce coupling (i.e. nothing, NOTHING, should ever refer to CGame statically, except to invoke its constructor). Create a CLevelView which knows about lists of objects (games, enemies, bullets) and maps and backgrounds, and either it or its components takes responsibility for composing the game-world objects by interpreting their world-space positions and setting the corresponding DisplayObject/MovieClip's positions on stage.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

This topic is closed to new replies.

Advertisement