Publishing System Settings Logout Login Register
Basic Platform Engine
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on December 4th, 2006
11109 views
Adobe Flash
Basic Platform Engine
Ben Fleming

Well I've seen a lot of platform engines, but each time I try their code, theres always some major glitch. Im not saying that mine is glitch free, but mine are very unoticable.

First, draw a square. Convert it to a movieclip (make sure the registration is set to the middle), and call its instance 'char'. This will be your character.
Add a new layer underneath the character, and draw another square, but change its colour or something. Convert it to a movieclip (make sure the registration is set to the middle), and call its instance 'p1'. You can drag and stretch this to make a proper platform. If you want more than one platform, copy and paste another platform onto the same layer, but change its instance to 'p2'. Each platform you make add 1 to the number. [NOTE] Sometimes it works better if you overlap certain platforms.

Ok now we're set for the code. Add a new layer ontop of everything, and click on the first frame. Open up the actionscript panel, and add this code:
[code=Java]stop();
speed = 0;
maxspeed = 8;
jumpspeed = 0;
jumpheight = 10;
fall = 0;
maxgravity = 15;
platforms = //how many platforms you have;
stagecentrex = Stage.width/2;
stagecentrey = Stage.height/2;
var jump:Boolean = true;
_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;
    fall++
    if(fall >= maxgravity) {
        fall = maxgravity;
    }
    if(Key.isDown(Key.LEFT)) {
        speed--;
        if(speed <= maxspeed*-1) {
            speed = maxspeed*-1;
        }
    } else if(Key.isDown(Key.RIGHT)) {
        speed++;
        if(speed >= maxspeed) {
            speed = maxspeed;
        }
    } else {
        if(speed > 0) {
            speed--;
            if(speed <= 0) {
                speed = 0;
            }
        } else if(speed < 0) {
            speed++;
            if(speed >= 0) {
                speed = 0;
            }
        }
    }
    if(Key.isDown(Key.SPACE) && jump == false && fall <= 1) {
        jump = true;
    }
    if(jump == true) {
        if(jumpspeed > 0) {
            jumpspeed--;
            fall = 0;
            _root.char._y -= jumpspeed;
        } else {
            jump = false;
        }
    } else {
        jumpspeed = jumpheight;
    }
    for(i=1; i<=platforms; i++) {
        plat = _root["p"+i];
        if(_root.char.hitTest(plat)) {
            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]
Make sure to change the platform variable to how many platforms you have. Now try it out!

Addon:
Springs

Alright first draw your spring. Convert it to a movieclip, and add a new keyframe (inside the movieclip). Pull the spring down a bit, and tween it moving up. It'll look cool if you use the ease option, though its not compulsory. Now you have the animation of the spring, and will look animate that when you jump on it. Click on the first frame of the spring movieclip, and then add this code:
[code=Java]stop();[/code]
Go back to the _root stage, and click on the spring. Name it 's1'. If you want more than one spring, just copy and paste it and change it to 's2'. Like you did with the platform.

Find the code:
[code=Java]_root.onEnterFrame = function() {[/code]
And add this line of code before it:
[code=Java]springs = HOWMANYSPRINGS[/code]
Obviously change how many springs you have on the stage.

Find the code:
[code=Java]for(i=1; i<=platforms; i++) {[/code]
And add this code before it:
[code=Java]    for(j=1; j<=springs; j++) {
    spring = _root["s"+j];
    if(_root.char.hitTest(_root.spring)) {
        jump = true;
        jumpspeed = jumpheight*2;
        _root.spring.gotoAndPlay(2);
    }
}[/code]

There! Now try it out :)
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Ben

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top