2

I am making an edutainment game using flash cs5, I'm really new at using flash, in fact we never yet tackle it at school, but I insist on learning about it.

In my codes, I encountered this error

C:\Users\acer\Desktop\JikanLibrary\Main.as, Line 16 1119: Access of possibly undefined property Click through a reference with static type Class.

This is the code I used in my program

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Main extends MovieClip
{
    var startPage:StartPage;
    var jikanBookshelf:JikanBookshelf;

    public function Main()
    {
        startPage = new StartPage;
        jikanBookshelf = new JikanBookshelf;

        startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);

        addChild(startPage);

        function onJikanBookshelf(event:MouseEvent):void
        {
            addChild(jikanBookshelf);
            removeChild(startPage);
        }


    }
}
}

The error is in this line

startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);

Since I'm really new at flash, I really don't know what went wrong with my codes, it was working a while ago before I put the mouse event. I hope someone could help me.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
MisaChan
  • 279
  • 1
  • 5
  • 15

2 Answers2

3

ActionScript is a case sensitive language. This means that Click is not the same as CLICK. So what you need here is MouseEvent.CLICK

"Why is CLICK all uppercase? Most property names aren't.", you might ask.

That's because CLICK is a static constant property of MouseEvent and the convention amongst ActionScript (and many other languages) programmers is that static constants are written in all uppercase to distinguish them visually from other variables.

  • 'static' means it's a property of the MouseEvent class, not of an instance of MouseEvent.
  • 'const' means it's not a variable: you can't change it's value.
RIAstar
  • 11,912
  • 20
  • 37
  • BTW: `new StartPage` is not going to work either. It should be `new StartPage()`. The constructor is a function too. – RIAstar Aug 21 '11 at 17:38
  • 1
    actually `new StartPage` will work. I used to think the same thing to but I recently learnt that a class with a non parameterized constructor in AS3 can be instantiated without the parentheses, not something I would personally do though. – Taurayi Aug 21 '11 at 20:23
  • my new startStartpage is working, the said line is my only problem, i tried to change the click to CLICK and the error became like this __TypeError: Error #1010: A term is undefined and has no properties. at Main()[I:\JikanLibrary\Main.as:16]__ – MisaChan Aug 22 '11 at 04:54
  • @MisaChan Now that would be the `onJikanBookshelf` function that isn't defined yet when you try to use it. Either move it to class level (out of the Main constructor: `private function onJikanBookShelf(event:MouseEvent):void { ... }`), or define it as a variable of type `Function` before you use it (`var onJikanBookShelf:Function = function(event:MouseEvent):void { ... }`). The first option would be your best choice in this case. – RIAstar Aug 22 '11 at 08:56
  • @RIAstar,im so sorry,i didn't notice that i put the wrong name in __startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);__ the _jikanBookshelf__ in my library is the name assign for the bookshelf page,i realize in my reference that it should be the buttons name inserted __startPage.BtnBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);__ BtnBookshelf is the name of my button that would trigger it to go to the next page, when i changed it,it produced the same error,i already rechecked my library if i misspelled something but all variables are spelled right – MisaChan Aug 22 '11 at 12:44
  • Currently in your code you have onJikanBookshelf defined after you are calling so as such the function does not exist and thats why you get an undefined error. You have 3 chooses. 1) move function definition of onJikanBookshelf to the first line of the main fucntion. 2) Convert it to a nameless function that the eventlistener will call. 3)move it ouside of the main function and make it a method for the class. Since you are still in school and this is probably an assignment I would highly suggest you use option 3. Stick with true OOP methods so you wont run into scoping issues. – The_asMan Aug 22 '11 at 22:29
  • Oh yeah and +1 cause I doubt you get a green checky for this. – The_asMan Aug 22 '11 at 22:29
  • @The_asMan I thought the same thing, but actually the code will run (if there's no other error) because of 'block scoping'. Same reason why something like `a = "hello"; var a:String;` will not throw an error. --- and thanks – RIAstar Aug 22 '11 at 22:36
  • As long as the nested function is defined after the eventlistener there will always be an error because it doesnt exist yet. And the scoping hell that will come from this could potentially be well just that hell lol. Personally if I was the teacher I would drop points on the grade if I saw that. This is OOP after all and you should stay with OOP. But that's just me and I don't teach anymore lol – The_asMan Aug 22 '11 at 23:53
0

It's a name conflict problem: The class definition name is same as the object name.

The problem in your script is that you have a class definition name startPage and you are trying to create an object of same name startPage.

You have to change the object name to something different. Let say startpage1.

nalply
  • 26,770
  • 15
  • 78
  • 101
justnajm
  • 4,422
  • 6
  • 36
  • 56
  • Or change the class name `StartPage`. It's a convention to use uppercase first letter for classes and lowercase first letter for instances (objects). – nalply Oct 21 '12 at 10:17