0

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

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

2 回答 2

0

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

if(player.hitTestObject(baddies[i])){
//kill the player or whatever, go to game over screen, break out of for loop
}
于 2013-06-12T17:08:03.340 回答
0

获取对象的边界 - 即坏人和平面,然后检查相交。在伪代码中:

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

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

这是一个更详细的教程:http: //zetcode.com/tutorials/javagamestutorial/collision/

于 2013-06-06T01:24:55.930 回答