Home > Back-end >  Character keeps falling when it shouldn't be
Character keeps falling when it shouldn't be

Time:06-17

I am doing an assessment task for school in as3 and I have made a feature that will let the character fall if it is not standing on top of something but the boolean variable falling keeps being on when it is not supposed to. I have figured out that what causes it to stop for a short time and then fall again past what it is standing on is that the velocity valuable increases past the floor it is on so I reset the velocity value in the code in another spot but that does not solve the issue of the falling boolean being true when it is not supposed too.

I have put a comment at the temporary solution it is in the gameEngine function in the if(falling) statement

// Imports
import flash.events.KeyboardEvent;
import flash.events.Event;

// Constants that can be edited
const grav:Number = 0.05;
const jumpPow:Number = 15;
const walkingDistance = 50; 

// Variables
var jumpVel:Number = jumpPow;
var velocity:Number = 0;
var dt:Number = 0;
var movingCount:Number = 0;
var newX:Number = 0;
var newY:Number = 0;
var charCornerHit:Number = 0; // 0 = TL 1 = TR 2 = BR 3 = BL (clockwise)
var jumping:Boolean = false;
var falling:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;
var hitDetVal:Boolean = false; // false
var floorHit:Boolean = false;
var object1Hit:Boolean = false;
var cCnrTL:Array = new Array(char.x, char.y);
var cCnrTR:Array = new Array(char.x   char.width, char.y);
var cCnrBR:Array = new Array(char.x   char.width, char.y   char.height);
var cCnrBL:Array = new Array(char.x, char.y   char.height);
var charCorners:Array = new Array(cCnrTL, cCnrTR, cCnrBR, cCnrBL); // Clockwise

addEventListener(Event.ENTER_FRAME, gameEngine);

function gameEngine(evt:Event):void{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter);
    
    charCorners[0][0] = char.x;
    charCorners[0][1] = char.y;
    charCorners[1][0] = char.x   char.width;
    charCorners[1][1] = char.y;
    charCorners[2][0] = char.x   char.width;
    charCorners[2][1] = char.y   char.height;
    charCorners[3][0] = char.x;
    charCorners[3][1] = char.y   char.height;
    
    //trace(char.y);
    
    // Check if char is standing on something
    // Supposed to only check when character is not falling
    if (falling == false) {
        //trace(char.y   char.height   1)
        hitDetection(0, char.y   char.height   1)
        if (hitDetVal == false) {
            falling = true;
            hitDetVal = false;
            trace("not standing");
        }
        else {trace("standing on something");}
    }
    
    // Move char loop
    if (movingRight){
        if (movingCount == walkingDistance) {
            movingCount = 0
            movingRight = false;
        } else {
            char.x =1
            movingCount  = 1;
        }
    } else if (movingLeft) {
        if (movingCount == walkingDistance) {
            movingCount = 0
            movingLeft = false;
        } else {
            char.x-=1
            movingCount  = 1;
        }
    }

    //trace(velocity)
    if (falling) {
        dt  = 1
        velocity = velocity   grav*dt
        
        hitDetection(0, velocity)
        if (hitDetVal) {
            falling = false;
            hitDetVal = false;
            // TEMPORARY SOLUTION - Stopped character from falling past the floor but still ran falling condition
            //velocity = 0;
            //dt = 0;
            if (floorHit) {
                if (char.y < floor.y){
                    char.y = floor.y - char.height;
                    floorHit = false;
                    trace("Char Stopped falling")
                }
            } else if (object1Hit) {
                if (char.y - char.height < object1.y){
                    char.y = object1.y - char.height;
                    object1Hit = false;
                    trace("Char Stopped falling")
                }
            }
        } else {
            char.y  = velocity;
        }
    } else {
        if (jumping) {
            
        }
        velocity = 0;
        dt = 0;
    }
}

function moveCharacter(event:KeyboardEvent):void{
    if(event.keyCode == Keyboard.LEFT){
        if(movingRight){
            movingRight = false;
            trace("STOPPED")
        } else {
            movingCount = 0
            movingLeft = true;
            trace("LEFT");
        }
    }
    if(event.keyCode == Keyboard.RIGHT){
        if(movingLeft){
            movingLeft = false;
            trace("STOPPED")
        } else {
            movingCount = 0
            movingRight = true;
            trace("RIGHT");
        }
    }
    if(event.keyCode == Keyboard.UP){
        jumping = true;
        trace("UP");
    }
}

function hitDetection(distX:Number, distY:Number) {
    for (var i:uint = 0; i < charCorners.length; i  ) {
        newX = charCorners[i][0]   distX; 
        newY = charCorners[i][1]   distY;
        if (floor.hitTestPoint(newX, newY, true)){
            hitDetVal = true;
            floorHit = true;
            charCornerHit = i;
            break;
        } else if (object1.hitTestPoint(newX, newY, true)){
            hitDetVal = true;
            object1Hit = true;
            charCornerHit = i;
            break;
        }
    }
}```

CodePudding user response:

It was a simple logic error occurring here hitDetection(0, char.y char.height 1) I had made adjustments for the location by adding char.y to the value when my hitDetection function already made an adjustment to each corner of my character. Only took ~3 hours to figure out

  • Related