【问题标题】:AS3. How to make good collision between hero's and enemy's animations?AS3。如何在英雄和敌人的动画之间进行良好的碰撞?
【发布时间】:2014-02-17 15:36:43
【问题描述】:

在玩家和敌人之间产生碰撞的最佳方法是什么?

在逗留动画中,我的播放器宽度为 30 像素,在行走动画中为 40 像素,在攻击 1 动画中宽度为 60 像素。

现在我使用代码:

if (Enemy.hitTestObject(Hero))
            {
    Enemy.gotoAndStop("attack1");
    }

如果敌人触摸英雄(主角)开始攻击动画。但是这里有一个问题,如果玩家的当前状态处于攻击位置,敌人会停留这么远蚂蚁试图击中。

我需要制作类似敌人的东西,总是检查玩家的“停留”动画宽度,而不是当前动画。或者,也许您可​​以提出更好的碰撞提示?谢谢。

更新 所以如果我有简单的attack1,我需要在这个函数中调用enterFrameHandler()吗?这将检查Hero是否与Enemy发生碰撞?或者我需要在哪里使用这个功能?

if (attack1)
{
            enterFrameHandler();
    Hero.gotoAndStop("attack1");

}

更新 2

这就是我宣布敌人的方式:

public var Enemy:Priesas = new Priesas; //Priesas is instance name of Enemy

英雄正在通过点击按钮选择模板:

public function selectHero(what:int):void {
    // this is called with correct "what", design yourself. I use array index
    var whatHero:Class = heroes[what]; // get selected hero symbol
    if (Hero && Hero.parent) Hero.parent.removeChild(Hero);
    // clean up previous hero. Drop listeners here, if any
    Hero = new whatHero(); // get new hero
    // process as usual, don't forget to "addChild(Hero)" somewhere
    create_hero();
}

    function choosePlayer(event:MouseEvent):void {
        selectHero(0);
        start(event);
        }

     function create_hero()
     {
        addChild(Hero);
     }

所以声明的变量是:英雄和敌人

更新 3

现在我收到以下错误:

1120: Access of undefined property enemyClipBmpData.
1180: Call to a possibly undefined method Point.
1120: Access of undefined property heroClipBmpData.
1180: Call to a possibly undefined method GlowFilter.

这是我的代码现在的样子(我通过创建变量知道大写,但现在我需要这样使用)

    public function Script()
    {

        btn_play.addEventListener(MouseEvent.CLICK, start);
        btn_credits.addEventListener(MouseEvent.CLICK, choosePlayer);
        btn_control.addEventListener(MouseEvent.CLICK, start);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
        stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
        addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true); //here added line

    }

    function enterFrameHandler(event:Event):void
    {
        Hero.x = mouseX;
        Hero.y = mouseY;

        if(enemyClipBmpData.hitTest(new Point(Enemy.x, Enemy.y),
                                255,
                                heroClipBmpData,
                                new Point(Hero.x, Hero.y),
                                255

                          ))
        {
            trace("hit");
            Enemy.filters = [new GlowFilter()];
        }
        else
        {
            Enemy.filters = [];
        }
    }

function create_enemy()
        {
            addChild(Enemy);
            var enemyRect:Rectangle = Enemy.getBounds(this);
            var enemyClipBmpData = new BitmapData(enemyRect.width, enemyRect.height, true, 0);
            enemyClipBmpData.draw(Enemy);
            Enemy.x = 10;
            Enemy.y = 420;
        }

     function create_hero()
    {

        addChild(Hero);
        var heroRect:Rectangle = Hero.getBounds(this);
        var heroClipBmpData = new BitmapData(heroRect.width, heroRect.height, true, 0);
        heroClipBmpData.draw(Hero);
        Hero.gotoAndStop("stay");
        Hero.x = stage.stageWidth / 2;
}

【问题讨论】:

  • 一种解决方案是在你的角色上添加一个不可见的块,用于碰撞,并针对它进行测试。

标签: actionscript-3 animation actionscript collision-detection collision


【解决方案1】:

我相信您可以尝试使用 BitmapData.hitTest,例如:

//using your instance name (just using lower case for the instance name)
var enemy:Priesas = new Priesas();
enemy.x = enemy.y = 300;
addChild(enemy);

var enemyRect:Rectangle = enemy.getBounds(this);
var enemyClipBmpData = new BitmapData(enemyRect.width, enemyRect.height, true, 0);
enemyClipBmpData.draw(enemy);

//using your instance name (just using lower case for the instance name)
var hero:Sprite = new Hero();
addChild(hero);

var heroRect:Rectangle = hero.getBounds(this);
var heroClipBmpData = new BitmapData(heroRect.width, heroRect.height, true, 0);
heroClipBmpData.draw(hero);

addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);

function enterFrameHandler(event:Event):void
{
    hero.x = mouseX;
    hero.y = mouseY;

    if(enemyClipBmpData.hitTest(new Point(enemy.x, enemy.y),
                            255,
                            heroClipBmpData,
                            new Point(hero.x, hero.y),
                            255

                      ))
    {
        trace("hit");
        enemy.filters = [new GlowFilter()];
    }
    else
    {
        enemy.filters = [];
    }
}

根据文档:

public function hitTest(firstPoint:Point, firstAlphaThreshold:uint, secondObject:Object, secondBitmapDataPoint:Point = null, secondAlphaThreshold:uint = 1):Boolean

在一个位图图像和一个点、矩形或其他位图图像之间执行像素级命中检测。命中定义为一个点或矩形在一个不透明像素上的重叠,或两个重叠的不透明像素。执行命中测试时,不考虑任一对象的拉伸、旋转或其他变换。

如果图像是不透明的图像,则此方法将其视为完全不透明的矩形。两个图像都必须是透明图像才能执行考虑透明度的像素级命中测试。当您测试两个透明图像时,alpha 阈值参数控制 0 到 255 的 alpha 通道值被认为是不透明的。

参数

  • firstPoint:Point — BitmapData 图像左上角在任意坐标空间中的位置。定义 secondBitmapPoint 参数时使用相同的坐标空间。

  • firstAlphaThreshold:uint - 被认为对于此命中测试不透明的最小 Alpha 通道值。

  • secondObject:Object — 矩形、点、位图或 BitmapData 对象。

  • secondBitmapDataPoint:Point(默认 = null)— 定义第二个 BitmapData 对象中像素位置的点。仅当 secondObject 的值为 BitmapData 对象时才使用此参数。

  • secondAlphaThreshold:uint(默认 = 1)— 在第二个 BitmapData 对象中被视为不透明的最小 Alpha 通道值。仅当 secondObject 的值为 BitmapData 对象且两个 BitmapData 对象都是透明的时才使用此参数。

退货

Boolean — 如果发生命中,则值为 true;否则为假。

投掷

ArgumentError — secondObject 参数不是 Point、Rectangle、Bitmap 或 BitmapData 对象。

TypeError — firstPoint 为空。

【讨论】:

  • 谢谢你的回答,你能解释一下代码吗?它是如何工作的?
  • 当我尝试这个时,我得到了错误:1120: Access of undefined property blueClip.,1120: Access of undefined property redClip.,1120: Access of undefined property redClipBmpData.
  • 我只是添加了有关该方法的文档的更多详细信息,因此,将有可能理解。您将 redClip 和 blueClip 替换为您的实例名称,我只是将其添加为示例。
  • 感谢您的帮助!我用我现在误解的问题更新了我的问题。
  • 不,我发给你的例子,是检测Hero和Enemy的碰撞(你替换原来的hitTestObject)
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多