PDA

View Full Version : [AS2] Platform Scrolling Blues


Murphy1976
05-06-2009, 02:53 AM
Good Evening Flash Gurus,

I need your help once again. I found some source code that is a PERFECT base for a platform game I am attempting to make. It has all the necessary elements that I'm looking for plus it is easily modified to make it absolutely perfect. The problem I'm having is not with the platform engine at all, but the custom scroll feature I am trying to build along with it.

Here's what I'm trying to do:

I am specifying a the center point of the engine at wherever the character is, BUT (and this is where I'm getting tripped up) when the map reaches is absolute minimum and absolute maximum, the level stops scrolling, until you go back in the other direction, either on the X axis or Y axis.

I saw a version of this at the following address:

http://www.tonypa.pri.ee/tbw/tut14.html

But the code is so verbose, I couldn't even begin to fathom it, so I decided to make my own, which is always better in the long run, because I learn more that way.

I have specified all the necessary variables (at least I think I do), as you will be able to see in frame 1 on the FLA file I have attached. The movie works fine as long I have comment out the portion of code that I have commented out on frame one, but as soon as you activate that code, the level stays put at 0,0.

I know I'm probably missing something extremely simple, and I'm at my wits end, and fighting a cold, so my fuse is even shorter.

I'm wonder if one of you Flash Masters could look this FLA over and see what I'm missing.

Please do not modify the attached FLA file, if you must add code, please mark it well and comment it out so I can read it and apply it where I would like to apply it.

sunlis
05-06-2009, 05:14 AM
There a few problems that I found in the map movement script:

You were testing to see if the map is "out of bounds", but you weren't correcting it.

If you don't want a ball to go out of the left side of the stage:
if(ball._x < 0){
ball._x = 0;
ball.xVelocity *= -1;
}
You must not only stop it from going any further (line 3), but you must also get it out of the bad area (line 2).




Also, you were referencing the current position of the map, but you should have been referencing where the map WILL be. If THAT value is invalid, then you set your xVal or yVal to 0.

Here's your commented out section, with my corrections.
//mapX + diffX gives us the map's position AFTER the potential move
if (mapX + diffX >= mapXmin) {
//put the map back in the "safe zone"
level_container._x = mapXmin;
xVal = false;
} else if (mapX + diffX <= mapXmax) {
level_container._x = mapXmax;
xVal = false;
} else {
xVal = true;
}

if (mapY + diffY >= mapYmin) {
level_container._y = mapYmin;
yVal = false;
} else if (mapY + diffY <= mapYmax) {
level_container._y = mapYmax;
yVal = false;
} else {
yVal = true;
}

----------------------------

Also, when you're testing for xVal and yVal, your script can be cut down to one line:
if (xVal == true) {
if (diffX >= 0) {
level_container._x += Math.abs (diffX);
} else {
level_container._x -= Math.abs (diffX);
}
}
//can be replaced with:
if (xVal == true) {
level_container._x += diffX;
}
This is because you are either adding a positive value, or subtracting a positive value. If diffX is negative, and you add it to level_container._x, it's the same as subtracting Math.abs(diffX).
This isn't a big deal, but it cuts out a few lines.

4 + |2| = 4 + (2)

2 - |-3| = 2 + (-3)


Great script though, looks promising!

Cheers. ;)

Murphy1976
05-06-2009, 11:39 PM
YOU SIR..... ARE A GOD....
I knew it was something simple that I either got forgot or got mixed up.... and if I wasn't fighting this cold... I probably would have figured it out.

Thank you!

sunlis
05-12-2009, 03:44 AM
That's the kind of stuff I've picked up here. Just look around sometimes, you'd be surprised how much you can learn. ;)

And you're welcome. lol