AS3 - 命中测试对象在不同的​​类中

时间:2014-05-19 20:23:56

标签: actionscript-3

我正在制作乒乓球

我需要球来感知墙壁和两个桨的检测

如何在不制作中央班级的情况下实现这一目标,并为Paddle 1 paddle 2和Ball保留3个不同的类别?

球类

package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;

    public class Grey extends MovieClip {
        /*var blues:Array = new Array
        var blue:Blue = new Blue();*/
        private var changes:Boolean
        private var directions
        private var speed:int = 20;
        public function Grey() {
            // constructor code
            stage.addEventListener(Event.ENTER_FRAME, loop)
            /*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
        }

        function loop(e:Event) {
            movements();
            hitTesting();
        }

        function movements() {
            if (changes==true) {
                directions = -1;
            } else {
                directions = 1;
            }

            x+=speed * directions;
        }

        function hitTesting() {
            /*if (this.hitTestObject(blues)) {
                changes = true;
            }*/
        }
    }
}

Paddle 1 Class(PLAYER)

package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;


    public class Blue extends MovieClip {

        private var speed:int = 10;
        public function Blue() {
            // constructor code
            stage.addEventListener(Event.ENTER_FRAME, loop);
            /*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
        }

        function loop(e:Event) {
            if (parent.mouseY > 0 && parent.mouseY < 360) {
                y = parent.mouseY;
            }
        }

        /*function pressed(e:KeyboardEvent) {
            switch(e.keyCode) {
                case 38:
                    y-=speed;
                break;

                case 40:
                    y+=speed;
                break;
            }
        }*/
    }
}

Paddle 2 Class(CPU)

package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;


    public class Red extends MovieClip {
        var greyy:Grey = new Grey();
        private var speed:int = 10;

        public function Red() {
            // constructor code
            stage.addEventListener(Event.ENTER_FRAME, loop);
            /*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
        }

        function loop(e:Event) {
            if (greyy.y > y) {
                y-=3;
            } else if(greyy.y < y){
                y+=3;
            }
        }
    }
}

0 个答案:

没有答案