【问题标题】:How to create a Random Generator for Movie Clip MouseEvents?如何为电影剪辑鼠标事件创建随机生成器?
【发布时间】:2014-02-27 03:53:23
【问题描述】:

我目前正在开发一个游戏,其中有不同的MovieClip 对象通过计时器事件数组添加到舞台。

这些影片剪辑由MouseEvent.MOUSE_DOWN 处理,因此当用户触摸屏幕上显示的正确对象时,nScore 会增加 1 分,但我想做的是在随机数量的用户获得的时间或随机点数我希望其中一个对象不再处于活动状态,因此如果您触摸不活动的对象,那么用户就会失败。然后在随机时间或分数之后,对象再次发生变化,您只需触摸该对象而不是屏幕上显示的前一个对象等...

所以我像这样创建了两个鼠标事件监听器:

mSquare.addEventListener(MouseEvent.MOUSE_DOWN, SquareNotActivated);
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, SquareIsActivated);

所以在我的 SquareIsActivated 函数中,我有另一个名为 SquareActivate 的函数,其代码如下:

private function SquareActivate(square:DisplayObject):void 
{          
    nScore ++;
    updateHighScore();
    updateCurrentScore();          
}

在我的 SquareNotActivated 函数中,我有一个名为squareNotActive 的函数,它具有以下代码:

private function squareNotActive(square:DisplayObject):void 
{  
     nLives --;
}

然后对于另一个名为mPop的影片剪辑对象@我有相同的设置。

我将尝试为nScore 制作多个if 语句来处理事件侦听器的更改以及我将尝试这样做的显示对象:

private function checkNScore():void 
{
    if (nScore >= 2)
    {
        // then remove a mouse listener for one object and add another
        stage.addChild(pop_Icon);
        pop_Icon.x = (stage.stageWidth / 2) + 180;
        pop_Icon.y = (stage.stageHeight / 2) - 300;             

    }

    if (nScore >= 4)
    {    
        //Remove Square icon
        pop_Icon.destroyPopIcon();

        //Add new Square icon
        stage.addChild(square_Icon);
        square_Icon.x = (stage.stageWidth / 2) + 180;
        square_Icon.y = (stage.stageHeight / 2) - 300;
    }
}

但我知道必须有一种方法可以随机调用它们,也许是一个计时器对象,它生成一个随机的MovieClip,用户必须触摸它,然后放置所有其他电影剪辑来删除生命,而不是多个if 语句.谁能把我推向正确的方向?

【问题讨论】:

    标签: android actionscript-3 flash random mouseevent


    【解决方案1】:

    我认为最好扩展MovieClip 为您创建另一个可点击对象的类,然后向它们添加type 属性。

    在计时器事件中,您只需更改目标类型。

    通过这种方式,您可以对所有对象使用相同的函数。

    例如:

    扩展的 MovieClip 类:

    package test
    {
        import flash.display.MovieClip;
    
        /**
         * This is a class that extends MovieClip and have a public type property.
         */
        public class MyObject extends MovieClip
        {
            public var type :int;
    
            public function MyObject( type = 0 )
            {
                super();
                this.type = type;
    
                // for this test i just create a circle with a color associated with the type
                this.graphics.beginFill( type*50 );
                this.graphics.drawCircle(0,0,20);
                this.graphics.endFill();
            }
        }
    }
    

    使用方法:

    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.utils.Timer;
        import flash.utils.getTimer;
    
        import test.MyObject;
    
        public class Main extends Sprite
        {
            /** the timer to change the type **/
            private var timer       :Timer;
            /** the current target type **/
            private var targetType  :int;
            /** the objects available **/
            private var objects     :Vector.<MyObject>;
    
            /** the last time of the enterFrameEvent **/
            private var lastTime        :int;
            /** the last time the target type changed **/
            private var lastTypeChanged :int;
            /** the last time we created an object **/
            private var lastObjectCreated   :int;
    
            public function Main()
            {
                objects = new Vector.<MyObject>();
    
                generateObject();
                changeTargetType();
    
                lastTime = getTimer();
                addEventListener( Event.ENTER_FRAME, onEnterFrame );
            }
    
            /** generate a new object and place it on the stage **/
            private function generateObject():void
            {
                // generate a random type
                var type:int = int(Math.random()*10)
                // create the object
                var o:MyObject = new MyObject( type );
                // place the object randomly on the stage
                o.x = 50 + (Math.random() * 350);
                o.y = 50 + (Math.random() * 350);
                // add mouse evtn listener to the object
                o.addEventListener( MouseEvent.CLICK, handleClick );
                // add the object to the display list
                addChild( o );
                // add the object to the array
                objects.push( o );
                // reset the timer
                lastObjectCreated = 0;
            }
    
            private function changeTargetType():void
            {
                // get an object randomly on the array to be sure the type is available on screen
                var typeId:int = Math.random()*objects.length;
                // get the type of the random object
                targetType = objects[typeId].type;
                // reset the timer
                lastTypeChanged = 0;
    
                trace( "new Type::",targetType );
            }
    
            /** on enter frame, calculate the passed time and see what you want to do **/
            private function onEnterFrame( event:Event ):void
            {
                // calculate passed time
                var passedTime:int = getTimer() - lastTime;
                //trace( passedTime, lastTime, lastTypeChanged, lastObjectCreated );
                lastTime = getTimer();
                // add the time to the timers
                lastTypeChanged += passedTime;
                lastObjectCreated += passedTime;
    
                // create an object each 5 seconds
                if( lastObjectCreated >= 5000 )     generateObject();
                // change the target type each 10 seconds
                if( lastTypeChanged >= 10000 )      changeTargetType();
            }
    
            /** handle the click **/
            private function handleClick( event:MouseEvent ):void
            {
                // get the object clicked
                var o:MyObject = event.target as MyObject;
    
                // if it is the goo type
                if( o.type == targetType )
                {
                    //update the score
                    trace( "yeah" );
                    //--> do what you want
    
                    //remove the object from the displayList
                    removeChild(o);
                    // remove listeners on the object
                    o.removeEventListener( MouseEvent.CLICK, handleClick );
                    // remove the object from the array
                    var i:int = objects.indexOf(o);
                    objects.splice(i,1);
                }
                else
                {
                    // remove life.
                    trace( "bad" );
                }
            }
        }
    }
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多