hitTestObject在一个单独的类中

时间:2013-10-25 17:13:17

标签: actionscript-3 actionscript hittest

我正在做一个简单的游戏。这是我面临的问题,但首先我会告诉你我的班级结构。 (我正在使用flash cs5.5)

Enemy.as :此类与MovieClip(在库中)相关联,具有简单的敌人动作和路线代码。

Hero.as :与库中的MovieClip相关联。英雄简单动作代码

EnemyManager.as :每20秒创建一个新敌人。

HeroManager.as :创建英雄(仅限一次,稍后会添加其他功能)。

HittingManager.as :检查合谋( 问题

现在我的问题在HittingManager.as类,因为我想在这个类中添加HitTestObject Functionalty。我将发布3个重要类的代码。 ( EnemyManager.as HeroManager.as HittingManager.as

package  
{    
import flash.display.MovieClip;
import flash.events.Event;

public class HeroManager extends MovieClip
{
    private var hManager:HittingManager = new HittingManager();
    public static var hero:Hero = new Hero();

    public function HeroManager() 
    {
        addEventListener(Event.ADDED_TO_STAGE, added);
    }

    private function added(event:Event):void
    {
        trace("hero manager added");

    }

    }//class

}//package

这是EnemyManager.as类的代码

package 
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.sampler.Sample;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sampler.NewObjectSample;

public class EnemyManager extends MovieClip
{

    private var hManager:HittingManager = new HittingManager();
    private var timer:Timer = new Timer(2000);
    public static var hitting:Boolean = false;
    public static var enemy:Enemy;

    public function EnemyManager()
    {           
        addEventListener(Event.ADDED_TO_STAGE, added);          
    }
    public function addEnemy(newEnemy:Enemy):void
    {
        addChild(newEnemy);
        hManager.registerEnemy(newEnemy);
    }

    private function added(event:Event):void
    {
        addEventListener(Event.ENTER_FRAME, update);
        trace("added enemy manger");
        timer.addEventListener(TimerEvent.TIMER, onTimer);
        timer.start();
    }
    private function onTimer(event:TimerEvent):void
    {           
         enemy = new Enemy();            

         this.addEnemy(enemy);
    }
    private function update(event:Event):void
    {

    }

    }

}

在这里Hittest.as上课(我已经尝试了很多技术,但都是徒劳的)所以如果法庭空了我就要离开

package 
{
import flash.display.MovieClip;
import flash.events.Event;

public class HittingManager extends MovieClip
{
    private var eManager:EnemyManager;

    private var hEnemy:Enemy = new Enemy();
    private var _enemies:Array;
    private var _hero:Hero;
    public function HittingManager()
    {
        //trace("Hitting Manager working");
        _enemies = [];
        addEventListener(Event.ADDED_TO_STAGE, added);
    }
    public  function registerEnemy(newEnemy:Enemy):void
    {   
        _enemies.push(newEnemy);

    }
    public  function registerHero():void
    {
        //trace("heroRegisterd");
        _hero = HeroManager.hero;
        addChild(_hero);


    }

    private  function added(event:Event):void
    {
        if(!_hero)
        {
            this.registerHero();
        }
        trace("Hitting manger added");
        addEventListener(Event.ENTER_FRAME, update);
    }

    private function update(event:Event):void
    {


        if(_hero)
        {           
            for each( newEnemy:Enemy in _enemies)
            {
                if(_hero.hitTestObject(newEnemy) )
                {
                    trace("Hitting")
                }
            }
        }

    }
    }//class

}//package

1 个答案:

答案 0 :(得分:0)

下面的修订版使您的代码尽可能少地进行更改,但它绝不是理想的解决方案。 您可能应该重构代码,以便敌方管理器不需要对HittingManager的公共静态引用

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class HeroManager extends MovieClip
    {
        public static var hero:Hero = new Hero();

        public function HeroManager()
        {
            addEventListener(Event.ADDED_TO_STAGE, added);
        }

        private function added(event:Event):void
        {
            trace("hero manager added");

        }

    } //class

} //package

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.sampler.Sample;
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.sampler.NewObjectSample;

    public class EnemyManager extends MovieClip
    {

        public static var hManager:HittingManager;
        private var timer:Timer = new Timer(2000);
        public static var hitting:Boolean = false;
        public static var enemy:Enemy;

        public function EnemyManager()
        {
            addEventListener(Event.ADDED_TO_STAGE, added);
        }

        public function addEnemy(newEnemy:Enemy):void
        {
            addChild(newEnemy);
            hManager.registerEnemy(newEnemy);
        }

        private function added(event:Event):void
        {
            addEventListener(Event.ENTER_FRAME, update);
            trace("added enemy manger");
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
        }

        private function onTimer(event:TimerEvent):void
        {
            enemy = new Enemy();

            this.addEnemy(enemy);
        }

        private function update(event:Event):void
        {

        }

    }

}


package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class HittingManager extends MovieClip
    {
        private var _enemies:Array;
        private var _hero:Hero;

        public function HittingManager()
        {
            //trace("Hitting Manager working");
            _enemies = [];
            addEventListener(Event.ADDED_TO_STAGE, added);
        }

        public function registerEnemy(newEnemy:Enemy):void
        {
            _enemies.push(newEnemy);

        }

        public function registerHero():void
        {
            //trace("heroRegisterd");
            _hero = HeroManager.hero;
            addChild(_hero);

        }

        private function added(event:Event):void
        {
            if (!_hero)
            {
                this.registerHero();
            }
            trace("Hitting manger added");
            addEventListener(Event.ENTER_FRAME, update);
        }

        private function update(event:Event):void
        {

            if (_hero)
            {
                for each (var newEnemy:Enemy in _enemies)
                {
                    if (_hero.hitTestObject(newEnemy))
                    {
                        trace("Hitting")
                    }
                }
            }

        }
    } //class

} //package

在您的主要文档课程中,您应该具有以下内容:

var hittingManager:HittingManager =  new HittingManager();
EnemyManager.hManager = hittingManager;

var enemyManager:EnemyManager = new EnemyManager();
var heroManager:HeroManager = new HeroManager();

addChild(heroManager);
addChild(enemyManager);
addChild(hittingManager);