PDA

View Full Version : Buttons in AS3



stefanmhodgson
April 13th, 2008, 06:02 PM
Hello everybody,

I am sturggling to get to grips with AS3, I am not great at coding but just can't get my head around the new way of linking buttons in AS3.

I want to create a button, that when it is clicked, it will load an external SWF.

Surely that can't be to difficult.

This is the code that I have cobbled together, the trace me statement works but it doesnt load the SWF.

inv_btn.addEventListener(MouseEvent.MOUSE_DOWN, newvid);
function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("PhysTest.swf"));
}

This is the error that comes up:

you clicked me
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at General::Input()
at Main()

Please help me, I am at my wits end!

Thanks

Stefano

rohicks
April 13th, 2008, 09:49 PM
Set your variables ...


import flash.net.URLRequest;
import flash.events.*;
import flash.display.*;

var PhysTestRequest:URLRequest = new URLRequest("PhysTest.swf");
var PhysTestLoader:Loader = new Loader();

inv_btn.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent):void
{
trace("you clicked me");
PhysTestLoader.load(PhysTestRequest);
addChild(PhysTestLoader);
}
Try that code and tell me if it works?

You could also create a container and add it as follows ...


import flash.net.URLRequest;
import flash.events.*;
import flash.display.*;

var PhysTestRequest:URLRequest = new URLRequest("PhysTest.swf");
var PhysTestLoader:Loader = new Loader();
var container:Sprite = new Sprite();

inv_btn.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent):void
{
addChild(container);
PhysTestLoader.load(PhysTestRequest);
container.addChild(PhysTestLoader);
}
The 1009 error, typically means you're accessing something before it's instantiated.

stefanmhodgson
April 14th, 2008, 06:29 AM
Hello, thanks so much for taking your time out to reply.

I used the first set of code, I just whacked it in the first keyframe, i have a button on the stage with the instance name "inv_btn".

It is coming up with this error though,

"1095: Syntax error: A string literal must be terminated before the line break."

Any ideas?

Stefano

rohicks
April 14th, 2008, 11:21 AM
Yah sorry i mistyped some of the code ... fixed in original reply with both sections as well as beneath.



import flash.net.URLRequest;
import flash.events.*;
import flash.display.*;

var PhysTestRequest:URLRequest = new URLRequest("PhysTest.swf");
var PhysTestLoader:Loader = new Loader();

inv_btn.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent):void
{
trace("you clicked me");
PhysTestLoader.load(PhysTestRequest);
addChild(PhysTestLoader);
}
I accidentally put in a quote where it wasn't supposed to be (-:

stefanmhodgson
April 14th, 2008, 01:11 PM
This is the error that comes up:

you clicked me
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at General::Input()
at Main()

You have been great so far, but there is still an error

Stefan

rohicks
April 15th, 2008, 03:10 PM
Could you post or send me the files you are working with through pm?
I'm no AS3 expert, but I'm pretty sure that should work?

stefanmhodgson
April 15th, 2008, 07:36 PM
Hello, thanks very much, your code was spot on.

The reason I am getting those errors, is the swf that I am importing is dependent on other actionscript files located in a seperate folder. However, these are all linked correctly, as the swf I am importing works fine on its own.

So I am not sure how to get around this, your code is right but I am getting that error and I am pretty sure it is because of the dependent .as files.

Any ideas?

Stefan

Rundevo
April 20th, 2008, 03:44 PM
Sounds like the instance you are talking to, ie, the button, is not on par with the actionsscript, maybe its on a different keyframe or the alpha is set to 0.

gametr4x
April 14th, 2010, 08:04 AM
Just in case anybody comes across a similar problem I was having, I thought I'd post here :P

I got the same error as described here, and was pretty sure I wasn't doing anything wrong. Turns out the SWF I was loading was doing something that was depending on itself being added to the stage (which it might not be since it's being loaded).

Simple solution is to create an event listener in the document class that listens for Event.ADDED_TO_STAGE, and to not do anything before then.