Andrey
Andrey

Reputation: 81

Actionscript 3 instance name in class is not working

I have set the instance name of MovieClip to char and when i try to compile this code i get 2 Errors:

package com.game
{

import flash.display.MovieClip;
import flash.events.*;


public class game extends MovieClip
{
    var gravity = 0.8;
    var velocity = 0;
    char.addEventListener(Event.ENTER_FRAME,isHitted);

    function isHitted(event:Event):void
    {
        if (char.hitTestObject(level1))
        {
            velocity++;
            char.y -= gravity+velocity;
        }
        else
        {

        }
    }

Errors:

.../game.as, Line 13    1120: Access of undefined property char.
../game.as, Line 13 1120: Access of undefined property isHitted.

Upvotes: 0

Views: 399

Answers (2)

al1p
al1p

Reputation: 23

You need to make 'char' accessible to 'game' before being able to use it.

One way is to pass 'char' as parameter when you instantiate 'game'.

Two ways of doing this are described in the answer of @lee-burrows in Access caller object when using composition in AS3

Upvotes: 0

Anil
Anil

Reputation: 2554

First off, it sounds like this class definition is the definition for the instance you are referring to, if it is, you should use 'this' instead of 'char'

Also, you typically do not specify method calls like:

char.addEventListener(Event.ENTER_FRAME,isHitted);

outside of methods when declaring a class. Instead, that line of code should exist inside of a constructor or a method that is called during the instantiation of the MovieClip.

Upvotes: 2

Related Questions