Actionscript3如何获取单击的对象索引

时间:2014-04-15 09:06:16

标签: actionscript-3 flash indexing

我正试图让这个英雄射击子弹。例如:子弹有火和冰型。如果放置的英雄是火型,那么它将射击子弹,如果它是冰型,那么它将射击冰弹。每个子弹都有自己的效果和损害。

所以,我试图获得选择的英雄索引,稍后索引将用于定义跟踪使用的子弹(heroesArray.indexOf(heroClicked));但heroclicked的价值是(对象Hero1)。所以我不能使用它,因为heroesArray的数组是[hero1,hero2]。我也做了分裂和加入,但有点搞砸......

我的问题是如何获取仅包含被点击对象变量的字符串值(hero1或hero2)?是否有任何“词汇表”来获取变量名称,例如getqualifiedclassname用于获取对象的类名?

或者有没有其他想法在不使用indexOf的情况下创建与英雄类型相同的项目符号类型?

谢谢!

以下是代码:

package  {
import flash.display.MovieClip
import flash.events.MouseEvent
import flash.events.Event
import flash.display.Sprite
import flash.utils.*

public class Hero {
    private var heroesArray:Array;
    private var heroContainer:Sprite = new Sprite;
    private var hero1:MovieClip = new Hero1();
    private var hero2:MovieClip = new Hero2();
    private var bulletArray:Array;
    private var bullet1:MovieClip = new Bullet1();
    private var bullet2:MovieClip = new Bullet2();
    private var moveHero:Boolean = false;
    private var movingHero:MovieClip;
    private var _money:Money =  new Money();
    private var _main:Main;
    private var _enemy:Enemy = new Enemy(_main);

    public function Hero(main:Main) 
    {   _main = main;
        heroesArray = [hero1,hero2];
        bulletArray = [bullet1,bullet2];
    }

    private function playerMoving(e:Event):void
    {
        if (moveHero == true)
        {
            movingHero.x = _main.mouseX;
            movingHero.y = _main.mouseY;
        }


    }

    private function chooseHero(e:MouseEvent):void
    {   
        var heroClicked:MovieClip = e.currentTarget as MovieClip;

        var cost:int = _main._money.money ;
        if(cost >= 10 && moveHero == false)
        {
            _main._money.money -= 10;
            _main._money.addText(_main);

            moveHero = true;
            var heroClass:Class = getDefinitionByName(getQualifiedClassName(heroClicked)) as Class;
            movingHero = new heroClass(); 
            heroContainer.addChild(movingHero);
            movingHero.addEventListener(MouseEvent.CLICK, placeHero);
        }
    }

    private function placeHero(e:MouseEvent):void
    {   
        var heroClicked:MovieClip = e.currentTarget as MovieClip;

        var heroRow:int = Math.floor(_main.mouseY/75);
        var heroCol:int = Math.floor((_main.mouseX-10)/65);

        if(heroRow>0 && heroCol>0 && heroRow<6 && heroCol<10&&
            _main.field[heroRow][heroCol]==0)

        {   
            movingHero.fireRate =75;
            movingHero.recharge = 0;
            movingHero.firing = false;
            movingHero.heroRow = heroRow;

            movingHero.x = 42+heroCol*65;
            movingHero.y = 10+heroRow*75;
            _main.field[heroRow][heroCol]=1;
            moveHero = false;
            movingHero.removeEventListener(MouseEvent.CLICK, placeHero);    
        }
    }

    public function displayHero(stage:Object):void
    {   
        stage.addChild(heroContainer);

        for (var i:int = 0; i<2;i++)
            {
                stage.addChild(heroesArray[i]);
                heroesArray[i].x = 37;
                heroesArray[i].y = 80+i*70;
                heroesArray[i].width=60;
                heroesArray[i].height=55;
                heroesArray[i].buttonMode = true;
                heroesArray[i].addEventListener(MouseEvent.CLICK, chooseHero);
                heroesArray[i].addEventListener(Event.ENTER_FRAME, playerMoving);


            }
    }

}

}

1 个答案:

答案 0 :(得分:0)

你几乎已经在你面前得到了答案。这两个英雄是Hero1和Hero2,你可以通过

找到课程
var heroClass:Class = getDefinitionByName(getQualifiedClassName(heroClicked)) as Class;

您需要做的就是将heroClass与这两个类进行比较:

if(heroClass == Hero1)
{
   ...
}
else
{
   ...
}

将英雄链接到子弹类型

如果您不了解或不想学习课程(请参阅我的评论以获得优秀教程),那么您需要一个查找表/词典,以便您可以链接项目符号类型对英雄。

//Put this in your constructor after the bullet types and heroes have been declared
var bulletDict = new Dictionary();
bulletDict[hero1] = bulletTypeIce; //Note the lack of quotations around hero1 and 2
bulletDict[hero2] = bulletTypeFire;

所以,要把子弹打开:

var bulletType = bulletDict[heroClicked];