Copy link to clipboard
Copied
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!
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
Copy link to clipboard
Copied
What is the main timeline code that is calling the updateGrassTiles function ?
Copy link to clipboard
Copied
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(); | ||
} |
Copy link to clipboard
Copied
I don't mean to sound rude, but I really need help on this. If anybody has anything that might help, please tell me.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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());
}
}
}
Copy link to clipboard
Copied
Thanks! I will test as soon as possible. I always thought (for some reason) that when you assigned a variable to an already existed one it "duplicated" it as you mentioned above. I will test out your code now
Copy link to clipboard
Copied
Worked like a charm! Many thanks.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.