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!
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());
}
}
}
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.
North America
Europe, Middle East and Africa
Asia Pacific