AS3: Accessing a dynamicaly added child from another dynamicaly added child - actionscript-3

So heres my question, I have a button on the stage that adds a MC called "fadeL" and "PDF1"
Quick note: "fadeL" and "PDF1" are instances of "fadeMC" and "PDFwindow" heres the code for them:
var fadeL:fadeMC = new fadeMC();
this.addChild(fadeL);
fadeL.x = 0;
fadeL.y = 0;
var PDF1:PDFwindow = new PDFwindow();
this.addChild(PDF1);
PDF1.x = 30;
PDF1.y = 130;
Within PDF1 is another MC called "PDFviewer" which contains a button called closeBtn
Here is the actionscript for that button:
var container:DisplayObjectContainer = stage.getChildAt(0) as DisplayObjectContainer;
var mc:MovieClip = container.getChildByName("fadeL") as MovieClip;
mc.gotoAndStop(12);
So basicly Im trying to tell "fadeL" to start playing at frame 12 (which ultimately makes it fade off the screen)
but here is the error I get when closeBtn is pressed:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at NovartisTable_fla::PDFviewer_4/closeTap()[NovartisTable_fla.PDFviewer_4::frame1:9]
Any suggestions here? been looking around for other ways to tackle this but all have failed me
-Todd

You're actually grabbing a reference to fadeL when you called stage.getChildAt(0) (assuming the code you are showing is the only time you're adding items to the display list).
Change your button handler to this:
var mc:MovieClip = stage.getChildAt(0) as DisplayObjectContainer;
mc.gotoAndStop(12);
Failing that, I would give fadeL an actual name so you can do the following:
fadeL.name = "fadeL";
...
var mc:MovieClip = stage.getChildByName("fadeL") as MovieClip;
mc.gotoAndStop(12);

Related

How to make an instance move

I got the following code and the flash events are all loaded properly. ball is a movie clip and is assigned a class.
var speedx: Number=5;
var speedy: Number=3;
var myball = new ball();
addChild(myball);
addEventListener(Event:ENTER_FRAME,ballmove);
function ballmove(e:Event):void{
myball.x+=speedx;
myball.y+=speedy;
}
But now the instance myball won't move, it is just stuck at position 0,0. Kindly help me and advice how to get this myball to move along a staright line..
If I had dragged and dropped the instance from the library, then
myball.x+=speedx;
myball.y+=speedy;
worked perfectly, but doesn't work after addChild is given.
In your code you incorrectly used Event:ENTER_FRAME when it should be : Event.ENTER_FRAME. I wonder if there is a "silent" error? When testing movies, use Ctrl+Shift+Enter to run the debugger (gives better feedback on issues/errors).
var myball = new ball(); does not mean anything. You declare instance by using this logic : var Name : Type of Data = Value, In your code here you've said only that var Name = Value. Surprisingly it works but I wonder if getting comfortable with that could lead to issues on bigger future projects?
Instance name only affects objects already on Stage. If you're adding by code (from Library) make sure you create a new instance of object by using its Linkage name.
Solution :
In the library, right-click the "ball" object and choose "properties", in there make sure "name" is ball and tick "Export for ActionScript" (you should see "Class" becomeClass: ball). OK that.
Now in your code, you can create new instance by : var myball: ball = new ball();...
Your code should look like
var speedx: Number=5;
var speedy: Number=3;
var myball: ball = new ball();
addChild(myball);
addEventListener(Event.ENTER_FRAME, ballmove);
function ballmove(e:Event):void
{
myball.x += speedx;
myball.y += speedy;
}

AS3 - Referencing a MC nested inside another MC - there has to be an easy way?

The problem is simple. I have a "levels" frame that contains 1 MovieClip instance named "levelbuttons". Inside this MC are all of the level buttons instance named p1l1Btn, p1l2Btn, etc. (so I can reference all 45 buttons at once). However, I'm having trouble referencing each individual button - specifically to gotoAndStop to a different frame of the button.
From what I have read I should be able to reference the buttons with a line like levelbuttons.p1l1Btn.gotoAndStop(2); - however, this generates Error #1009: Cannot access a property or method of a null object reference.
Suggestions or ideas? Thanks!
Here's code that shows storing references to buttons in an array (store everything in arrays).
var aButtons: Array = new Array();
for (var i = 0; i < aButtons.length; i++) {
var mcNewButton: MyButton = new MyButton();
mcButtons.addChild(mcNewButton);
mcNewButton.x = i * 100;
mcNewButton.iButtonID = i;
mcNewButton.tMaintText.text = "Button " + i;
mcNewButton.addEventListener(MouseEvent.MOUSE_DOWN, fButtonPress);
aButtons.push(mcNewButton);
}
Then you can reference the buttons like this: aButtons[i]
levelbuttons.p1l1Btn.gotoAndStop(2); works after all...
Just have to call it after the buttons have been added to stage :facepalm:

Actionscript 3 how to duplicate a symbol onto the next frame

Im a total beginner at using AS3. I want to know is how does one duplicate a symbol and make it appear in the next frame.
example: when the user clicks on the symbol in that frame, the same symbol will appear in the next frame. If it is not possible then how does one move that symbol to the next frame.
Thank You in advance for answering.
You should probably avoid to use timeline keyframe to achieve this kind of thing. However, if you really need to do it this way, here what I would do :
First, make sure the MovieClip you want to clone has ActionScript linkage.
Then:
my_mc.addEventListener(MouseEvent.CLICK,onClick)
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
addChild(clone);
}
This will add the clone to the stage, so if you go back to frame 1, you'll see the clone. There's no way to add an object to a specific timeline frame. If you want to achieve such a thing, you have to target a container on frame 2 and add to clone to the container.
something like this :
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
myContainerOnframe2.addChild(clone);
}

Using a Numeric Stepper from one Flash file to affect gamespeed in an external Flash file? Actionscript 3.0

Currently I have an intro screen to my flash file which has two objects.
A button which will load an external flash file using:
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("flashgame.swf");
The second thing is a Numeric Stepper, which will be from 1 to 10. If the user selects a number e.g. 3 then the game speed I have set in the flashgame.swf should be changed
Such as:
var gameSpeed:uint = 10 * numericStepper.value;
But I think my problem is coming into place because the stepper and gamespeed are from different files.
Anyone got any idea please?
I have also tried creating a stepper in the game file and used this code:
var gameLevel:NumericStepper = new NumericStepper();
gameLevel.maximum = 10;
gameLevel.minimum = 1;
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
For some reason the stepper just flashes on the stage, no errors come up and the game doesn't work
When you execute you code, the stepper has no chance to wait for user input.
There is no time between theese two instructions.
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
You should wait for user input in your NumericStepper, and then, on user event, set the game speed.
Edit: Yeah I know it's kinda sad to type out all this code (especially since some people wouldn't even be grateful enough to say thanks) but I think this question is important enough to justify the code as it may be helpful to others in future also.
Hi,
You were close. In your game file you could have put a var _setgameSpeed and then from Intro you could adjust it by flashgame._setgameSpeed = gameSpeed; It's a bit more complicated though since you also have to setup a reference to flashgame in the first place. Let me explain...
Ideally you want to put all your code in one place (an .as file would be best but...) if you would rather use timeline then you should create a new empty layer called "actions" and put all your code in the first frame of that.
Also change your button to a movieClip type and remove any code within it since everything will be controlled by the code in "actions" layer. In the example I have that movieclip on the stage with instance name of "btn_load_SWF"
Intro.swf (Parent SWF file)
var my_Game_Swf:MovieClip; //reference name when accessing "flashgame.swf"
var _SWF_is_loaded:Boolean = false; //initial value
var set_gameSpeed:int; //temp value holder for speed
var swf_loader:Loader = new Loader();
btn_load_SWF.buttonMode = true; //instance name of movieclip used as "load" button
btn_load_SWF.addEventListener(MouseEvent.CLICK, load_Game_SWF);
function load_Game_SWF (event:MouseEvent) : void
{
//set_gameSpeed = 10 * numericStepper.value;
set_gameSpeed = 100; //manual set cos I dont have the above numericStepper
if ( _SWF_is_loaded == true)
{
stage.removeChild(swf_loader);
swf_loader.load ( new URLRequest ("flashgame.swf") );
}
else
{ swf_loader.load ( new URLRequest ("flashgame.swf") ); }
swf_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, Game_SWF_ready);
}
function Game_SWF_ready (evt:Event) : void
{
swf_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, Game_SWF_ready);
//Treat the Loader contents (flashgame.swf) as a MovieClip named "my_Game_Swf"
my_Game_Swf = swf_loader.content as MovieClip;
my_Game_Swf.gameSpeed = set_gameSpeed; //update gameSpeed variable in flashgame.swf
//also adjust SWF placement (.x and .y positions etc) here if necessary
stage.addChild(my_Game_Swf);
_SWF_is_loaded = true;
}
Now in you flashgame file make sure the there's also an actions layers and put in code like this below then compile it first before debugging the Intro/Parent file. When your Intro loads the flashgame.swf it should load an swf that already has the code below compiled.
flashgame.swf
var gameSpeed:int;
gameSpeed = 0; //initial value & will be changed by parent
addEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
function onAdded_toStage (e:Event):void
{
trace("trace gameSpeed is.." + String(gameSpeed)); //confirm speed in Debugger
//*** Example usage ***
var my_shape:Shape = new Shape();
my_shape.graphics.lineStyle(5, 0xFF0000, 5);
my_shape.graphics.moveTo(10, 50);
my_shape.graphics.lineTo(gameSpeed * 10, 50); //this line length is affected by gameSpeed as set in parent SWF
addChild(my_shape);
}
The key line in intro.swf is this: my_Game_Swf.gameSpeed = set_gameSpeed; as it updates a variable in flashgame.swf (referred as my_Game_Swf) with an amount that is taken from a variable in the Parent SWF.
This is just one way you can access information between two separate SWF files. Hope it helps out.

addChild(); - Adding a movieclip to a frame in AS3

I am trying to create a function which adds a text box (a blue rectangle) and a textfield on the box. I have a class in the library named textBox, no external classes used.
I started actionscript 3 (before even learning many of the programming fundamentals) about a month ago, so I am not experienced with this.
function createTextBox() {
var textBoxCoordX:int = 305;
var textBoxCoordY:int = 80;
var dialogueBox:textBox = new textBox;
var dialogueText:TextField = new TextField();
addChild(dialogueBox);
dialogueBox.x = textBoxCoordX;
dialogueBox.y = textBoxCoordY;
dialogueText.x = textBoxCoordX+5;
dialogueText.y = textBoxCoordY+5;
dialogueText.text = "Insert Text Here";
}
After playing, I immediately get two errors that link me to "var dialogueBox:textBox = new textBox;" These errors' descriptions say "1046: Type was not found or was not a compile-time constant: textBox." and "1180: Call to a possibly undefined method textBox."
Ensure that the linkage is set. Right click, advanced, tick Export for ActionScript.
i think no find textBox movieclip,
You must determine the library has the movieClips and the linkedname called textBox.