AS3.如何做好英雄与敌人动画的碰撞?



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

在"保持动画"中,我的玩家的宽度为30px,在步行动画40px和Attack1动画宽度60px中。

现在我使用代码:

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

如果敌人触摸英雄(主角)开始攻击动画。但是,这是一个问题,如果球员的当前状态处于进攻位置,敌人会留下来袭击的敌人。

我需要做一个像敌人总是检查玩家的"停留"动画宽度,而不是当前动画。也许您可以建议更好的碰撞技巧?谢谢。

更新因此,如果我有简单的攻击1,我需要在此功能中调用enterFrameHandler()吗?这将检查英雄是否与敌人发生碰撞?或我需要使用此功能的地方?

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.

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

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

我相信您可以尝试使用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通道值被认为是不透明的。

参数

  • 第一个点:点 - 位于任意坐标空间中位板块图像的左上角的位置。定义二标准参数。

  • 使用相同的坐标空间。
  • firstalphathreshold:uint - 该命中测试被认为是不透明的最小的alpha通道值。

  • secondObject:object - 矩形,点,位图或bitmapdata对象。

  • secondbitmapdatapoint:point (default = null) - 在第二个BitMapData对象中定义像素位置的点。仅在第二个对象的值为bitmapdata对象时才使用此参数。

  • sectalphathreshold:uint (默认值= 1) - 第二位BitmapData对象中被视为不透明的alpha通道值。仅当第二个对象的值为bitmapdata对象并且两个bitmapdata对象都是透明的时才使用此参数。

返回

布尔人 - 如果发生命中,则为真实的值;否则,false。

投掷

参数 - 第二个对象参数不是点,矩形,位图或位mapdata对象。

TypeError - 第一个点为null。

最新更新