Add movieclips within another movieclip - actionscript-3

I am having trouble embedding a child object to a movieClip with code using AS3 and I am not sure what I am doing wrong.
I am trying to create a new movieclip with code and add it to the stage. I am then trying to embed dynamically created objects inside that movieclip. When I trace the objects it shows that they are on the stage and not embedded into the movieclip. I looked through the forums but I didn't find an answer. Below is a watered down version of the code I have. Any help is appreciated.
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite;
import flash.text.*;
import flash.net.URLRequest;
import flash.display.Loader;
var btnAImage:Loader = new Loader();
var image:URLRequest = new URLRequest("btn_A.png");
public function TextWithImage()
{
var TextField1:TextField = new TextField;
var myText1:String = "TEXT FIELD 1";
var BtnMovieClip:MovieClip = new MovieClip();
var btnPadding = 5;
addChild(BtnMovieClip); //add new MC to stage
btnAImage.load(image);
BtnMovieClip.addChild(btnAImage); // no error gets thrown. I am trying to add the btnAImage inside of my BtnMovieClip.
btnAImage.x = btnPadding; //I am able to reference the btnAImage without referencing the BtnMovieClip object.
btnAImage.y = btnPadding;
var screenW = stage.stageWidth;
TextField1.height = 40;
TextField1.width = 250;
var textPadding = 5;
var TextField1_fontFormat:TextFormat = new TextFormat ;
TextField1_fontFormat.size = 40;
TextField1_fontFormat.font = "TestFont";
TextField1.defaultTextFormat = TextField1_fontFormat;
BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
TextField1.text = myText1;
TextField1.x = (btnAImage.width + textPadding);
TextField1.y = textPadding;
}

I wasn't preloading my image so here's how I fixed it. Thank you to Organis for the help!!!
public var btnAImage:Loader;
private function Question8_Answer()
{
trace("Question8_Answer() was called");
btnAImage = new Loader();
btnAImage.load(new URLRequest("btn_A.png"));
btnAImage.contentLoaderInfo.addEventListener(Event.COMPLETE, TextWithImage);
}
public function TextWithImage()
{
var TextField1:TextField = new TextField;
var myText1:String = "TEXT FIELD 1";
var BtnContainer:Sprite = new Sprite();
var btnPadding = 5;
addChild(BtnMovieClip);
BtnMovieClip.addChild(btnAImage);
btnAImage.x = btnPadding;
btnAImage.y = btnPadding;
var screenW = stage.stageWidth;
TextField1.height = 40;
TextField1.width = 250;
var textPadding = 5;
var TextField1_fontFormat:TextFormat = new TextFormat ;
TextField1_fontFormat.size = 40;
TextField1_fontFormat.font = "TestFont";
TextField1.defaultTextFormat = TextField1_fontFormat;
BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
TextField1.text = myText1;
TextField1.x = (btnAImage.width + textPadding);
TextField1.y = textPadding;
BtnContainer.width = 300;
}

Related

TypeError: Error #1006: addChild is not a function

I'm trying to embed a "pre-made" flash actionscript 3 gallery into my own flash animation. Both animations work in good ways independently. But once I insert the photo gallery into my own project I am getting an error of;
TypeError: Error #1006: addChild is not a function.
at project_fla::galeri_6/onCompleteXmlLoad()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
My flash button code which leads to gallery is;
stop();
galeri_btn.addEventListener(MouseEvent.CLICK, galeri);
function galeri(event:MouseEvent):void
{
gotoAndStop(3);
}
And lastly, the flash gallery code is;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
var xmlLoader:URLLoader = new URLLoader(new URLRequest("photo_gallery.xml"));
xmlLoader.addEventListener(Event.COMPLETE, onCompleteXmlLoad);
var xmlFile:XML;
var xcoord:int = 10;
var gal:gallery = new gallery();
gal.x = 10;
gal.y = 10;
addChild(gal);
var thumbsContainer:Sprite = new Sprite();
thumbsContainer.x = 10;
thumbsContainer.y = 320;
addChild(thumbsContainer);
var txtTitle:TextField = new TextField();
txtTitle.x = 15;
txtTitle.y = 15;
var format:TextFormat = new TextFormat();
format.bold = true;
format.color = 0xFFFFFF;
format.size = "20";
format.font = "Arial";
txtTitle.defaultTextFormat = format;
addChild(txtTitle);
var txtDesc:TextField = new TextField();
txtDesc.x = 15;
txtDesc.y = 40;
var format1:TextFormat = new TextFormat();
format1.color = 0x000000;
format.font = "Calibri";
format.size = 12;
txtDesc.defaultTextFormat = format1;
addChild(txtDesc);
function onCompleteXmlLoad(e:Event):void{
xmlFile = new XML(xmlLoader.data);
var len:int = xmlFile.photo.length();
txtTitle.text = xmlFile.photo.name[0];
txtDesc.text = xmlFile.photo.desc[0];
for(var i:int = 0;i<len;i++){
var t:thumbs = new thumbs();
t.x = xcoord;
t.y = 10;
t.buttonMode = true;
t.name = (i+1).toString();
thumbsContainer.addChild(t);
t.addEventListener(MouseEvent.MOUSE_OVER, onMouseover);
t.addEventListener(MouseEvent.MOUSE_OUT, onMouseout);
t.addEventListener(MouseEvent.CLICK, onMouseClick);
var tloader:Loader = new Loader();
tloader.load(new URLRequest("thumbs/" + (i+1) + ".jpg"));
t.addChild(tloader);
xcoord += t.width + 10;
}
var loader:Loader = new Loader();
loader.load(new URLRequest("img/1.jpg"));
gal.addChild(loader);
scroller.source = thumbsContainer;
scroller.setSize(550,110);
}
function onMouseover(e:MouseEvent):void{
e.currentTarget.alpha = 0.5;
}
function onMouseout(e:MouseEvent):void{
e.currentTarget.alpha = 1.0;
}
function onMouseClick(e:MouseEvent):void{
var loader:Loader = new Loader();
loader.load(new URLRequest("img/" + e.currentTarget.name + ".jpg"));
gal.addChild(loader);
txtTitle.text = xmlFile.photo.name[int(e.currentTarget.name) - 1];
txtDesc.text = xmlFile.photo.desc[int(e.currentTarget.name) - 1];
}
Any help is appreciated.
The problem is within one of those two lines:
t.addChild(tloader);
or
gal.addChild(loader);
And the reason for this is because of addChild is not a function error message. If you were calling addChild on non-existing object, the error would be something else.
t is of type thumbs - var t:thumbs = new thumbs(); and gal is gallery - var gal:gallery = new gallery();
So either one of those classes (thumbs or gallery) is problematic. They should do one of the following (inside the classes):
extend DisplayObjectContainer - Sprite or even MovieClip are safest
provide addChild kind of function

Flash-Adding Symbol to Stage in AS3

I am trying to add a symbol from my library to the stage. For some reason, when I run the code, no objects are appearing on the stage but I am not getting any errors either. I can't provide all of the code because its VERY long, but I'll provide enough for anyone reading to get a good idea of what I'm trying to do.
Basically, the main as3 file starts like this:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.media.*;
import flash.net.*;
public class NewFrogMod extends MovieClip
{
var oneHit:Boolean;
var twoHit:Boolean;
var threeHit:Boolean;
var fourHit:Boolean;
var fiveHit:Boolean;
var sixHit:Boolean;
var sevenHit:Boolean;
var eightHit:Boolean;
var nineHit:Boolean;
var tenHit:Boolean;
var elevenHit:Boolean;
var twelveHit:Boolean;
var thirteenHit:Boolean;
var fourteenHit:Boolean;
var score:uint;
var frog1:Frog;
var truck1:Truck;
var truck2:Truck;
var truck3:Truck;
var car1:Car;
var car2:Car;
var log1:Logs;
var log2:Logs;
var log3:Logs;
var log4:Logs;
var turtle1:Turtles;
var turtle2:Turtles;
var z1:Zfrog;
var z2:Zfrog;
var z3:Zfrog;
var z4:Zfrog;
var z5:Zfrog;
public function NewFrogMod()
{
var score = 0;
frog1 = new Frog();
truck1 = new Truck();
truck2= new Truck();
truck3 = new Truck();
car1 = new Car();
car2 = new Car();
log1 = new Logs();
log2 = new Logs();
log3 = new Logs();
log4 = new Logs();
turtle1 = new Turtles();
turtle2 = new Turtles();
z1 = new Zfrog();
z2 = new Zfrog();
z3 = new Zfrog();
z4 = new Zfrog();
z5 = new Zfrog();
addChild(frog1);
addChild(truck1);
addChild(truck2);
addChild(car1);
addChild(car2);
addChild(log1);
addChild(log2);
addChild(log3);
addChild(turtle1);
addChild(turtle2);
addChild(z1);
addChild(z2);
addChild(z3);
addChild(z4);
addChild(z5);
frog1.x = 238;
frog1.y = 373;
truck1.x = 0;
truck1.y = 252;
truck2.x = 205;
truck2.y = 252;
car1.x = 82;
car1.y = 175;
car2.x = 363;
car2.y = 175;
log1.x = 22;
log1.y = 51;
log2.x = 355;
log2.y = 51;
log3.x = 43;
log3.y = 102;
log4.x = 292;
log4.y = 102;
turtle1.x = 241;
turtle1.y = 81;
turtle2.x = 508;
turtle2.y = 125;
z1.x = 200;
z1.y = 250;
z2.x = 300;
z2.y = 350;
z3.x = 100;
z3.y = 150;
z4.x = 100;
z4.y = 250;
z5.x = 200;
z5.y = 150;
Then there's a bunch more after that....
Now, to give you an idea of how I'm setting up the objects, here is the code for some of them:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Car extends NewFrogMod{
public function Car()
{
addEventListener(Event.ENTER_FRAME, Enter4);
}
function Enter4(event:Event):void
{
this.x += 3;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Truck extends NewFrogMod{
public function Truck()
{
addEventListener(Event.ENTER_FRAME, Enter5);
}
function Enter5(event:Event):void {
if(this.hitTestObject(frog1))
{
health1.width -= 5;
}
}
}
}
I've tried extending MovieClip with these files as well and that doesn't work either. Any hep is GREATLY aqppreciated. Thank you!
Ensure that the parent class NewFrogMod which adds the movieclip, has itself been added to the stage.
Also ensure that each library symbol, eg. Truck, has been linked to its class:
Right-click the symbol in the library and choose Properties from the menu.
Check the box 'Export for ActionScript'.
In the Class field, enter the full class path of your Truck class (simply 'Truck' in your case). The base field class can be ignored as your Truck class already extends MovieClip.
Alternatively, specify NewFrogMod as the document class of the main timeline:
Left-click an empty part of the stage, go to the Properties inspector and enter NewFrogMod into the Class field.
i think it may be that you added the objects before you specified the location. try moving the addChild functions below the x and y coordinates.

AS3 Video Playback with FLVPlayback

I am using the code below to load some data from an .xml file.
I am preloading all data (Audio Paths, Video Paths including a video from xml.
When everything is loaded complete i am loading the video in Frame 2 on FLVPlayback 2.5 with this code:
videoPlayer.source = videofile;
The problem is that the video shows a white screen for 3-4 seconds and then starts play.
At some other pc's it plays normaly when the loading ends.
My Code:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
//******************************************************
// XML Loader
//******************************************************
var myLoader:URLLoader = new URLLoader();
//myLoader.load(new URLRequest("myxml.php"));
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void{
var myXml:XML = new XML(e.target.data);
parseXML(myXml);
}
//******************************************************
// Extract XML value and fill up variables
//******************************************************
var thename:XML;
var soundpath:XML;
var theage:XML;
var theplace:XML;
var everyday:XML;
var youwill:XML;
var pic1:XML;
var pic2:XML;
var pic3:XML;
var videofile:XML;
var assetsList:Array;
//-----------------------------
var sound:Sound;
var soundChannel:SoundChannel;
//-----------------------------
function parseXML(xml:XML):void{
thename = xml.paths.thename[0];
soundpath = xml.paths.soundpath[0];
theage = xml.paths.theage[0];
theplace = xml.paths.theplace[0];
everyday = xml.paths.everyday[0];
youwill = xml.paths.youwill[0];
pic1 = xml.paths.pic1[0];
pic2 = xml.paths.pic2[0];
pic3 = xml.paths.pic3[0];
videofile = xml.paths.videofile[0];
txtThename.text = thename;
txtSoundpath.text = soundpath;
txtTheage.text = theage;
txtTheplace.text = theplace;
txtEveryday.text = everyday;
txtYouwill.text = youwill;
txtPic1.text = pic1;
txtPic2.text = pic2;
txtPic3.text = pic3;
txtVideofile.text = videofile;
assetsList = [soundpath,theage,theplace,everyday,youwill,pic1,pic2,pic3,videofile];
preloadAssets();
}
//******************************************************
// preloaded assets
//******************************************************
var assetsLoader:URLLoader
var assetsCtr:Number=0;
function preloadAssets():void{
assetsLoader = new URLLoader ();
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
assetsLoader.addEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.addEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
}
function assetProgressHandler(evt:ProgressEvent):void{
var bl:uint = evt.bytesLoaded;
var bt:uint = evt.bytesTotal;
var perEachAssets = 1/assetsList.length;
var assetsBlLoaded = ((bl / bt)*perEachAssets)+((assetsCtr)/assetsList.length*100)/100;
var _percentLoaded = Math.floor(assetsBlLoaded*100);
progBar.setProgress(_percentLoaded,100)
//trace("_percentLoaded:",_percentLoaded)
}
function assetLoadedHanlder(evt:Event):void{
assetsCtr+=1;
if(assetsCtr<assetsList.length){
//trace("preloading:"+assetsList[assetsCtr])
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
}else{
//trace("done!")
assetsLoader.removeEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.removeEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
gotoAndStop(2);
}
}
Rather than adding the component directly to the stage you might want to try creating and adding it with ActionScript.
By doing this you can instantiate the FLVPlayback instance before you need to show it, rather than having to wait until you hit frame 2 on your timeline.
I can't guarantee it will fix your problem but it's worth a go.
var _videoFLV:FLVPlayback;
_videoFLV = new FLVPlayback();
_videoFLV.fullScreenTakeOver = false;
_videoFLV.autoPlay = false;
_videoFLV.autoRewind = true;
_videoFLV.isLive = false;
_videoFLV.skin = null;
_videoFLV.bufferTime = .1;
_videoFLV.width = 320;
_videoFLV.height = 240;
_videoFLV.source = videofile;
_videoFLV.stop();
_videoFLV.x = 240;
_videoFLV.y = 240;
addChild(_videoFLV);

Actionscript 3 - DataGridColumn and having a specific column automatically sorted

I am using the DataGrid component in Flash, that is loaded with data via a external XML file. I have a column, A (Serial), which once loaded, I'd like for the information to be sorted Ascendingly, automatically. Does anyone have any idea on how to do this?
Here is my code:
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
import fl.events.DataGridEvent;
var dp:DataProvider;
var A:DataGridColumn = new DataGridColumn("Serial");
A.headerText = "Serial No.";
A.width = 100;
A.resizable = false;
var B:DataGridColumn = new DataGridColumn("Mold");
B.headerText = "Mold No.";
B.width = 150;
B.resizable = false;
var C:DataGridColumn = new DataGridColumn("Type");
C.headerText = "Grid Type: ";
C.width = 350;
C.resizable = false;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(A);
myDataGrid.addColumn(B);
myDataGrid.addColumn(C);
myDataGrid.verticalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(600, 800);
myDataGrid.move(0, 0);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
var url:String = "xml/TEST.xml";
var req:URLRequest = new URLRequest(url);
var uLdr:URLLoader = new URLLoader();
uLdr.addEventListener(Event.COMPLETE, completeHandler);
uLdr.load(req);
function completeHandler(event:Event):void {
var ldr:URLLoader = event.currentTarget as URLLoader;
var xmlDP:XML = new XML(ldr.data);
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
}
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
Thanx
Try the sortItemsOn() method on the DataGrid.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/SelectableList.html#sortItemsOn() for an example how to use it.

How to create a light effect inside a rectangle in Actionscript 3?

I have tried to do this here http://wonderfl.net/c/9Kdv but what I want is not this
alt text http://reboltutorial.com/images/flash-banner-trial.png
but rather the equivalent of this. As I'm flash newbie I don't see how:
(source: reboltutorial.com)
action script 3 code below:
package {
import flash.display.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.filters.*;
import flash.geom.Rectangle;
public class FlashTest extends Sprite {
public function FlashTest() {
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0x400000);
mc.graphics.drawRoundRect(0, 0, 278, 170,25,25);
mc.graphics.endFill();
mc.x = 80;
mc.y = 60;
addChild(mc);
//from tut http://blog.0tutor.com/post.aspx?id=116
var filt:GlowFilter = new GlowFilter();
var filt_shadow:DropShadowFilter = new DropShadowFilter();
//here we add some properties to the two filters, the glow filter we give a color.
filt.color = 0xFF0000;
//and how much it should blur.
filt.blurX = 7;
filt.blurY = 7;
//then the dropshadow filter, also how much it should blur on the object.
filt_shadow.blurX = 4;
filt_shadow.blurY = 4;
//and finally an alpha, the alpha goes from 1 to 0, 1 being fully visible and 0 is transparent, then of cause .5 is just in between.
filt_shadow.alpha = .4;
mc.filters = [filt,filt_shadow];
var theTextField:TextField = new TextField();
theTextField.border = false;
theTextField.x = 30;
theTextField.y = 50;
theTextField.autoSize = TextFieldAutoSize.LEFT;
theTextField.text = "Experiment";
var myformat:TextFormat = new TextFormat();
myformat.color = 0xFFFFFF;
myformat.size =24;
myformat.align="center";
myformat.font = "Impact";
theTextField.setTextFormat(myformat);
mc.addChild(theTextField);
var url:String = "//www.rebol.com/graphics/reb-logo.gif";
var urlReq:URLRequest = new URLRequest(url);
var ldr:Loader = new Loader();
ldr.load(urlReq);
ldr.x=30;
ldr.y=88;
mc.addChild(ldr);
}
}
}
Instead of this line:
mc.graphics.beginFill(0x400000);
you can use beginGradientFill with the fillType set to GradientType.RADIAL. You would just need to adjust the focalPointRatio to make it offcenter. Check out the example in the docs for how to do this.