9 Replies Latest reply on Oct 14, 2010 6:19 PM by Rothrock

    Variable problem (Example .fla/swf inside)

    JamesCun

      I have a plane movieclip which chooses a plane sprite randomly. When you drop a bomb the bomb should choose the sprite fitting the plane that it was dropped by. (Plane 1 drops bomb 1, plane 2 drops bomb 2)

       

      This works if I have one plane already on the stage, and no others. However, if I attach more plane movieclips it drops only bombs with the same sprite. (plane 1 and plane 2 only drop bomb 1)

       

      Here is an example fla/swf:
      http://dl.dropbox.com/u/6414392/plane%20example.zip

       

      I commented the code a little, its not much. If you delete the controller movie clip and put the plane on the stage then it will work. Use it like it is and it doesn't.

       

      This is driving me nuts, but I am probably only missing one little thing that makes it not work when attaching movieclips.

        • 1. Re: Variable problem (Example .fla/swf inside)
          Rothrock Level 5

          I (and many other folks) don't download FLAs from the forums. So if you can pull out the relevant bits of code and post those then it would be easier.

           

          My guess is that if you are using attachMovie you are using some kind of variable to say which linkageID should be attached and that you are not properly defining that variable for each of the clips that needs to be attached...

          • 2. Re: Variable problem (Example .fla/swf inside)
            JamesCun Level 1

            Alright, I just put it there to avoid that I start to ramble and make a giant first post. Here is the code from the plane:

             

            class Plane extends MovieClip
            {
            var bomb_type;

                  function onLoad()

            {
            //choose random frame (plane image)
            bomb_type = Math.floor(Math.random()*2+1);
            //Go to the frame
            gotoAndStop(bomb_type)
            }
            function onEnterFrame()
            {
            _x-=4;
            if (Key.isDown(Key.DOWN))
            {
            var bomb = _root.attachMovie( "bomb" , "bomb" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
            bomb._x = _x-20;
            bomb._y = _y+20;         
            }


            }

             

            }

             

            Here is the code for the bomb:

            class bomb extends MovieClip
            {
            var gravity;

            function onLoad()
            {
            //Change bomb picture to the same number that is bomb_type (which is set in the Plane Movieclip)
            gotoAndStop(_root.plane.bomb_type)


            gravity = Math.random()* 4+ 2;
            }

            function onEnterFrame()
            {
            _y+=gravity
            _x-=2
            _rotation-=gravity

            }
            }

             

             

            Here is the "controller" that spawns the planes:

             

             

            class controller extends MovieClip
            {
            var enemyTimer    
            function onLoad()
            {
            enemyTimer=0;
            }
            function onEnterFrame()
            {

            enemyTimer+=1;
            //spawn planes when timer is over 80 and reset
            if(enemyTimer > 80)
            {
            enemyTimer = 0;
            var plane = _root.attachMovie("plane", "plane"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
            plane._x=400;
            plane._y=200;
            }
            }

             

            }

            • 3. Re: Variable problem (Example .fla/swf inside)
              Rothrock Level 5

              Well yeah, I see several problems...

               

              bomb_type = Math.floor(Math.random()*2+1);

               

              That is inside your Plan class. That seems like it should actually be called plane_type because it is setting the frame of your plane. Or perhaps bomber_type. Subtle distinction, but when you come back in 6 months to update everything having good names will make a difference.

               

              I see you have declared bomb_type as instance variable in the plane class, so each instance of that class will have its own randomly determined type.

               

              But what I don't see is anyway for each instance of the Bomb class to know which instance of "plane" spawned it. So they are all trying to get their types from:

               

              _root.plane.bomb_type

               

              Which shouldn't even exist because your controller is giving the planes names like this:

               

              "plane"+_root.getNextHighestDepth()

               

              that is to say

               

              plane137

               

              So I'm not sure how it works with even 1 instance of a plane.

               

              What you need to do is pass the type or the instance of the plane that generated it to the bomb when it is attached. That way it knows.

              1 person found this helpful
              • 4. Re: Variable problem (Example .fla/swf inside)
                JamesCun Level 1

                I kinda expected that its that. It does work with one, since I don't "attach" it, but simply put it on the stage from the start, so it doesn't get the plane+depth name I think.

                 

                Could you help me out with the actual "vocabulary" for this? I'm new to actionscript so sometimes I need a little longer to wrap my head around things. I already had a hell of a time figuring out how to pass variables between movieclips and nested movieclips. In the end I got it to work, but with this, I can't imagine how I can pass on the "on the fly" generated name.

                 

                And to the bomb_type thing you mentioned: Of course you are right. I just edited that one quickly because first I used "type" as the name, but since it turned blue when I tried to refer it I thought it might not work because its some built in functionality and added "bomb" to it. I will get rid of that.

                • 5. Re: Variable problem (Example .fla/swf inside)
                  Rothrock Level 5

                  Ah that would explain how one works: luck.

                   

                  It has been awhile since I used AS2 and attachMovie with instances of a class that extended MovieClip. And I think there are some tricks to that, so I'm not sure if what I'm telling you is going to be the best approach. But it is worth a try.

                   

                  For your Bomb class have a public bombType variable and in your Bomb constructor have a line like:

                   

                  gotoAndStop(bombType);

                   

                  Then when you attachMovie you can use the init paramter,

                   

                  this.attachMovie("Bomb","bomb"+i,i,{bombType:planeType});

                   

                  Also I would avoid using getNextHighestDepth() on the _root level. If you have components in your library you will get numbers you didn't expect. It would be better to have all this contained inside some kind of movieclip.

                  • 6. Re: Variable problem (Example .fla/swf inside)
                    JamesCun Level 1

                    Ok I tried that but I don't quite understand what your attachcode does. I get "Plane.as Line 17 - There is no property with the name "i"" when I just slap it in there. I changed the "bomb_type" variable in the plane.as as you said and named it "plane_type". The code now looks like this for the plane:

                     

                    class Plane extends MovieClip
                    {
                         var plane_type;
                         
                         function onLoad()
                         {
                         //choose random frame (plane image)
                         plane_type = Math.floor(Math.random()*2+1);
                         //Go to the frame
                         gotoAndStop(plane_type)
                         }
                              function onEnterFrame()
                              {
                                   _x-=4;
                                   if (Key.isDown(Key.DOWN))
                              {
                              var bomb = this.attachMovie("Bomb","bomb"+i,i,{bombType:planeType});
                              bomb._x = _x-20;
                              bomb._y = _y+20;          
                    }

                    }

                     

                    }

                    The bomb.as thing I modified and put "gotoAndStop(bombType)" in the function onLoad() as you said.

                     

                    class bomb extends MovieClip
                    {
                         var gravity;
                         public var bombType
                         function onLoad()
                         {
                         //Change bomb picture to the same number that is bomb_type (which is set in the Plane Movieclip)

                     

                         gotoAndStop(bombType);
                         
                         
                         gravity = Math.random()* 4+ 2;
                         }
                         
                         function onEnterFrame()
                         {
                         _y+=gravity
                         _x-=2
                         _rotation-=gravity
                         
                         }
                    }

                    Thank you for helping, I will be very happy if you can help me do this, I ran into situations where I could use something like this several times already, I would love to finally learn it already.

                    • 7. Re: Variable problem (Example .fla/swf inside)
                      Rothrock Level 5

                      Sorry I was going to explain the different ways that I would handle it, but it was late and I messed it all up.

                       

                      Replace this with _root and replace i with _root.getNextHightestDepth();

                       

                      That should fix it up for you.

                       

                      I would still think about creating a movie to hold all the action and then either passing that as a value to the plane and bomb instances so they could attach themselves to that movie.

                      1 person found this helpful
                      • 8. Re: Variable problem (Example .fla/swf inside)
                        JamesCun Level 1

                        Now only the first appearing plane drops a bomb, the others don't drop anything at all.


                        Couldn't you take a look at the .fla? Or simply the .as files I attached? Because right now, I'm not sure if I just put it there wrong. Its probably harder to see whats wrong without taking a look at it too.

                        The code now looks like this:

                        var bomb = _root.attachMovie("Bomb","bomb"+_root.getNextHightestDepth(),_root.getNextHightestDepth() ,{bombType:plane_type});

                        I don't know what to do. I don't get how to tell the bomb from which plane it came. All the other problems I encountered so far, I could wrap my head around and learned from it. But this is a tough nut to crack.

                         

                        Edit: I'm really sorry to be so thick and daft. I hope you don't give up on me, I really want to get this!

                         

                        Edit2: Ok I tried around with it, and now this happens: (its the .swf, no need to download)

                        http://dl.dropbox.com/u/6414392/Plane_Example.swf


                        This is the code I used:

                         

                        Plane.as:

                        class Plane extends MovieClip
                        {
                             var plane_type;
                            
                             function onLoad()
                             {
                             //choose random frame (plane image)
                             plane_type = Math.floor(Math.random()*2+1);
                             //Go to the frame
                             gotoAndStop(plane_type)
                             }
                                  function onEnterFrame()
                                  {
                                       _x-=4;
                                       if (Key.isDown(Key.DOWN))
                                  {
                                  var bomb = _root.attachMovie("Bomb","bomb"+_root.getNextHightestDepth(),_root.getNextHightestDepth() ,{bombType:plane_type});
                                  bomb._x = _x-20;
                                  bomb._y = _y+20;         
                                  }
                                 
                                 
                                  }

                        }

                        Bomb.as

                        class bomb extends MovieClip
                        {
                             var gravity;
                              var bombType
                             function onLoad()
                             {
                             //Change bomb picture to the same number that is bomb_type (which is set in the Plane Movieclip)
                            

                             gotoAndStop(bombType);
                            
                            
                             gravity = Math.random()* 4+ 2;
                             }
                            
                             function onEnterFrame()
                             {
                             _y+=gravity
                             _x-=2
                             _rotation-=gravity
                            
                             }
                        }

                        This is so frustrating, I've wasted hours on this.

                        • 9. Re: Variable problem (Example .fla/swf inside)
                          Rothrock Level 5

                          That is strange. I'm not getting any bombs at all.

                           

                          I think that it is time to add some traces to see what is going on.

                           

                          I don't know what the onLoad() event for those things is, but you can add a trace statement to it. Something like:

                           

                          class Plane extends MovieClip
                          {
                               var plane_type;
                              
                               function onLoad() {
                                    //choose random frame (plane image)
                                    plane_type = Math.floor(Math.random()*2+1);
                                    //Go to the frame
                                    gotoAndStop(plane_type)

                                    trace("Plane "+this._name+" has loaded and gone to "+plane_type);
                               }

                               function onEnterFrame(){
                                         _x-=4;
                                         if (Key.isDown(Key.DOWN))   {

                                              var bomb = _root.attachMovie("Bomb","bomb"+_root.getNextHightestDepth(),_root.ge tNextHightestDepth(),{bombType:plane_type});

                                              trace("   Plane "+this._name+" has dropped bomb "+bomb._name);
                                              bomb._x = _x-20;
                                              bomb._y = _y+20;         
                                         }     
                               }

                          }

                           

                           

                          class bomb extends MovieClip
                          {
                               var gravity;
                               var bombType;

                               function bomb(){

                                    trace("Constructor function for bomb class is called and type is: "+bombType);

                               }


                               function onLoad()
                               {
                               //Change bomb picture to the same number that is bomb_type (which is set in the Plane Movieclip)

                           

                               gotoAndStop(bombType);
                               trace("Bomb "+this._name+" has been loaded and is of type: "+bombType);
                              
                               gravity = Math.random()* 4+ 2;
                               }
                              
                               function onEnterFrame()
                               {
                               _y+=gravity
                               _x-=2
                               _rotation-=gravity
                              
                               }
                          }

                           

                          So what do those traces reveal to you?