Flash CS5.5 Output Error - actionscript-3

I have been creating a hypermedia player, and i have got to a stage where it is glitching out and it is apparently a...
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AvalancheCityHypermediaPlayer_fla::MainTimeline/fl_CustomMouseCursor()
Here is my code:
import flash.events.Event;
cust_cursor.mouseEnabled= false;
cust_cursor.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
cust_cursor.x = stage.mouseX;
cust_cursor.y = stage.mouseY;
}
Mouse.hide();
I am not sure why it is not working properly, basically when a button is hovered over it is meant to jump to frame 2 and stop, but it is jumping to that frame, and then jumping straight to frame 1 without stopping on frame 2, and stops on frame 1.

1 . Your error isn't producing a line number. You (and I) will find this invaluable for debugging; if in the Flash IDE, you can turn this on in the Publish Settings under swf preferences as a toggle titled "Permit debugging".
2 . Is this code inside a class, or in document code (e.g., Flash IDE "Actions" tab)? If it's inside a class, make sure you pass a reference of the stage to the constructor of your class and assign it to an internally persistent variable so that fl_CustomMouseCursor can address it. By default, classes have no internal way of referencing the stage, and I'm assuming that's what's producing your #1009 error.
For example, inside your class constructor...
package com.example {
public class MyClass {
private var stage;
public function MyClass(arg) {
stage = arg;
}
}
}
And outside when instantiating the class...
var myObj:MyClass = new MyClass(stage);
3 . If you want your code to stop on a frame, use stop(); or gotoAndStop()
4 . Finally, if you're compiling with Flash IDE, you can debug this and see exactly which variable in the stack the runtime environment is having issues with. You can access it from the debug menu or by compiling with control-shift-enter.

Related

How can I call timeline-specific methods from an external file in AS3?

I'm creating a game is Flash CS5 with ActionScript 3. To simplify things, I've created a file (Game.as) in the top layer of my source folder. My Game.as file looks as follows:
package {
public class Game {
public static function fail():void {
stop();
var restart:RestartButton = new RestartButton();
addChild(restart);
restart.x = stage.stageWidth/2;
restart.y = stage.stageHeight/2;
Game.createButton(restart, function(e:MouseEvent):void { gotoAndPlay (1, "Title Sequence") });
}
}
}
I would supposedly call Game.fail () from a frame on a timeline a scene, but I get these compiler errors:
Line 11 1180: Call to a possibly undefined method stop.
Line 19 1180: Call to a possibly undefined method gotoAndPlay.
Line 17 1120: Access of undefined property stage.
Line 16 1120: Access of undefined property stage.
Line 14 1180: Call to a possibly undefined method addChild.
Why are these errors happening? What can I do to fix them?
Thanks for your help in advance.
The problem is that none of those methods exist on your Game class.
There's at least two good reasons for this:
Your Game class needs to extend MovieClip to have the methods stop(), gotoAndPlay(), addChild(), and the stage property. For example class Game extends MovieClip would give you those methods.
Even if your Game extends MovieClip you can't access methods like stop() from static scope, only from instance scope (ie this). This is because static scope is essentially a global code space so none of those methods make any sense: when you say stop() you have to say what instance will stop, ex this.stop() or thatThing.stop(). The static code has no awareness of what instances of the class there may even be, so there's no way an instance function can work. You need an instance of the Game class, not just a static function.
What I think you are trying to do is essentially use Game as a singleton for your main timeline. You can do that like this:
package {
public class Game extends MovieClip {
// global reference to a single instance of the Game
public static game:Game;
public function Game() {
// store a global reference to this instance
// NOTE: bad things will happen if you create more than one Game instance
game = this;
}
// do NOT use static for your methods
public function fail():void {
stop();
var restart:RestartButton = new RestartButton();
addChild(restart);
restart.x = stage.stageWidth/2;
restart.y = stage.stageHeight/2;
createButton(restart, function(e:MouseEvent):void { gotoAndPlay (1, "Title Sequence") });
}
}
}
Now you need to set this class as your document class. There is only ever one instance of the document class, so this works well for you. However, you could link this class to a symbol in your library and instantiate it by code (new Game()) or by placing a timeline instance on a keyframe.
Finally, to call Game methods from anywhere you can use the Game.game instance:
Game.game.fail();

External ActionScript Only Executed When Testing Single Scene

New to AS 3.0 and I seem to have an issue where an external AS file is run when I test an individual scene in Flash Pro but not when I test the entire movie or when I test from Flash Builder. Anyone know what the problem might be?
Here's the code from the AS file:
package {
import flash.display.MovieClip;
public class Level1 extends MovieClip {
public var myplayer:MyPlayer;
public function Level1() {
super();
myplayer.x = 516;
myplayer.y = 371;
if (myplayer.x == 516)
{
trace("player x is 516");
}
else if (myplayer.y == 371)
{
trace("player y is 371");
}
}
}
}
Any ideas?
EDIT
I think I figured out the problem. The swf contained two scenes, and the external AS file started running at the start of Scene 1, but the myPlayer movie clip was not instantiated until Scene2, which, I think was causing the problem I was having, in addition to giving a 1009 null object error.
So I simply deleted the first scene, and now everything works fine. Maybe I will put that first scene in a separate SWF? Or, is there some way to delay a script's execution until a certain scene?
Your problem:
When the constructor of your doucment class runs, myPlayer doesn't yet exist so it throws a 1009 runtime error and exits the constructor at the first reference to myPlayer.
Solutions:
Put all the myPlayer code on the first frame of the MyPlayer timeline. OR use your current document class as the class file for MyPlayer (instead of documentClass). Change all references to myPlayer to this.
Listen for frame changes and check until myPlayer is populated, then run your code.
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler(e):void {
if(myPlayer){
//run the myPlayer code
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
}
If your frame rate is 24fps, this code though will run 24 times every second (until it finds myPlayer), so it's not the most performant way to go.
Use events. Add an event to the first frame of myPlayer (or to a class file for MyPLayer) that tells the document class that it exists now.
stage.dispatchEvent(new Event("myPlayerReady"));
Then listen for that event on the document class:
stage.addEventListener("myPlayerReady",playerReadyHandler);
playerReadyHandler(e:Event):void {
//your player code
var myPlayer = MyPlayer(e.target); //you can even get the reference from the event object
}
Thanks for your constructive, helpful responses, LDMS. I thought I had found a solution when I hadn't. Your advice worked. What I did was add the following code to the timeline of MyPlayer
this.x = 516;
this.y = 371;
if (this.x == 516)
{
trace("player x is 516");
}
if (this.y == 371)
{
trace("player y is 371");
}
and I removed the code from the document class. Everything seems to be working fine now. Thanks again for your help!

MovieClip button In Class

Tried this,
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
Image
And the same code on frame 1, And there's a MovieClip Button on stage having Instance name "Homebtn".
Imports
import flash.events.*;
Importing all classes from a package that originates in flash has zero impact on compile size because they're already present in the Flash Player runtime environment. It's pure monotony that you're required to explicitly declare these imports, but good practice when dealing with third party packages.
Stage Relationship
Document code (i.e., code in the Flash IDE timelines) have a direct relationship to MainTimeline, whereas class files do not. If you want to add button1.addEventListener(MouseEvent.CLICK, myButtonClick); to your class, you're not going to be able to do so unless you:
A: Pass a pointer to the button/stage/root to the class when instantiating your test class:
var myObj:test = new test(root)
B: Wait to add the event listener until after you've given the test object a parent relationship to the stage from which to traverse to the button:
addChild(test);
inside your class...
public function test() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, registerBtn)
}
private function registerBtn():void {
this.parent.button1.addEventListener(MouseEvent.CLICK, myButtonClick);
}
Turn on Debugging
To find the cause of your bugs, you need to debug your code. If you're using Flash IDE CS6, then you can enable this by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...
null object reference at myDocument/doSomething()
...to a much clearer...
null object reference at myDocument/doSomething() package\myClass.as:20
...which now denotes which line in your code to look for your issue.
Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.
Run by going to the menu "Debug > Debug Movie > Debug", or use the keyboard combo CONTROL+SHIFT+ENTER.
Now that you're armed with the know-how to do this on your own, I'll cover what you'd encounter, and how you'd fix it (since you're new).
First, it's flash.events with an "s". So we'll change that.
Next, compiling it we get the following errors:
So we see on line 7 of our test.as class: you've placed the timeline code into the class.
var myObj:test = new test(root);
addChild(test);
You don't want to instantiate you class from within itself as it'll never get instantiated. Think of your code as a railroad. The train starts with your timeline code, and only runs on the rails set before it. Your class is floating off to the side, ready with all its interesting turns and zigzags, but you have to add it to the rails for it to be actually run. That's instantiation; we're copying that path onto the current track, and the train runs on it.
So, we get rid of lines 6 & 7, and we're left with Access of possibly undefined property Homebtn. Calling this.parent is actually a getter function, and it returns a DisplayObjectContainer. Because AS3 is a strongly datatyped language, the compiler will know that there is no such property "Homebtn" on DisplayObjectContainers (silly compiler). But of course, you know it's there (or at least it will be by the time this code runs). A simple way of getting around that is by making it evaluate the reference at run-time.
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
By encapsulating the button name as a string and within brackets, we've done that.
Now we recompile again, and get the following:
This is because all event listeners receive one argument: an event object. You may not use it, but not having a variable to hold it is a no-no.
private function registerBtn(e:Event):void {
As a final point. All class functions need to be denoted as to what namespace they exist in. myButtonClick needs one, so we'll add it as private since no external (ie., non-class based) functions need access to it.
Here's your revised code:
test.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
test.fla (timeline code on frame 1)
import test;
var Homebtn:MovieClip = new MovieClip();
Homebtn.graphics.beginFill(0xFF0000, 1);
Homebtn.graphics.drawRect(0, 0, 150, 25);
Homebtn.graphics.endFill();
addChild(Homebtn);
var testObj:test = new test();
addChild(testObj);

AS3 Add event listener to a movieclip within a movieclip

I currently have two movieclips one called mcInvFrame, and one called btnCloseInv (It is a movieclip, and I know the naming convention is wrong). btnCloseInv is located inside mcInvFrame. I have two files Inventory.as and my main document class. I can load the mcInvFrame just fine to the stage and everything works as expected. However when I try to access the btnCloseInv movieclip i get errors. Here is the code for Inventory.as I have commented out my most recent failed attempt
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Inventory extends MovieClip
{
public var inv:MovieClip = new mcInvFrame;
public function Inventory()
{
addChild(inv);
/*var invClose:MovieClip = inv.btnCloseInv;
invClose.addEventListener(MouseEvent.CLICK, CloseInventory);
function CloseInventory($e:MouseEvent):void
{
this.parent.removeChild(inv);
}*/
}
}
}
What I need to know is can/should i create a variable within inventory.as for the button that I can access from the main document? If so how?
P.S. I have been searching the forums and trying various solutions but I either didn't understand the implementation or they were not suitable for this situation. The most common error I receive is "Error #1009: Cannot access a property or method of a null object reference." Occasionally I will receive an error stating that an object has no properties.
you cant register event on stage.movieclip.movieclip2 , i have tried to do the same thing before, but it wont work, try to create btnCloseInv outside, then use this code
btnCloseInv.x = mcInvFrame.x + numberHere;
btnCloseInv.y = mcInvFrame.y + numberHere2;
if you doesn't want to use this code, AS3 - Button inside MovieClip triggers MC's event
EDIT: if you set mcInvFrame.buttonMode = true it will not work

How to fix AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference?

I am getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Skool_fla::MainTimeline/frame1()[Skool_fla.MainTimeline::frame1:10]
at flash.display::MovieClip/gotoAndStop()
at Skool_fla::MainTimeline/goback()[Skool_fla.MainTimeline::frame2:22]
What is causing this error and how do I fix it?
This is my code for both the frames:
Frame 1: This is the main menu screen where you can access the credit section
import flash.events.MouseEvent;
//setting up the variables
//events
//stop the timeline
stop();
//the play button
play_btn.addEventListener(MouseEvent.CLICK, playani);
function playani(e:MouseEvent)
{
//asking it to progress to the load menu
gotoAndStop(3);
}
//the credits button
credit_btn.addEventListener(MouseEvent.CLICK, creditslide);
function creditslide(e:MouseEvent)
{
//asking it to go the credits frame
gotoAndStop(2);
}
Frame 2: This is where the credits appear
//
//
//all the imports
//events
var credit:credits_bck = new credits_bck ();
var credits_name: credit_nm = new credit_nm ();
var back_butn: back_button = new back_button ();
addChild (credit);
addChild (credits_name);
addChild (back_butn);
back_butn.addEventListener(MouseEvent.CLICK,goback);
function goback(G:MouseEvent)
{
removeChild (credit);
removeChild (credits_name);
gotoAndStop(1);
}
Either play_btn or back_butn is null. Your error message's line numbers don't correspond to your code so it's hard to say. But the gist is you're trying to access a property of something that isn't anything. Check to make sure you're initializing your variables/references properly.
Maybe your problem is Flash bug too.
In my FLA there was a layer with one empty keyframe. If I puted a vector graphics on it, the error was gone. If there was one or multiple MovieClips and there was no vector graphic - the error was there again.
Then I made a new layer and copy pasted all the objects from damaged layer to new and deleted the damaged layer. It solved the problem.
NOTE: Don't copy the keyframes. Only copy the contents.
Now my project is much more complicated and sadly the error came back again.
Test movie frequently and if the error comes back, check the last keyframes and layers you created.