Actionscript 3.0。如何校准碰撞检测?

时间:2017-01-25 18:54:19

标签: actionscript-3 flash

这是我正在制作的Flash游戏。我正在尝试改进LevelOnePlayer和Wall11之间的碰撞检测。但是,即使我将墙壁命中检测变量更改为0.1像素,当游戏距离墙壁太近或太远时,游戏仍会继续检测到碰撞。反正有没有使碰撞检测像素完美?

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;
    }

1 个答案:

答案 0 :(得分:1)

如果您正在寻找像素完美碰撞检测,您需要稍微改变您的方法并使用BitmapData.hitTest();代替MovieClip.hitTestPoint();您可以阅读2个函数/实现之间的差异{{3如果你愿意的话。

要实现这一点,您可以转到here,但这是他们提供的示例代码段(confusingggggg);

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

var bmd1:BitmapData = new BitmapData(80, 80, true, 0x00000000);
var rect:Rectangle = new Rectangle(20, 20, 40, 40);
bmd1.fillRect(rect, 0xFF0000FF);

var pt1:Point = new Point(1, 1);
trace(bmd1.hitTest(pt1, 0xFF, pt1)); // false
var pt2:Point = new Point(40, 40);
trace(bmd1.hitTest(pt1, 0xFF, pt2)); // true

在您的情况下,您可以执行以下操作,但请注意我在没有IDE的情况下编写了以下内容,因此可能存在语法错误或两个错误:

var Wall1Rect:Rectangle = Wall1.getBounds(this);
var Wall1BmpData = new BitmapData(Wall1Rect.width, Wall1Rect.height, true, 0);

var playerRect:Rectangle = LevelOnePlayer.getBounds(this);
var playerBmpData = new BitmapData(playerRect.width, playerRect.height, true, 0);

if(playerBmpData.hitTest(new Point(LevelOnePlayer.x, LevelOnePlayer.y), 255, Wall1BmpData, new Point(Wall1.x, Wall1.y),255))
{
    trace("hit");
}

另一件好事adobe docs