0

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

Enemy.as:这个类与一个MovieClip(在库中)链接,具有简单的敌人移动和方向的代码。

Hero.as:与库中的影片剪辑链接。英雄简单动作代码

EnemyManager.as:每 20 秒创造新的敌人。

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

HittingManager.as:检查勾结(问题在这里

现在我的问题在HittingManager.as课堂上,因为我想在这个类中添加 HitTestObject 功能。我将发布 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
4

1 回答 1

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);
于 2013-10-25T17:36:51.180 回答