11 Replies Latest reply on Jul 21, 2009 1:22 PM by fox005

    Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear

    fox005 Level 1

        The objective is to get the fox - by using the arrow keys - to move around on the stage and when he touches a duck the duck should disappear of the stage. The duck has a motion tween on it, to make it fly.
      Also if the fox touches one of the traps he should make a sound - using the redfox.wav sound  in the library and disappear off the stage.
      I have this code on the actionscript timeline
      //Program to generate a set number of traps
      //Declaring Variables
      var numOfTraps:Number;
      var movieWidth:Number;
      var movieHeight:Number;
      var counter:Number;
      //setting variables values for traps
      numOfTraps = 6
      movieWidth = Stage.width;
      movieHeight = Stage.height;
      //Program to generate a set number of traps
      //Declaring Variables
          for (counter = 7; counter <= 13; counter++)
      {
      //Place 6 traps on the stage
      _root.attachMovie("trap","trap"+ counter, counter);
      //giving each duck unique characteristics
      _root["trap"+counter]._x = movieWidth * Math.random();
      _root["trap"+counter]._y = movieHeight * Math.random();
      }
      I have this code on the first frame of my fox
      stop();
      var distance:Number = 10;
      var keyListener:Object = new Object();
      keyListener.onKeyDown = function() {
      if (Key.isDown(Key.LEFT)) {
      flyinfox_mc._x = Math.max(flyinfox_mc._x-distance, 0);
      display_txt.text = "Left";
      } else if (Key.isDown(Key.RIGHT)) {
      flyinfox_mc._x = Math.min(flyinfox_mc._x+distance, 650);
      display_txt.text = "Right";
      } else if (Key.isDown(Key.UP)) {
      flyinfox_mc._y = Math.max(flyinfox_mc._y-distance, 0);
      display_txt.text = "Up";
      } else if (Key.isDown(Key.DOWN)) {
      flyinfox_mc._y = Math.min(flyinfox_mc._y+distance, 600);
      display_txt.text = "Down";
      }
      };
      Key.addListener(keyListener);
      I have this code on an instance of the duck that is positioned off-stage (in the grey area).
      onClipEvent(enterFrame){
      if (this.hitTest(_root.duck_mc))
      {
      //trace("Bang your dead");
      }
      }
      I also have an instance of a trap that is positioned off-stage (in the grey area) - no code on it.

        • 1. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
          kglad Adobe Community Professional & MVP

          don't attach code to objects.

           

          create one onEnterFrame loop and check all your hittests in that loop.

          • 2. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
            fox005 Level 1

            Don't know how to go about doing that.

             

            What do I do after I take the code off the duck  and should I use it on the timeline of the duck?

            • 3. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
              kglad Adobe Community Professional & MVP

              all your code should be on your main timeline and you would use something like:

               

              this.onEnterFrame=function(){

              for(var i=0;i<foxTrapArray.length;i++){

              if(foxmc.hitTest(foxTrapArray[i])){  // push all your fox trap movieclips into this array

              // your fox has been trapped.  do whatever.  ? delete this.onEnterFrame

              }

              }

              if(foxmc.hitTest(duckmc)){

              //your duck has been caught.  do whatever. ? delete this.onEnterFrame

              }

              }

              • 4. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                fox005 Level 1

                When I delete the code that I already have on the  actionscript timeline and put on yours, it does not show the ducks or traps on the stage when I run the test movie.  What do I do?

                • 5. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                  kglad Adobe Community Professional & MVP

                  only remove the code that's attached to objects.

                  • 6. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                    fox005 Level 1

                    Put the script that u gave me on the timeline ac and deleted all else, but now my fox is no longer animating with the arrow keys and no ducks or traps on screen either. 

                    • 7. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                      kglad Adobe Community Professional & MVP

                      only remove the code that's attached to objects.  do not remove the code you attached to your frame.

                      • 8. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                        fox005 Level 1

                        All of this code is on the actionscript timeline and nowhere else, but I'm still not able to delete a duck with the fox or delete the fox when he hits a trap.  
                        stop();
                        var distance:Number = 10;
                        var keyListener:Object = new Object();
                        keyListener.onKeyDown = function() {
                        if (Key.isDown(Key.LEFT)) {
                        flyinfox_mc._x = Math.max(flyinfox_mc._x-distance, 0);
                        display_txt.text = "Left";
                        } else if (Key.isDown(Key.RIGHT)) {
                        flyinfox_mc._x = Math.min(flyinfox_mc._x+distance, 650);
                        display_txt.text = "Right";
                        } else if (Key.isDown(Key.UP)) {
                        flyinfox_mc._y = Math.max(flyinfox_mc._y-distance, 0);
                        display_txt.text = "Up";
                        } else if (Key.isDown(Key.DOWN)) {
                        flyinfox_mc._y = Math.min(flyinfox_mc._y+distance, 600);
                        display_txt.text = "Down";
                        }
                        };
                        Key.addListener(keyListener);
                        //Program to generate a set number of ducks
                        //Declaring Variables
                        var numOfDucks:Number;
                        var movieWidth:Number;
                        var movieHeight:Number;
                        var counter:Number;
                        //setting variables values for ducks
                        numOfDucks = 6
                        movieWidth = Stage.width;
                        movieHeight = Stage.height;
                        //Program to generate a set number of traps
                        //Declaring Variables
                            for (counter = 1; counter <= 6; counter++)
                        {
                        //Place 6 ducks on the stage
                        _root.attachMovie("duck","duck"+ counter, counter);
                        //giving each duck unique characteristics
                        _root["duck"+counter]._x = movieWidth * Math.random();
                        _root["duck"+counter]._y = movieHeight * Math.random();
                        }
                        //Program to generate a set number of traps
                        //Declaring Variables
                        var numOfTraps:Number;
                        var movieWidth:Number;
                        var movieHeight:Number;
                        var counter:Number;
                        //setting variables values for traps
                        numOfTraps = 6
                        movieWidth = Stage.width;
                        movieHeight = Stage.height;
                        //Program to generate a set number of traps
                        //Declaring Variables
                            for (counter = 7; counter <= 13; counter++)
                        {
                        //Place 6 traps on the stage
                        _root.attachMovie("trap","trap"+ counter, counter);
                        //giving each duck unique characteristics
                        _root["trap"+counter]._x = movieWidth * Math.random();
                        _root["trap"+counter]._y = movieHeight * Math.random();
                        }
                        for(var i=0;i<foxTrapArray.length;i++){
                        if(flyinfox_mc.hitTest(foxTrapArray[i])){
                        delete this.onEnterFrame
                        }
                        if(flyinfox_mc.hitTest(duck_mc)){
                        delete this.onEnterFrame
                        }
                        }

                        • 9. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                          kglad Adobe Community Professional & MVP

                          use the trace() function to make sure all your instance names are correct.

                          • 10. Re: Fox roll-over duck, delete n disappear - fox roll-over trap, screech n disappear
                            fox005 Level 1

                                Can someone point me help me sort this out!!!

                             

                            Still no collision detected.          This is how my script looks like now - not sure where to put in any other traces.     Why is it not deleting?

                             

                             

                             

                            stop();
                            var distance:Number = 10;
                            var keyListener:Object = new Object();
                            keyListener.onKeyDown = function() {
                            if (Key.isDown(Key.LEFT)) {
                            flyinfox_mc._x = Math.max(flyinfox_mc._x-distance, 0);
                            trace("move fox left");
                            } else if (Key.isDown(Key.RIGHT)) {
                            flyinfox_mc._x = Math.min(flyinfox_mc._x+distance, 650);
                            trace("move fox right");
                            } else if (Key.isDown(Key.UP)) {
                            flyinfox_mc._y = Math.max(flyinfox_mc._y-distance, 0);
                            trace("move fox up");
                            } else if (Key.isDown(Key.DOWN)) {
                            flyinfox_mc._y = Math.min(flyinfox_mc._y+distance, 600);
                            trace("move fox down");
                            }
                            };
                            Key.addListener(keyListener);
                            //Program to generate a set number of ducks
                            //Declaring Variables
                            var numOfDucks:Number;
                            var movieWidth:Number;
                            var movieHeight:Number;
                            var counter:Number;
                            //setting variables values for ducks
                            numOfDucks = 6
                            movieWidth = Stage.width;
                            movieHeight = Stage.height;
                            //Program to generate a set number of traps
                            //Declaring Variables
                                for (counter = 1; counter <= 6; counter++)
                            {
                            //Place 6 ducks on the stage
                            _root.attachMovie("duck","duck"+ counter, counter);
                            //giving each duck unique characteristics
                            _root["duck"+counter]._x = movieWidth * Math.random();
                            _root["duck"+counter]._y = movieHeight * Math.random();
                            }
                            //Program to generate a set number of traps
                            //Declaring Variables
                            var numOfTraps:Number;
                            var movieWidth:Number;
                            var movieHeight:Number;
                            var counter:Number;
                            //setting variables values for traps
                            numOfTraps = 6
                            movieWidth = Stage.width;
                            movieHeight = Stage.height;
                            //Program to generate a set number of traps
                            //Declaring Variables
                                for (counter = 7; counter <= 13; counter++)
                            {
                            //Place 6 traps on the stage
                            _root.attachMovie("trap","trap"+ counter, counter);
                            //giving each duck unique characteristics
                            _root["trap"+counter]._x = movieWidth * Math.random();
                            _root["trap"+counter]._y = movieHeight * Math.random();
                            }
                            for(var i=0;i<foxTrapArray.length;i++){
                            if(flyinfox_mc.hitTest(foxTrapArray[i])){
                            delete this.onEnterFrame
                            trace("Hello World");
                            }
                            if(flyinfox_mc.hitTest(duck_mc)){
                            delete this.onEnterFrame
                            flyinfox_mc.onRollOver = function () {
                            trace("Hello World");
                            };
                            }
                            }