Advertisement

AS3. Inventory function can't be called in a Scene

Started by November 17, 2015 05:45 AM
6 comments, last by jeteran 9 years, 1 month ago
Greetings.
I am going through a really head pain because of an error I am unable to resolve and, after many trials and errors, I came here to look for help.
The game logic is like this.
. The game is a mystery game, which you are in a basement and you have to find a key in order to go out. Once you have it, you go to the next room: the corridor.
. Once in the corridor, you can either go left to the library or right to enter a room.
. The next object (a code) is in a mask in the room. Once you pick it up, you go to the library, open a safe box and pick up a door knob.
. The exit door is in the corridor. You use the door knob to exit.
Now, this is the logic of the inventory.
. The inventory is a movie clip which has 4 frames / states, the first one it's empty, the second shows a key, the third shows the key and a code and the fourth shows the key, code and a door knob.
. You start with an empty inventory.
. I created a function that reads something like this:
function checkInventory()
{
if (keyTaken)
{
inventory.gotoAndStop(2);
}
if (keyTaken && codeTaken)
{
inventory.gotoAndStop(3);
}
if (keyTaken && codeTaken && doorknobTaken)
{
inventory.gotoAndStop(4);
}
}
Simple.
. I checkInventory() when the player picks up an object.
. I checkInventory() everytime I enter a new scene.
. Now, when I am in the basement, and I want to go to the corridor, it works perfectly. The same happens when I go to the library. And even if I go back to the basement.
. But when I enter the room, I receive an error that says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at KikisEscape_fla::MainTimeline/checkInventory()
at KikisEscape_fla::MainTimeline/frame5()
at flash.display::MovieClip/gotoAndStop()
at KikisEscape_fla::MainTimeline/corridorRightDoorClick()
. I double check everything and there is no way this error has to occur. And it does only in this room.
. Looks like the problem is when I click the right door of the corridor, but it is simply a: gotoAndStop(1, "Room"); The same way is used in the other scenes.
What can this be? What am I missing?
Thanks you for any advice you can share with me!

You are trying to execute a function of a none existing object in as. In Flash CC you can start a debug session with ctrl+shift+enter, when this error occurs it should stop at that line of the code, either in the AS file or on the actions tab for a particular frame.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Advertisement
Hey buddy, thanks for your response.
Indeed, when I debug the game, stops in that part, and selects the line, inside function checkInventory():
-> inventory.gotoAndStop(2);
I still don't understand why is stopping in that position when in the other rooms the function works perfectly.
I revisited the whole Room code, even I delete it all the code and just called the function, and nothing.
I checked also the door that leads to the Room, but it is coded the same as the other doors of the house.
Any other piece of advice?
Thanks a ton!

Hi JETeran,

Can you add your code for the inventory object?

GJ

Hello guys.

I found the solution! It was there the whole time!

What happened is that the inventory inside the stage didn't have an instance name (duh!), I put "inventory" and now the function can be read properly.

About the function, I have to optimize it because it was appear only the first object. I post here the result of the function in case someone else needs it:

function inventoryObjects()
{
if (keyTaken == true)
{
inventory.gotoAndStop(2);
}
if (keyTaken == true && codeTaken == true)
{
inventory.gotoAndStop(3);
}
if (keyTaken == true && codeTaken == true && doorknobTaken == true)
{
inventory.gotoAndStop(4);
}
}
Thanks everyone!

Generally this means that you have no stage object for that frame to call the function on, make sure the inventory object exists when you take the last room thing were you are having an issue.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Advertisement

That is totally correct. I bypass the name of the object in the stage. When I wrote its name (inventory) everything ran excellent.

Thanks for your contribution!

That is totally correct. I bypass the name of the object in the stage. When I wrote its name (inventory) everything ran excellent.

Thanks for your contribution!

This topic is closed to new replies.

Advertisement