Jump and Gravity

Hey there,

I’m making a small platform game for a module in University and I’m banging my head against the wall all the time. I’m quite new to flash as3 which means the smallest of errors can take me hours of time to figure out. I’m just in a rut at the moment since im suffering from two problems. The first being when the game begins my character can walk left, right and jump. When he jumps though the character then stops detecting the floor and just goes straight throught it. The second being I want my character to be able to walk on the ceiling, in other words changing the characters gravity so he floats upwards and recognises the cieling collision.

I’ve tried for hours on end yet no prevail. Any help but be wonderous and I forever in your debt!

Here’s the code I think is relevant to my problems:
PS. Im really sorry if this looks like I’ve copied a lot of code over, i just dont have any idea what parts i need to change. Like I said any help would be wonderful, thank you so much!

EDIT: just so you know grav is the character name :slight_smile:


public class gravGame extends MovieClip {

        // screen constants
        static const edgeDistance:Number = 100;
        static const edgeHeight:Number = 100;

        // object arrays
        private var gx:Number = 0; // pixel movement right
        private var gy:Number = 0; // pixel movement down
        private var gravity:Number = 1; // drop speed increase per frame
        private var fixedObjects:Array;
        private var otherobjects:Array;
        private var playerSpeed:Number = 7;
        private var xgravity:Number = 1;
        private var yVelocity:Number = 0;
        private var xVelocity:Number = 0;
        private var canJump: Boolean = false;
        private var jumpPow:Number = 0;
        private var jumping:Boolean = false;
        
        // grav and enemies
        private var grav:Object;

public function createGrav() {
            grav = new Object();
            grav.mc = gamelevel.grav;
            grav.dx = 0.0;
            grav.dy = 0.0;
            grav.inAir = false;
            grav.direction = 1;
            grav.jump = false;
            grav.walkAnimation = new Array("walk");
            grav.animstep = 0;
            grav.moveLeft = false;
            grav.moveRight = false;
            grav.gravup = false;
            grav.gravup = false;
            grav.jumpSpeed = 6;
            grav.walkSpeed = .25;
            grav.width = 80.0;
            grav.height = 100.0;
            grav.startx = grav.mc.x;
            grav.starty = grav.mc.y;
        }

// look at all level children and note wallys, floors and items
        public function examineLevel() {
            fixedObjects = new Array();
            otherobjects = new Array();
            for(var i:int=0;i<this.gamelevel.numChildren;i++) {
                var mc = this.gamelevel.getChildAt(i);
                
                // add floors and wallys to fixedObjects
                if ((mc is Floor) || (mc is wally)) {
                    var floorObject:Object = new Object();
                    floorObject.mc = mc;
                    floorObject.leftside = mc.x;
                    floorObject.rightside = mc.x+mc.width;
                    floorObject.topside = mc.y;
                    floorObject.bottomside = mc.y+mc.height;
                    fixedObjects.push(floorObject);
                    
                } 
            }
        }

public function keyDownFunction(event:KeyboardEvent) {
        if (gameMode != "play") return; // don't move until in play mode
            
        **[SIZE="4"]*other keycode events are here, i just left them out*[/SIZE]**
            } else if (event.keyCode == 32) {
                if (jumping != true) {
                    jumpPow =-35;}
                    jumping = true;
            }
        }

}
           **[SIZE="3"][SIZE="4"]*i dont know which ones to activate and if they are correct*[/SIZE][/SIZE]**
            if (jumping) {
                // start jump
                grav.mc.y += jumpPow;
                jumpPow += gravity;
                //grav.jump = false;
                //grav.dy = -grav.jumpSpeed;
                //verticalChange = -grav.jumpSpeed;
                //newAnimState = "jump";
            }
                
                if (grav.mc.y >= stage.stageHeight*2) {
                    jumping = false;
                    grav.mc.y = stage.stageHeight*2;
                }
            if (grav.gravup) {
                grav.mc.rotation += 180;
                gravity = -1;         
                grav.inAir = true;

// assume no wally hit, and hanging in air
            grav.hitwallyRight = false;
            grav.hitwallyLeft = false;
            grav.inAir = true;
                    
            // find new vertical position
            var newY:Number = grav.mc.y + verticalChange;
        
            // loop through all fixed objects to see if gravacter has landed
            for(var i:int=0;i<fixedObjects.length;i++) {
                if ((grav.mc.x+grav.width/2 > fixedObjects*.leftside) && (grav.mc.x-grav.width/2 < fixedObjects*.rightside)) {
                    if ((grav.mc.y <= fixedObjects*.topside) && (newY > fixedObjects*.topside)) {
                        newY = fixedObjects*.topside;
                        grav.dy = 0;
                        grav.inAir = false;
                        break;
                    }
                }
            }
            
            // find new horizontal position
            var newX:Number = grav.mc.x + horizontalChange;
        
            // loop through all objects to see if gravacter has bumped into a wally
            for(i=0;i<fixedObjects.length;i++) {
                if ((newY > fixedObjects*.topside) && (newY-grav.height < fixedObjects*.bottomside)) {
                    if ((grav.mc.x-grav.width/2 >= fixedObjects*.rightside) && (newX-grav.width/2 <= fixedObjects*.rightside)) {
                        newX = fixedObjects*.rightside+grav.width/2;
                        grav.hitwallyLeft = true;
                        break;
                    }
                    if ((grav.mc.x+grav.width/2 <= fixedObjects*.leftside) && (newX+grav.width/2 >= fixedObjects*.leftside)) {
                        newX = fixedObjects*.leftside-grav.width/2;
                        grav.hitwallyyRight = true;
                        break;
                    }
            
                }
            }
            
            // set position of gravacter
            grav.mc.x = newX;
            grav.mc.y = newY;
            
            // set animation state
            if (grav.inAir) {
                newAnimState = "jump";
            }
            grav.animstate = newAnimState;