1: First, open up flash. size doesn’t matter.
2: make a character. size doesn’t matter. make it a movie clip and give it the instance name “char” without quotes.
3: now make a rectangle with the rectangle tool. i usually turn off the border color. i’m using this size: W: 88.0, H: 25.00. but, size doesn’t matter here either. make it a movie clip and give it the instance name “pm1”, again, without quotes.
4: make another rectangle, this is going to be a regular platform. convert a movie clip and give it an instance name “p1”.
5: now then, add this code to the frame:
stop();
speed = 0;
maxspeed = 8;
jumpspeed = 0;
jumpheight = 15;
fall = 0;
maxgravity = 15;
platforms = 2
movplats = 1
platdistance = 0
maxdistance = 330
pxspeed = 2
restspeed = 0
_root.onEnterFrame = function() {
charx = _root.char._x;
chary = _root.char._y;
charh = _root.char._height;
charw = _root.char._width;
_root.char._x += speed;
_root.char._y += fall;
if(charx > Stage.width/2) {
_root._x = -charx+stagecentrex;
} else {
_root._x = 0;
}
if(chary < Stage.height/2) {
_root._y = -chary+stagecentrey;
} else {
_root._y = 0;
}
fall++
if(fall >= maxgravity) {
fall = maxgravity;
}
if(Key.isDown(Key.LEFT)) {
speed–;
if(speed <= maxspeed*-1) {
speed = maxspeed*-1;
}
if(_root.char._xscale > 0) {
_root.char._xscale *= -1;
}
} else if(Key.isDown(Key.RIGHT)) {
speed++;
if(speed >= maxspeed) {
speed = maxspeed;
}
if(_root.char._xscale < 0) {
_root.char._xscale *= -1;
}
} else {
if(speed > restspeed) {
speed–;
if(speed <= restspeed) {
speed = restspeed;
}
} else if(speed < restspeed) {
speed++;
if(speed >= restspeed) {
speed = restspeed;
}
}
}
if(Key.isDown(Key.SPACE) && jump == false && fall <= 1) {
jump = true;
restspeed = 0
}
if(jump == true) {
if(jumpspeed > 0) {
jumpspeed–;
fall = 0;
_root.char._y -= jumpspeed;
} else {
jump = false;
}
} else {
jumpspeed = jumpheight;
}
for(i3=1; i3<=movplats; i3++) {
mplat = _root[“pm”+i3];
_root.mplat._x += pxspeed
platdistance += pxspeed
if(platdistance >= maxdistance){
pxspeed = -pxspeed
}
if(platdistance <= 0){
pxspeed = -pxspeed
}
if(_root.char.hitTest(mplat)) {
if(chary+charh/2-1 < mplat._y-mplat._height/2) {
_root.char._y = mplat._y-mplat._height/2-charh/2-.1;
jumpspeed = jumpheight;
fall = 0;
jump = false;
restspeed = pxspeed
}
}
}
for(i=1; i<=platforms; i++) {
plat = _root["p"+i];
if(_root.char.hitTest(plat)) {
restspeed = 0
if(charx+charw/2 < plat._x-plat._width/2) {
_root.char._x = plat._x-plat._width/2-charw/2-.1;
speed = 0;
} else if(charx-charw/2 > plat._x+plat._width/2) {
_root.char._x = plat._x+plat._width/2+charw/2+.1;
speed = 0;
}
if(chary+charh/2-1 < plat._y-plat._height/2) {
_root.char._y = plat._y-plat._height/2-charh/2-.1;
jumpspeed = jumpheight;
fall = 0;
jump = false;
} else if(chary-charh/2+1 > plat._y+plat._height/2) {
_root.char._y = plat._y+plat._height/2+charh/2+.1
jumpspeed *= -1;
}
}
}
}
Code Explained:
speed = 0;
maxspeed = 8;
jumpspeed = 0;
jumpheight = 15;
fall = 0;
maxgravity = 15;
: this is for the char.
platforms = 2 : this how many regular platforms you have.
umm, the more platforms you have, the instance name number goes up, like so: p1, p2, p3 etc.
this goes for the moving platforms, too.
movplats = 1 : this is how many moving platforms you have.
platdistance = 0 : this is where the moving platform starts.
maxdistance = 330 : this where the moving platform stops and starts coming back.
pxspeed = 2 : this is the speed of the moving platform.
for(i3=1; i3<=movplats; i3++) {
mplat = _root["pm"+i3];
_root.mplat._x += pxspeed
platdistance += pxspeed
if(platdistance >= maxdistance){
pxspeed = -pxspeed
}
if(platdistance <= 0){
pxspeed = -pxspeed
}
if(_root.char.hitTest(mplat)) {
if(chary+charh/2-1 < mplat._y-mplat._height/2) {
_root.char._y = mplat._y-mplat._height/2-charh/2-.1;
jumpspeed = jumpheight;
fall = 0;
jump = false;
restspeed = pxspeed : this is for the moving platform... i don't know that much about it. sorry.
i hope you can figure out the rest.
basically, i set up my scene like this.
P: means where the player is positioned.
PL: platform
MPL: where the moving platform is.
P
PL_MPL________PL
somethin like that…
once again, my first tut…
my friend taught me this on msn.