0

I'm creating simple flash game, I just made character controls (move left, right and jump). When I add gravity my character falling down from the stage.

I have painted floor, imported to library, but I don't know how code should looks like.

My floor is called "Ground" I added AS Linkage "Ground".

For now my code looks like: (Note: my character is named "Hero")

public class Script extends MovieClip{              // start the script

private const gravity:int = 1;
private const max_speed:int = 8;

private const walkspeed:int = 4;
private const jumpspeed:int = 10;

private var forecast_x:int;
private var forecast_y:int;

private const start_x:int = 50;
private const start_y:int = 50;

private var left:Boolean;
private var up:Boolean;
private var right:Boolean;
private var space:Boolean;

private var level:Array = new Array();

private var Map_data:Data = new Data;               // create a version of the Data.as
private var Hero_col:collision_manager = new collision_manager;

private var Hero:hero = new hero;

    public function Script(){                       // the init (will only be runned once)
        //BuildMap();
        create_hero();
        addEventListener(Event.ENTER_FRAME, main);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
        stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

        Hero_col.Setup(25,level,Hero);
    }

    private function main(event:Event){
        update_hero();
    }

    private function key_down(event:KeyboardEvent){
        if(event.keyCode == 37){
            left = true;
        }
        if(event.keyCode == 38){
            up = true;
        }
        if(event.keyCode == 39){
            right = true;
        }
    }

    private function key_up(event:KeyboardEvent){
        if(event.keyCode == 37){
            left = false;
        }
        if(event.keyCode == 38){
            up = false;
        }
        if(event.keyCode == 39){
            right = false;
        }
    }

        private function create_hero(){
            addChild(Hero);
            Hero.x = start_x;
            Hero.y = start_y;
            Hero.x_speed = 0;
            Hero.y_speed = 0;
        }
        private function setDirection(param) {
    if (param == 0) {
        Hero.scaleX = 1;
    } else {
        Hero.scaleX = -1;
    }
}
        private function update_hero(){
            Hero.y_speed += gravity;
            if(left){
                Hero.x_speed = -walkspeed;
                setDirection(1);
            }
            if(right){
                Hero.x_speed = walkspeed;
                setDirection(0);
            }
            if(up && Hero_col.can_jump){
                Hero.y_speed = -jumpspeed;
            }

            if(Hero.y_speed > max_speed){
                Hero.y_speed = max_speed;
            }

            forecast_y = Hero.y + Hero.y_speed;
            forecast_x = Hero.x + Hero.x_speed;

            Hero_col.solve_all(forecast_x, forecast_y);


            Hero.x_speed = 0;
        }

Thank you for help.

  • Is the floor flat? if it is flat you can use `stageHeight` and `hero.y`. – null.point3r Sep 12 '13 at 08:10
  • Thank you for answer, for now I use `if (Hero.y == 150){ Hero.y_speed = 0; }` but now I can't Jump when It's on the floor –  Sep 12 '13 at 08:13

1 Answers1

1

You need to have a couple of Boolean variables to check if the hero is on the floor, and also when the gravity is applied. So if he reached the floor or a specific area, you must set the gravity to false, not updating it each frame like you're doing here : Hero.y_speed += gravity;

If you're pressing the space bar, you need to let the character to jump, then you apply the gravity, by setting the Boolean to true, after that, when it hits the floor, you should disable the gravity, and enable jumping again.

void update(float delta) {
    if (grounded) { 
        // ground based movement and actions

        // check to see if the spacebar was pressed to jump
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) {
            ya = -40;   // go up 40 pixels per second
            grounded = false;
        }
    } else {
        // air based movement and actions

        // apply gravity
        ya += GRAVITY * delta;    
    }

    // movement and collision work after you've figured out the x and y acceleration of the player
    y += ya * delta;
}
andre_lamothe
  • 2,171
  • 2
  • 41
  • 74
  • Thank you for reply, could you help me with that? How It should like correctly? –  Sep 12 '13 at 08:34
  • I've tried: `private var gravity:Boolean = true;` `if(Hero.y_speed>0 && Hero.y>(stage.stageHeight-Hero.height-10)){ gravity = false;}` but It not working –  Sep 12 '13 at 08:53
  • First the gravity should be false. When you jump it should be false, after specific period, you enable it. Modify my pseudo-code for your purpose. – andre_lamothe Sep 12 '13 at 08:58
  • Thank you for answer, but I got this error: `1084: Syntax error: expecting rightparen before delta.` at this line: `void update(float delta) {` –  Sep 12 '13 at 09:00
  • also I got this error: `1083: Syntax error: else is unexpected.` –  Sep 12 '13 at 09:03
  • you need to modify your code based on the idea of the code that I posted. Don't copy and paste it!. – andre_lamothe Sep 12 '13 at 09:14
  • Could you a little bit explain me your example? As i understand where `if (grounded)` I need to use `if(Hero.y_speed>0 && Hero.y>(stage.stageHeight-Hero.height-10))`, how correctly check If space clicked? As you wrote `(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)` I got error: `1084: Syntax error: expecting rightparen before doublecolon.` and what's about `ya` variable? Thank you for answers. –  Sep 12 '13 at 09:28
  • For now I use this: `if(Hero.y_speed>0 && Hero.y>(stage.stageHeight-Hero.height-10)){ Hero.y=(stage.stageHeight-Hero.height-10); if(space){ trace("Clicked SPACE"); gravity = true; Hero.y = 200; }` This is not so good solution, because It so quickly falling on the ground again, when my gravity is Boolean type, how can I reduce gravity for slowly falling? –  Sep 12 '13 at 10:02
  • And there is no any other way to make It? If I will add any walls, objects and It will be not on the ground, after I will click "Space" button It just will teleport my character to 200 height of the stage as I understand? –  Sep 12 '13 at 10:03
  • please read this http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/ – andre_lamothe Sep 12 '13 at 11:24