Skip navigation
ZXZ1661
Currently Being Moderated

How come my MovieClip isn't working?

Sep 15, 2012 12:22 PM

Tags: #script #actionscript #movieclip #actionscript3

What I'm trying to do is create a series of MovieClips into my flash project (via actionscript) to create a scrolling background. But I'vev run into a strange issue.

For some reason, although I think I am adding the movieclips correctly to the stage, all the variables are set to zero.

Here is the code in my main timeline I am using to create the MovieClips (GrassTile)

 

[code]

var grassHolder:Sprite = new Sprite();

addChild(grassHolder)

for(var i:int = 0;i < 5; i++)

{

    grassHolder.addChildAt(new GrassTile(i), i);

}

[/code] (and, if you know a working BBCode to show code, it would also help )

 

And here is my GrassTile class: I cut out the unimportant stuff. I can add it in later if it is needed.

[code]

package  {

    import flash.display.Bitmap;

    import flash.display.MovieClip;

    import flash.events.Event;

   

    public class GrassTile extends MovieClip

    {

        private var _root:MovieClip

        private var grass:Bitmap;

        public var id:int;

        public function GrassTile(id:int)

        {

            this.x = 100

            this.id = id;

            addEventListener(Event.ADDED_TO_STAGE, onEnterStage);

        }

       

        public function updateGrassTiles():void //called from the main timeline

        {

            this.x = (0 % this.width) + (this.width * (id));

            this.y = stage.stageHeight - this.height;

            trace(this.width);

            trace(this.x); //These traces return only "0" unless it was the last one created

        }

       

        private function onEnterStage(e:Event):void

        {

            removeEventListener(Event.ADDED_TO_STAGE, onEnterStage);

            _root = MovieClip(root);

            grass = _root._resource.GrassImage;

            addChild(grass);

        }

 

    }

   

}

[/code]

As I stated in the code comments, when the GrassTiles are added, every one except the last one added returns 0 for its x and width, and doesn't add the Bitmap image.

 

Thanks in advance!

 
Replies
  • Currently Being Moderated
    Sep 15, 2012 12:42 PM   in reply to ZXZ1661

    What is the main timeline code that is calling the updateGrassTiles function ?

     
    |
    Mark as:
  • kglad
    62,197 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 16, 2012 12:22 PM   in reply to ZXZ1661

    use the trace function to see what id you're passing and your GrassTile width.

     

    p.s.  0%anything is always 0 so you can remove that part of your coe.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 16, 2012 1:27 PM   in reply to ZXZ1661

    If you use the same instance of _resource.GrassImage - it is moved from one instance of GrassTile to another. It doesn't make copies. Thus - you see it only in one (last) GrassTile.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 16, 2012 1:36 PM   in reply to ZXZ1661

    Try this - it clones (should clone - I did not test it) bitmap

     

     

    package
    {
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.MovieClip;
        import flash.events.Event;
        
        public class GrassTile extends MovieClip
        {
            private var _root:MovieClip
            private var grass:Bitmap;
            public var id:int;
            
            public function GrassTile(id:int)
            {
                this.x = 100
                this.id = id;
                addEventListener(Event.ADDED_TO_STAGE, onEnterStage);
            
            }
            
            public function updateGrassTiles():void //called from the main timeline
            {
                this.x = (0 % this.width) + (this.width * (id));
                this.y = stage.stageHeight - this.height;
                trace(this.width);
                trace(this.x); //These traces return only "0" unless it was the last one created
            }
            
            private function onEnterStage(e:Event):void
            {
                removeEventListener(Event.ADDED_TO_STAGE, onEnterStage);
                _root = MovieClip(root);
                grass = cloneBitmap(_root._resource.GrassImage);
                addChild(grass);
            }
            
            private function cloneBitmap(bitmap:Bitmap):Bitmap
            {
                return new Bitmap(bitmap.bitmapData.clone());
            }
        }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 16, 2012 4:43 PM   in reply to ZXZ1661

    You are welcome.

     

    There are two types of objects. One type can be used by reference; another - by value.

     

    Instances of Bitmap (and other display objects as well as the majority of AS3 classes) are used by reference. This means that there is no duplicate created.

     

    Another subtle thing that you may already know - addChild() method of DisplayObjectContainer deals with a single DisplayObject (duplicates are not created) instance. This, in particular, means that if a DisplayObject  D is on display list A and one adds this D to display list B via B.addChild(D) - D is automatically removed from the display list A although there is no explicit code that states this removal.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points