How do I make camera follow an object while jumping only vertical on platforms?

257 views Asked by At

I am creating flash android game with as3. I already made object jumping on platforms, but I am facing problems with following object vertically. Any help or suggestion how to improve code are more than welcome!

The object instance name is hero and all platforms are made as one movie clip whose instance name is level

Here is my code:

import flash.events.Event;

var Key:KeyObject = new KeyObject(stage);
var dy:Number=10;
var gravity:Number=1;    
var canjump:Boolean = false;

stage.addEventListener(Event.ENTER_FRAME,onenter);

function onenter(e:Event) :void {

    if (Key.isDown(Key.LEFT)) {
    hero.x-=30;
    hero.y-=10;
    }

    if (Key.isDown(Key.RIGHT)) {
    hero.x+=30;
    hero.y-=20;
    }


    dy+=gravity;

    if (! level.hitTestPoint(hero.x,hero.y,true)) {   //if the hero not hitting the platform
        hero.y+=dy;
    }
    if (dy>10) {   //controls gravity
        dy=10;
    }
    for (var i:int = 0; i<10; i++) {
        if (level.hitTestPoint (hero.x,hero.y,true)) {   
            hero.y--;
            dy=0;
            canjump=false;
        }
    }

}
1

There are 1 answers

1
Bold and Brash On BEST ANSWER

I'm not quite sure whether it would work on android but on my code for some other platformer, I used the script

this.y=-hero.y+(stage.stageWidth/2); 

this follows the character as he ascends, in the swf file at least; hope it helps!