as3 hitTestObject类对象命中类

时间:2015-03-30 11:37:03

标签: actionscript-3

我正在尝试加载多个气泡并让它们在屏幕上反弹。如果他们互相攻击,他们应该“从我的代码中做点什么”。

以下代码无效:

if(e.target.hitTestObject(e.target.getChildAt(1)))
            {
                trace("Items hit");// Runs on every enter frame. Should not. 
            }

这是我的主时间轴:numberPop.as

package {

    import flash.display.MovieClip;
    import flash.events.*;

    import flash.system.Capabilities;
    import flash.display.StageDisplayState;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.system.Capabilities;
    import flash.display.StageDisplayState;

    // Greensock Annimation
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.greensock.plugins.*;
    import com.greensock.plugins.TransformAroundCenterPlugin;
    TweenPlugin.activate([TransformAroundCenterPlugin]);
    TweenPlugin.activate([TransformAroundPointPlugin]);
    import com.greensock.plugins.CacheAsBitmapPlugin;

    import com.theBubble;


    public class numberPop extends MovieClip 
    {

        public var numberBubble:theBubble;

        ///screen rez
        public static var _screenX:int;
        public static var _screenY:int;

        public var Desktop:Boolean;
        public var iPhone:Boolean;

        public var i:Number = 2;

        public var Jesus:Array = new Array('3','4');

        public function numberPop() 
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            setStage(null);

            //addBubble(3);
            TweenMax.delayedCall(1, addBubble);
            TweenMax.delayedCall(3, addBubble);

        }


        public function addBubble()
        {

            numberBubble = new theBubble(i,this);
            addChild(numberBubble);
            numberBubble.name = ""+i+"";
            trace("I here " + numberBubble.name);
            i++;
        }

        public function setStage(e:Event)
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            if (flash.system.Capabilities.screenResolutionX > stage.fullScreenWidth)
            {

                _screenX = stage.fullScreenWidth;
                _screenY = stage.fullScreenHeight;
                //Rez = "Screen Height";
                trace("Second: " + _screenY);

            }
            else
            {
                if(iPhone)
                {
                _screenX = flash.system.Capabilities.screenResolutionY;
                _screenY = flash.system.Capabilities.screenResolutionX;
                }
                else
                {
                _screenX = flash.system.Capabilities.screenResolutionX;
                _screenY = flash.system.Capabilities.screenResolutionY;
                }

                trace("Second: " + _screenY);
            }

            // _screenX = stage.stageWidth;
            //_screenY = stage.stageHeight;
        }


        //////////////////////////
        //////////////////////////
    }

}

这是我加载的课程

package com {

    import flash.display.MovieClip;
    import flash.events.*;

    import flash.system.Capabilities;
    import flash.display.StageDisplayState;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.system.Capabilities;
    import flash.display.StageDisplayState;


    // Greensock Annimation
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.greensock.plugins.*;
    import com.greensock.plugins.TransformAroundCenterPlugin;
    TweenPlugin.activate([TransformAroundCenterPlugin]);
    TweenPlugin.activate([TransformAroundPointPlugin]);
    import com.greensock.plugins.CacheAsBitmapPlugin;


    public class theBubble extends MovieClip 
    {

        public var num:Number;


        public var _screenX:Number;
        public var _screenY:Number;

        public var xDie:Boolean = true;
        public var yDie:Boolean = true;

        public var Main:MovieClip;


        public function theBubble(num:Number,mc:MovieClip) 
        {
            super();
            this.num = num;
            trace("Number is: " + num);

            Main = mc;


            this.Number_text.text = "" + num + "";

            this.addEventListener(MouseEvent.CLICK, bubbleAction);
            this.addEventListener(Event.ENTER_FRAME, checkHit);


            trace(Main);

        }




        public function checkHit(e:Event)
        {

            if(e.target.hitTestObject(e.target.getChildAt(1)))
            {
                trace("Items hit");
            }



            var xVal = 5;
            var yVal = 5;

            if(xDie)
            {
            this.x = this.x + xVal;
            }
            else
            {
                this.x = this.x - xVal;
            }

            if(this.x >= numberPop._screenX - this.width)
            {
                xDie = false;
            }
            if(this.x <= 0)
            {
                xDie = true;
            }
            ///////
            if(yDie)
            {
                this.y = this.y + yVal;
            }
            else
            {
                this.y = this.y - yVal;
            }
            if(this.y >= numberPop._screenY - this.height)
            {
                yDie = false;
            }
            if(this.y <= 0)
            {
                yDie = true;
            }




        }



        public function bubbleAction(e:Event)
        {

            trace(this.name);
        }

    }

}

1 个答案:

答案 0 :(得分:0)

e.target.getChildAt(1)是e.target的孩子,所以它总会被击中。你会想要以某种方式分开泡泡和你正在测试的任何东西。