mercredi 25 janvier 2017

Actionscript 3.0. How to calibrate collision detection?

This is for a flash game that I am working on. I am trying to improve the collision detection between LevelOnePlayer and Wall11. However, even if I change the Wall Hit Detection variables by 0.1 pixels, the game keeps on detecting collision when the player is too close or too far from the walls. Is there anyway to make the collision detection pixel perfect?

const LEFT_WALL_HIT_DETECTION: Number=5.5;
const RIGHT_WALL_HIT_DETECTION: Number=5.5;
const UP_WALL_HIT_DETECTION: Number=5.5;
const DOWN_WALL_HIT_DETECTION: Number=5.5;
const ENEMY_HIT_DETECTION: Number=5.5;

var left:Boolean = false;
var right:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;

var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;



var Onespeed: Number=3.5;
var LevelOneStartFrame: Number=1010; 
addEventListener(Event.ENTER_FRAME, LevelOne);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased); 
    function keyPressed(event:KeyboardEvent): void{
        if(event.keyCode == Keyboard.LEFT || event.keyCode == 65){
            left=true; 
        }
        if(event.keyCode == Keyboard.RIGHT || event.keyCode == 68){
            right=true;
        }
        if(event.keyCode == Keyboard.UP || event.keyCode == 87){
            up=true;  
        }
        if(event.keyCode == Keyboard.DOWN || event.keyCode == 83){
            down=true; 
        }
    }
    function keyReleased(event:KeyboardEvent): void{
        if(event.keyCode == Keyboard.LEFT || event.keyCode == 65){
            left=false;
        }
        if(event.keyCode == Keyboard.RIGHT || event.keyCode == 68){
            right=false;
        }
        if(event.keyCode == Keyboard.UP || event.keyCode == 87){
            up=false;
        }
        if(event.keyCode == Keyboard.DOWN || event.keyCode == 83){
            down=false;
        }
    }
    function LevelOne(event:Event){
        if(left){
            LevelOnePlayer.x-=Onespeed; 
        }
        if(right){
            LevelOnePlayer.x+=Onespeed; 
        }
        if(up){
            LevelOnePlayer.y-=Onespeed; 
        }
        if(down){
            LevelOnePlayer.y+=Onespeed; 
        }
        if(Wall11.hitTestPoint(LevelOnePlayer.x-LEFT_WALL_HIT_DETECTION,       LevelOnePlayer.y, true))
        leftBumping=true; else leftBumping=false;
        if(Wall11.hitTestPoint(LevelOnePlayer.x+RIGHT_WALL_HIT_DETECTION, LevelOnePlayer.y, true))
        rightBumping=true; else rightBumping=false;
        if(Wall11.hitTestPoint(LevelOnePlayer.x, LevelOnePlayer.y+DOWN_WALL_HIT_DETECTION, true))
        downBumping=true; else downBumping=false;
        if(Wall11.hitTestPoint(LevelOnePlayer.x, LevelOnePlayer.y-UP_WALL_HIT_DETECTION, true))
        upBumping=true; else upBumping=false;

        if(leftBumping) LevelOnePlayer.x+=Onespeed;
        if(rightBumping) LevelOnePlayer.x-=Onespeed;
        if(downBumping) LevelOnePlayer.y-=Onespeed;
        if(upBumping) LevelOnePlayer.y+=Onespeed;
    }

Aucun commentaire:

Enregistrer un commentaire