I am currently making a media player and everything was working fine before i put this code in the the first scene.
btn_next.addEventListener(MouseEvent.CLICK, nextsong);
function nextsong(event:MouseEvent):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace("finished");
isPlaying=false;
gotoAndPlay(1,"Airy");
}
After you press the button it is supposed to jump to the scene "Airy" but i get this error message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at player1_fla::MainTimeline/frame2()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at flash.display::MovieClip/gotoAndPlay()
at player1_fla::MainTimeline/nextsong()
This error occurs after the button is pressed and the next scene begins playing. Im fairly new to action script 3.0 and im not sure what is wrong. When i run the debugger it points out this line of script:
airybtn.addEventListener(MouseEvent.CLICK, playsound1);
the function playsound1 is:
function playsound1(event:MouseEvent):void
{
if (! isPlaying1)
{
soundchannel1 = mysound1.play(lastposition,0);
airybtn.btn_pause.visible = true;
isPlaying1 = true;
}
else
{
lastposition = soundchannel1.position;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace(lastposition.toFixed(0), mysound1.length.toFixed(0));
isPlaying1 = false;
}
soundchannel1.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel1 = mysound1.play(0,0);
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false
}
}
PLease help me....
I did that and this is what it came up with:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at player1_fla::MainTimeline/frame2()[player1_fla.MainTimeline::frame2:5 8]
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at flash.display::MovieClip/gotoAndPlay()
at player1_fla::MainTimeline/nextsong()[player1_fla.MainTimeline::frame1 :15]
Error messages are ordered such that the "at" lines are ordered closest to furthest from the problem, so you want to look at line 58 of frame 2 to find the problem. If you are using scenes and only have one frame in scene 1, then frame 2 would be the first frame in scene 2
If line 58 is the line you say was pointed to by the debugger, then chances are airybtn does not exist as far as your code sees it.
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name (or a different name) assigned in the preceding frame(s).
Ned Murphy wrote:
Error messages are ordered such that the "at" lines are ordered closest to furthest from the problem, so you want to look at line 58 of frame 2 to find the problem. If you are using scenes and only have one frame in scene 1, then frame 2 would be the first frame in scene 2
If line 58 is the line you say was pointed to by the debugger, then chances are
airybtndoes not exist as far as your code sees it.
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name (or a different name) assigned in the preceding frame(s).
What do you mean by declared but not instantiated?
The instance name is correct as i copy and pasted from scene 1 and i checked it. Nothing is animated, it has the same name as the instance in scene 1.
Im not sure what to do?
in that case the variable has been instantiated. here is the full script:
import flash.media.SoundChannel ;
import flash.events.MouseEvent ;
import flash.events.Event;
var isPlaying1:Boolean = true;
var lastposition1:Number = 0;
var mysound1:airy = new airy() ;
var soundchannel1:SoundChannel = new SoundChannel() ;
soundchannel1 = mysound1.play(0,0);
soundchannel1.addEventListener(Event.SOUND_COMPLETE,
onPlaybackComplete);
function onPlaybackComplete (event:Event):void
{
lastposition1 = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
airybtn.addEventListener(MouseEvent.CLICK, playsound1);
function playsound1(event:MouseEvent):void
{
if (! isPlaying1)
{
soundchannel1 = mysound1.play(lastposition,0);
airybtn.btn_pause.visible = true;
isPlaying1 = true;
}
else
{
lastposition = soundchannel1.position;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace(lastposition.toFixed(0), mysound1.length.toFixed(0));
isPlaying1 = false;
}
soundchannel1.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel1 = mysound1.play(0,0);
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false
}
}
btn_stop.addEventListener(MouseEvent.CLICK, stopsound1);
function stopsound1(event:MouseEvent):void{
if(isPlaying1)
{
lastposition = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
}
btn_replay.addEventListener(MouseEvent.CLICK, replaysound1);
function replaysound1(event:MouseEvent):void{
lastposition = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = true;
trace("finished");
isPlaying1=false;
{soundchannel1 = mysound1.play(0,0);
airybtn.btn_pause.visible = true;
isPlaying1 = true;
}
}
//-----All playlist button events-----//
//4 AM//
btn_4am.addEventListener(MouseEvent.CLICK, play4am1);
function play4am1(event:MouseEvent):void
{
lastposition = 0;
soundchannel1 = mysound1.play(0,0);
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
stop();
do you have code in more than one layer of frame 2?
if yes, put all your code in one layer and retest and copy-paste the problematic line of code. if no, take a screen shot showing your timeline at frame 2 with all its layers and showing the actions panel with all its code for that frame and the layer with your code.
ok i put all the scripts in scene1 on one layer and same with scene 2, to 2 lines that the debugger mentions have nothing on it at all.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at player1_fla::MainTimeline/frame2()[player1_fla.MainTimeline::frame2:3 0]
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at flash.display::MovieClip/gotoAndPlay()
at player1_fla::MainTimeline/nextsong()[player1_fla.MainTimeline::frame1 :114]
frame2, line 30: {
frame1, line 114: [blank]
im pretty sure it has something to do with the gotoAndPlay action as that is when the error started occuring.
btn_next.addEventListener(MouseEvent.CLICK, nextsong);
function nextsong(event:MouseEvent):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace("finished");
isPlaying=false;
gotoAndPlay(1,"Airy");
}
and the script is:
import flash.media.SoundChannel ;
import flash.events.MouseEvent ;
import flash.events.Event;
var isPlaying1:Boolean = true;
var lastposition1:Number = 0;
var mysound1:airy = new airy() ;
var soundchannel1:SoundChannel = new SoundChannel() ;
soundchannel1 = mysound1.play(0,0);
soundchannel1.addEventListener(Event.SOUND_COMPLETE,
onPlaybackComplete);
function onPlaybackComplete (event:Event):void
{
lastposition1 = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
airybtn.addEventListener(MouseEvent.CLICK, playsound1);
function playsound1(event:MouseEvent):void
{
if (! isPlaying1)
{
soundchannel1 = mysound1.play(lastposition,0);
airybtn.btn_pause.visible = true;
isPlaying1 = true;
}
else
{
lastposition = soundchannel1.position;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace(lastposition.toFixed(0), mysound1.length.toFixed(0));
isPlaying1 = false;
}
soundchannel1.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel1 = mysound1.play(0,0);
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false
}
}
btn_stop.addEventListener(MouseEvent.CLICK, stopsound1);
function stopsound1(event:MouseEvent):void{
if(isPlaying1)
{
lastposition = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
}
btn_replay.addEventListener(MouseEvent.CLICK, replaysound1);
function replaysound1(event:MouseEvent):void{
lastposition = 0;
soundchannel1.stop();
airybtn.btn_pause.visible = true;
trace("finished");
isPlaying1=false;
{soundchannel1 = mysound1.play(0,0);
airybtn.btn_pause.visible = true;
isPlaying1 = true;
}
}
//Volume Slider//
import fl.events.SliderEvent;
import flash.media.SoundTransform;
var voltransform1:SoundTransform = new SoundTransform();
sc.addEventListener(SliderEvent.THUMB_DRAG, changevol1);
function changevol1(event:SliderEvent):void{
voltransform1.volume = sc.value;
SoundMixer.soundTransform = voltransform1;
}
//Back button- btn_back//
btn_next.addEventListener(MouseEvent.CLICK, prevsong);
function prevsong(event:MouseEvent):void
{
lastposition = 0;
soundchannel1.stop();
btn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
gotoAndPlay(1,"4am");
}
//-----All playlist button events-----//
//4 AM//
btn_4am.addEventListener(MouseEvent.CLICK, play4am1);
function play4am1(event:MouseEvent):void
{
lastposition = 0;
soundchannel1 = mysound1.play(0,0);
soundchannel1.stop();
airybtn.btn_pause.visible = false;
trace("finished");
isPlaying1=false;
}
stop();
Thanks for the plain English replay Ned! With my code, the error was as you mentioned:
- is one of two or more consecutive keyframes of the same objects with no name (or a different name) assigned in the preceding frame(s).
I had a button which was on two keyframes with different names. It would be really nice in the next version of Flash if Adobe could make some plain English error codes though!
Dan
I am having a similar problem.
Please excuse me if this is a stupid question, but I'm still new to flash/as3.
I've modified a small game from the book I've been studing, and it works great.
The player object was controlled by it's own class, and sitting directly on the stage.
In the player class, it adds the eventlisteners for the keyboard to the stage (stage.addEventListener....).
So, I changed it so this was level 1 in a bigger game.
I moved all symbols and objects from the stage of my FLA into a movie class symbol I called Level01, which used the class Level01, which had the same code as the main class had before. I then created a new "main" class that adds the Level01 symbol to the stage when I press a start button.
This all works fine, but my player class now gives me an error (like above) when calling the stage.addEventListener...
I have tried eliminating stage, replacing it with this, parent, and a combination of those, but it doesn't work.
What am I missing?
If you need the source code, please let me know.
Thanks,
Ole
North America
Europe, Middle East and Africa
Asia Pacific