Copy link to clipboard
Copied
Hello, I am working on a simple Flash game where you navigate through different rooms. Each room is set in a frame on the main timeline. I am getting an Error #1009 message when I navigate to certain rooms, but each frame pretty much has the same code so I'm not sure why some frames are encountering this issue while others are fine. When the error message appears I am still directed to the "room" I clicked on, but all of the buttons in that room disappear and you are then stuck there and have to restart the project. I looked through other forums where people had this similar error message but I couldn't find a solution that would help me, so I apologize for posting such a common problem! Here is a screenshot of the error message I receive, which says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at project1_fla::MainTimeline/frame4()[project1_fla.MainTimeline::frame4:43]
at flash.display::MovieClip/gotoAndStop()
at project1_fla::MainTimeline/enterElevator3()[project1_fla.MainTimeline::frame12:25]
Here are links to screenshots of the debug session where it points to the lines of code that contain the error and also screenshots of my code in those frames:
Frame 4 is labelled as "elevator" and frame 12 is "medEntrance". I know the problem is jumping from frame to frame, but I honestly have no idea what the error would be. I double checked all of my code and it should all be correct, and what makes it even more confusing is that some rooms work perfectly while others have this problem and they all use this exact code but with different MC names! 😕 Something I did notice is that rooms that are more frequently linked to (for example the elevator can link to three different "floors" or frames) are the ones that encounter this issue. But I just chose this pair in particular since it contains the least amount of code I don't understand why having several frames linking to one frame in particular would be a problem, but this is my first time using Flash and AS3 so I'm not familiar at all with its limitations.
Sorry for the messy code and information overload! I figured it's better to provide too much information than too little and thank you for your help, it is much appreciated!
instead of distributing your rooms to different keyframes,
move them to separate Layers
and only use the first frame of every layer
then do what amy suggested and either wirtie a document class that will have all your main code in it
or put all your code on the first frame of the first layer (seperated from the layers where you have your rooms on)
Thus it becomes available for all your rooms.
To hide rooms you don`t currently need put all the rooms inside an array:
var rooms:Array = [room1,room2....ro
...Copy link to clipboard
Copied
It looks like you are using code on the frame. This is a really bad practice (although you can do it) in AS3.0.
Essentially the problem here is that a variable is being requested and it is not available. Most likely due to conflicting frames not sharing the variables. When you switch a frame you are actually removing everything from the previous frame.
Good practice would be to write your code into the document class and other classes that are imported. This is the object orientated programming practice and will serve you better in the long run.
Copy link to clipboard
Copied
instead of distributing your rooms to different keyframes,
move them to separate Layers
and only use the first frame of every layer
then do what amy suggested and either wirtie a document class that will have all your main code in it
or put all your code on the first frame of the first layer (seperated from the layers where you have your rooms on)
Thus it becomes available for all your rooms.
To hide rooms you don`t currently need put all the rooms inside an array:
var rooms:Array = [room1,room2....room100];
then write a function
function showRoom(_index:int):void{
for (var i:int = 0;i<rooms.length;i++){
if(i==_index){
rooms.visible = true;
}
else{
rooms.visible = false;
}
}
}
so when you move to the next room call sth. like
showRoom(4);
//this will hide every room besides the 5th one
Copy link to clipboard
Copied
Thank you very much for responding! I re-organized my images so that each "room" is on a different layer so that they are all on frame 1, and I placed all of the code on frame 1 of the first layer as well. I decided not to go with Document Classes because I'm very new to AS3 so given the time restraints on my project I thought the other option would be easier.
I followed the code moccamaximum provided in order to set up the array but I received this error message:
ReferenceError: Error #1056: Cannot create property visible on String.
at Project2_fla::MainTimeline/showRoom()
at Project2_fla::MainTimeline/enterHospital()
I'm pretty sure I just didn't set it up correctly, but I labeled each layer either room1, room2, room3, etc. and set up the code like this:
var rooms:Array = ["room1","room2","room3","room4","room5","room6","room7","room8",
"room9","room10","room11","room12"];
function showRoom(_index:int):void{
for (var i:int = 0;i<rooms.length;i++){
if(i==_index){
rooms.visible = true;
}
else{
rooms.visible = false; ------------------------> this is where the error is
}
}
}
// introduction message
var introMessage_mc:introMessage = new introMessage();
introMessage_mc.x = 900;
introMessage_mc.y = 822;
addChild(introMessage_mc);
introMessage_mc.visible = false;
// button on door to enter the hospital
mainEntrance_mc.addEventListener(MouseEvent.CLICK, enterHospital);
mainEntrance_mc.addEventListener(MouseEvent.MOUSE_OVER, hover);
mainEntrance_mc.addEventListener(MouseEvent.MOUSE_OUT, hoverOff);
// button click that moves you to room 2
function enterHospital(e:MouseEvent):void{
showRoom(1); ---------------------------------------------> Also where the error is
}
function hover(e:Event):void{
mainEntrance_mc.buttonMode = true;
introMessage_mc.visible = true;
}
function hoverOff(e:Event):void{
introMessage_mc.visible = false;
}
What I'm trying to do here is move from room1 to room2, but I also have another concern. As you can see above each room has different buttons the player can click on, either to move to a different room, inspect an object, etc. I currently have these buttons sitting on the stage, but now that I will be navigating via layer visibility instead of frames how would I make it so the buttons in each room also appear/disappear with their respective rooms? I was thinking I could store each button in my library and call it to the stage like I am currently doing with my "messages", but I just wanted to see if there was an easy way to keep them on the stage in order to cut down on my code
Thanks again for all of your help, and if you have any tutorials in mind that would help me create this kind of point and click game that involves navigating through rooms that would also be super helpful!! I tried looking for some but I couldn't seem to find one that would suit my needs If you guys have ever played the game Deeper Sleep or something similar, that is the gameplay structure I am going for. Just being able to freely navigate through rooms while picking up or inspecting objects
Copy link to clipboard
Copied
ReferenceError: Error #1056: Cannot create property visible on String.
at Project2_fla::MainTimeline/showRoom()
at Project2_fla::MainTimeline/enterHospital()
var rooms:Array = ["room1","room2","room3","room4","room5","room6","room7","room8",
"room9","room10","room11","room12"];
Gettting rid of the error:
change the function to:
function showRoom(_index:int):void{
for (var i:int = 0;i<rooms.length;i++){
if(i==_index){
getChildByName(rooms).visible = true;
}
else{
getChildByName(rooms).visible = false;
}
}
}
Copy link to clipboard
Copied
I think you're making this way too complicated.
Give each of your buttons a base Class like this:
public class EventButton extends SimpleButton {
public function EventButton() {
super();
addEventListener(MouseEvent.CLICK, dispatchCustomEvent);
buttonMode=true;
}
protected function dispatchCustomEvent(e:MouseEvent):void {
dispatchEvent(new Event(name, true));
}
}
This will have the effect of allowing each button to dispatch an event that's the same as its name when it is clicked.
Now, you just have to listen to the main timeline for these named events and you don't need to care whether any particular button exists or not. So:
addEventListener('mainFloor_mc', goToReception);//you probably want to now rename that button, but you get the idea.
Note that by just defining your buttons as Buttons in the library/on stage, you can just draw in your over and down states rather than having to bother with all that code for your hover states, etc. I've assumed you're going to do this by using SimpleButton as the base Class above. If you like doing all that extra work, then by all means extend Sprite or MovieClip instead.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now