Skip to main content
Known Participant
September 15, 2012
Answered

How come my MovieClip isn't working?

  • September 15, 2012
  • 2 replies
  • 1735 views

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)

var grassHolder:Sprite = new Sprite();

addChild(grassHolder)

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

{

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

}

(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.

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);

        }

    }

   

}

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!

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Thanks for the help, kglad.  I used the trace function, and the id is correct for each tile. And (Sorry I didn't mention earlier) the 0 % is just for testing purposes. Later it will be replaced with the scroll value of the camera.


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());

        }

    }

}

2 replies

Inspiring
September 16, 2012

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.

Ned Murphy
Legend
September 15, 2012

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

ZXZ1661Author
Known Participant
September 15, 2012

It's within an "ENTER_FRAME" event handler.

EDIT

This is the code I am using to update them:

for(var i:int = 0;i < 5; i++)
{
GrassTile(grassHolder.getChildAt(i)).updateGrassTiles();
}

ZXZ1661Author
Known Participant
September 16, 2012

I don't mean to sound rude, but I really need help on this. If anybody has anything that might help, please tell me.