How to refer to a single frame in a an object class? - actionscript-3

So, I have my character movieclip on frame1 of my timeline. When it hits an enemy the movie goes on frame2 where I have a game-over screen. However I get this error at runtime when the character hits the enemy:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Coinsy/update()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/CharMovement()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/ResetBox()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/EnemyBall()
I debugged and it seems that the problem is in my coins class:
package
{
import flash.display.*;
import flash.events.*;
public class Coinsy extends MovieClip
{
var char:MovieClip;
var MTL = MovieClip(root);
public function Coinsy()
{
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
char = MTL.char1;
// CC = int(root).CC;
if(hitTestObject(char))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MTL.CC++;
}
}
}
}
at char = MTL.char1;, apparently I can't use MovieClip(root) when I have more then one frame.
char1 is my character's instance name andCC is the coin count var.
I'm pretty new at ActionScript and I'd like to know where i did wrong. THANKS!

The error is actually happening on this line
if(hitTestObject(char))
What's happening is the game is still trying to check for collisions on the game-over screen. The char1 object couldn't be found, so it became null. Null essentially means something's empty, so you can't do anything with it. If you ever try null.something you'll get an error, and someFunction(null) will quite likely cause an error too.
A quick fix is changing the line to
if(char != null && hitTestObject(char))
as Karma mentioned.
However, the following three TypeErrors are unrelated to the code you posted. They're happening because some line of code is trying to do null.something. The CharMovement, ResetBox and EnemyBall functions are probably being called when they shouldn't be. You might need to remove event listeners when game-over happens, or add extra logic to check for null values.

Related

TypeError: Error #1009: Cannot access a property or method of a null object reference. The object isn't null

Okay, so my game codes are not problematic at all and don't effect the game UNLESS I declare the level "OneManager" as a variable.
OneManager is the class for my level. The level is a movieclip containing all the level's components. And Main is the document class.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at OneManager()[C:\Users\Jay\Creative Cloud Files\Subject 51 Experimental\OneManager.as:38]
at Main()[C:\Users\Jay\Creative Cloud Files\Subject 51 Experimental\Main.as:16]
I don't think it makes any sense. I deleted line 38, then the problem indicated until line 39, then I deleted them over and over. Then practically until my code was useless, that's when I FINALLY got no error...
These codes don't even look problematic at all. I even tried adding them manually by adding the class's movieclip to the stage myself and it worked completely fine. But what I'm trying to do is add it to the stage through code by making this class's movieclip a variable and when the button is clicked, but if I did that - I would get these random errors from other classes.
I'm not sure what's wrong here. There is no trouble compiling, so I get no compile errors. Only errors from the output. These lines of codes aren't null, but the output says it is. I'm confused.
Please help, thanks!
Code for Main Document Class:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
var mountains: Mountains;
var homePage: HomePage;
var oneManager: OneManager;
public function Main()
{
mountains = new Mountains;
homePage = new HomePage;
oneManager = new OneManager;
addChild(homePage);
homePage.playButtons.addEventListener(MouseEvent.CLICK, onPlayButtonsClick);
}
function onPlayButtonsClick(event:MouseEvent):void
{
//var level1Page = new Level1Page;
removeChild(homePage);
addChild(oneManager);
}
}
}

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.

Error #1034 while running flash application

Alright, I know that this error is occurring because of something to do with addchild() function. On frame 2. How do I know? Because when I remove the snippets of code that dynamically place an object onto the stage it works fine. The error is below and the source code for frame 2 is below the error messages.
Error Code
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.DisplayObject.
at fl.motion::AnimatorBase/play()
at fl.motion::AnimatorBase$/processCurrentFrame()
at fl.motion::AnimatorBase/handleEnterFrame()
at flash.display::MovieClip/nextFrame()
at Lemonade_fla::MainTimeline/begin()[Lemonade_fla.MainTimeline::frame1:27]
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to
FRAME 2 CODE
flash.display.DisplayObject.
stop();
var guide_tut:guide = new guide();
addChild(guide_tut);
//stand.addEventListener(MouseEvent.CLICK, check);
addEventListener(Event.ENTER_FRAME, check);
function check(ev : Event) : void {
cash.text.text = cash1;
lemons_count.text.text = lemons1;
cups_count.text.text = cups;
straws_count.text.text = straws;
ice_count.text.text = ice;
}
shop_mc.addEventListener(MouseEvent.CLICK, shopnow);
function shopnow(event:MouseEvent):void{
nextFrame();
}
1) I ran your code it works fine, the only place the error could be generated is:
flash.display.DisplayObject.
should be: flash.display.DisplayObject;
but you don't need it.
and
var guide_tut:guide = new guide();
addChild(guide_tut);
I ran it with just an empty mc, it works fine.
2) no reason to have (i know its commented out) but there never a reason to have
stand.addEventListener(MouseEvent.CLICK, check) ever since check is EnterFrame
basically you're error is within either display, most likely you're not extending the class from MovieClip
public class guide extends MovieClip
instead of movieclip it can be sprite or other displayobject...

External SWF Loading problem

I have an SWF loading in an SWF containing a papervision scene.
I've done it before yet problem is, I get an error - I'm not sure what the issue really is.
private function downloadSWF(url:String):void
{
trace(url);
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressEventHandler);
loader.load(urlRequest);
}
private function loaderProgressEventHandler(ev:ProgressEvent):void
{
loader.preloaderCircle.percent = ev.bytesLoaded / ev.bytesTotal;
}
When the application runs the code - I get the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.dehash.pv3d.examples.physics::WowDemo()
Why am I getting this if the loading hasn't even complete yet?
Thanks in advance guys.
Edit: Try a blank child swf, see if the other one was trying access something in the parent. – Jorge
I did this, it seems, even with a simple SWF with a mouse click listener causes the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at simple_fla::MainTimeline/frame1()
My code for that is:
import flash.events.MouseEvent;
this.stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(ev:MouseEvent):void
{
trace("MouseClick");
}
Am I missing something blatantly obvious??
The problem is that the loaded swf starts running without it being added to the stage. So stage is null, resulting in that error.
The second example with the addedToStageEventHandler works because there stage is only referenced after the object was added to the stage, so stage is not null anymore.
A possible solution for the first error is adding the loader to the stage. That way, when the swf is loaded and starts, it already has a stage reference.
It won't even load if there's an error. You're accessing an unreferenced object on the WowDemo() class...did you instantiate correctly the class?
this seems to work inside my child SWF:
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
function onClick(ev:MouseEvent):void
{
trace("MouseClick");
var event:Event = new Event("END", true, false);
this.dispatchEvent(event);
}
function addedToStageEventHandler(ev:Event):void
{
this.stage.addEventListener(MouseEvent.CLICK, onClick);
}
does anyone know Why?

AS3: global var of type SimpleButton changes to DisplayObject for unknown reason, won't let me access .upState.textColor!

This is a problem that's best explained in code.
I don't see how active_button.upState, which I know is a TextField (see trace statements), mysteriously turns into a DisplayObject when I try to access the textColor property.
I've included the error messages below for reference.
Also, why is it that when I have an object that I know is a SimpleButton (again, see traces) I need to cast it to SimpleButton in order to store it in a var? That doesn't make any sense to me.
All help is much appreciated. Thanks.
public class Menu extends MovieClip
{
private var active_button:SimpleButton;
public function Menu()
{
// menu_list is a collection of SimpleButtons, I make the first one the 'active_button' and give each a MOUSE_DOWN event listener.
trace( menu_list.getChildAt( 0 )); // writes [object SimpleButton]
active_button = SimpleButton( menu_list.getChildAt( 0 )); // Cast is required here. Otherwise throws Error 1118. Strange. Why is that?
for( var i:Number = 0; i < menu_list.numChildren; i++ )
{
menu_list.getChildAt( i ).addEventListener( MouseEvent.MOUSE_DOWN, menuClick );
}
}
private function menuClick( e:Event ) : void
{
trace( e.target ); // writes [object SimpleButton]
active_button = SimpleButton( e.target ); // Cast is required here. Otherwise throws Error 1118. Still Strange.
trace( active_button ); // writes [object SimpleButton]. Normal.
trace( active_button.upState ); // writes [object TextField]. Normal.
active_button.upState.textColor = 0xAAAAAA; // Throws Error 1119. WTF?! textColor is a perfectly valid property of active_button.upState. Why is it suddenly type DisplayObject?
}
}
ERRORS:
1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:SimpleButton.
1119: Access of possibly undefined property textColor through a reference with static type flash.display:DisplayObject
EDIT: I've condensed my question a little bit and posted as AS3: Why does datatype automatically change from TextField to DisplayObject on its own?
Anything that is pulled from the displaylist is classed as the lowest possible dinominator (eg. displayObject), as although SimpleButton is a displayObject, not all displayObjects will be SimpleButtons.
This is probably the reason for your second problem too, just slightly different in execution. is there a direct reference in SimpleButton that states upState is a TextField?
try switching off Strict Mode of compilation and see if it helps.
see Type Checking Livedocs for more information