如何添加碰撞检测

时间:2013-06-06 01:12:55

标签: flash actionscript

我正在尝试制作一个游戏,坏人在飞机上飞行,你必须尝试躲避他们。我设法编写了飞机的控制以及如何将坏人添加/移动到舞台上。我想知道我是如何对碰撞检测进行编码的,这样如果飞机被坏人击中,我将被带到“游戏结束”屏幕。

import flash.utils.Timer;
import flash.events.TimerEvent;

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var baddies:Array = new Array();
var baddieSpeed:int = 15;
var timer: Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.start();

//deg2rad = Math.PI/180;
//rad2deg = 180/Math.PI;

plane.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);



function fl_MoveInDirectionOfKey_2(event:Event)
{
    if (upPressed)
    {
        plane.y -= 8;
    }
    if (downPressed)
    {
        plane.y += 8;
    }
    if (leftPressed)
    {
        plane.x -= 8;
    }
    if (rightPressed)
    {
        plane.x += 8;
    }
}

function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = true;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = true;
            break;
        }
        case Keyboard.LEFT:
        {
            leftPressed = true;
            break;
        }
        case Keyboard.RIGHT:
        {
            rightPressed = true;
            break;
        }
    }
}

function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = false;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = false;
            break;
        }
        case Keyboard.LEFT:
        {
            leftPressed = false;
            break;
        }
        case Keyboard.RIGHT:
        {
            rightPressed = false;
            break;
        }
    }
}

timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.start();
function addBaddie(evt:TimerEvent):void {
    var baddie:Baddie = new Baddie();
    baddie.x = - baddie.width
    baddie.y = Math.random() * stage.stageHeight;
    baddie.angle = getAngle(baddie.x, baddie.y, plane.x, plane.y);
    baddie.rotation = getAngle(baddie.x, baddie.y, plane.x, plane.y);
    baddie.speed = baddieSpeed
    addChild(baddie);
    baddies.push(baddie);
    }

var baddieMover:Timer = new Timer(40);
baddieMover.addEventListener(TimerEvent.TIMER, moveBaddies);
baddieMover.start();
var counter:int = 0;
function moveBaddies(evt:TimerEvent):void {
    for (var i:int = 0; i < baddies.length; i++) {
        var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
        var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
        baddies[i].x += dx;
        baddies[i].y += dy;
    }
}



function deg2rad(deg:Number):Number {
    return deg * (Math.PI/180);
}

function rad2deg(rad:Number):Number {
    return rad * (180 / Math.PI);
}



function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
    var radians:Number = Math.atan2(y2 - y1, x2 - x1);
    return rad2deg(radians);
}

2 个答案:

答案 0 :(得分:0)

获取物体的边界 - 即坏人和飞机,然后检查交叉点。在伪代码中:

Rectangle rect_1 = plane.getBounds(); 
Rectangle rect_2 = baddie.getBounds();

if(rect_1.intersects(rect_2))
{
   DoSomethingAmazing();
}

以下是更详细的教程:http://zetcode.com/tutorials/javagamestutorial/collision/

答案 1 :(得分:0)

Flash有一个名为hitTestObject的内置方法,可用于简单的基于方形/边界框的碰撞。

if(player.hitTestObject(baddies[i])){
//kill the player or whatever, go to game over screen, break out of for loop
}