Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
Hi everyone.

Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

This error is probably the biggest problem on my way into haxe.

I understand that static variables shouldn't access non-static members, but my class doesn't have any static methods and I still get such error.

Can someone help me to understand the reason of these errors?

Here is the problem class Enemy (also available at http://pastie.org/2218734 ):
------
class Enemy
{
        public var clip: MovieClip;
        public var speed: Float;
        public var direction: Float;
       
        public function new(x:Float, y:Float, direction:Float, size:Float, speed:Float)
        {
                this.clip = Lib.attach("enemy");
                this.speed = 10;
                this.direction = 0;
               
                flash.Lib.current.addChild(clip);
                Lib.current.addEventListener(Event.ENTER_FRAME, function () move());
                clip.x = x;
                clip.y = y;
                clip.width *= size;
                clip.height *= size;
               
                this.direction = direction;
                clip.rotationZ = direction * 180 / 3.14;
               
                this.speed *= speed;
               
        }
       
        function move()
        {
                clip.x += Math.cos(direction) * speed;
                clip.y += Math.sin(direction) * speed;
        }
}
------




After compiling with flash10 target on Win XP with haxe 2.07 I get following errors:
------
src/tilo/Enemy.hx:25: characters 62-66 : Cannot access move in static function
src/tilo/Enemy.hx:26: characters 2-8 : Cannot access clip in static function
src/tilo/Enemy.hx:27: characters 2-8 : Cannot access clip in static function
src/tilo/Enemy.hx:28: characters 2-12 : Cannot access clip in static function
src/tilo/Enemy.hx:29: characters 2-13 : Cannot access clip in static function
src/tilo/Enemy.hx:31: characters 2-16 : Cannot access this from a static function
src/tilo/Enemy.hx:32: characters 2-16 : Cannot access clip in static function
src/tilo/Enemy.hx:34: characters 2-12 : Cannot access this from a static function
------

Best Regards,
Artyom S.

Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

alexander.konotop
В Fri, 15 Jul 2011 10:04:40 -0700 (PDT)
Artyom Silivonchik <[hidden email]> пишет:

> Hi everyone.
>
> Why do a class without static methods or variables can return an error
> "Cannot access VAR_NAME in static function"?
>
> This error is probably the biggest problem on my way into haxe.
>
> I understand that static variables shouldn't access non-static
> members, but my class doesn't have any static methods and I still get
> such error.
>
> Can someone help me to understand the reason of these errors?
>
> Here is the problem class Enemy (also available at
> http://pastie.org/2218734 ):
> ------
> class Enemy
> {
> public var clip: MovieClip;
> public var speed: Float;
> public var direction: Float;
>
> public function new(x:Float, y:Float, direction:Float,
> size:Float, speed:Float)
> {
> this.clip = Lib.attach("enemy");
> this.speed = 10;
> this.direction = 0;
>
> flash.Lib.current.addChild(clip);
> Lib.current.addEventListener(Event.ENTER_FRAME,
> function () move()); clip.x = x;
> clip.y = y;
> clip.width *= size;
> clip.height *= size;
>
> this.direction = direction;
> clip.rotationZ = direction * 180 / 3.14;
>
> this.speed *= speed;
>
> }
>
> function move()
> {
> clip.x += Math.cos(direction) * speed;
> clip.y += Math.sin(direction) * speed;
> }
> }
> ------
>
>
>
>
> After compiling with flash10 target on Win XP with haxe 2.07 I get
> following errors:
> ------
> src/tilo/Enemy.hx:25: characters 62-66 : Cannot access move in static
> function
> src/tilo/Enemy.hx:26: characters 2-8 : Cannot access clip in static
> function src/tilo/Enemy.hx:27: characters 2-8 : Cannot access clip in
> static function src/tilo/Enemy.hx:28: characters 2-12 : Cannot access
> clip in static function
> src/tilo/Enemy.hx:29: characters 2-13 : Cannot access clip in static
> function
> src/tilo/Enemy.hx:31: characters 2-16 : Cannot access this from a
> static function
> src/tilo/Enemy.hx:32: characters 2-16 : Cannot access clip in static
> function
> src/tilo/Enemy.hx:34: characters 2-12 : Cannot access this from a
> static function
> ------
>
> Best Regards,
> Artyom S.
>
>
>
> --
> View this message in context:
> http://haxe.1354130.n2.nabble.com/Why-do-a-class-without-static-methods-or-variables-can-return-an-error-Cannot-access-VAR-NAME-in-sta-tp6587518p6587518.html
> Sent from the Haxe mailing list archive at Nabble.com.
>

Are You sure about this?:
Lib.current.addEventListener(Event.ENTER_FRAME, function () move());
Maybe just this way?:
Lib.current.addEventListener(Event.ENTER_FRAME, move);
or:
Lib.current.addEventListener(Event.ENTER_FRAME, this.move);

P.S. Maybe I'm wrong about what I wrote above but addEventListener is
definitely a static function. I think, You have to think further
keeping this info in mind and You'll find a solution - it looks like
You have some kind of OOP brain-exploding issue :-).

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Jan_Flanders
In reply to this post by Artyom Silivonchik


On Fri, Jul 15, 2011 at 7:04 PM, Artyom Silivonchik <[hidden email]> wrote:
Hi everyone.

Why do a class without static methods or variables can return an error
"Cannot access VAR_NAME in static function"?

This error is probably the biggest problem on my way into haxe.

var me = this;
Lib.current.addEventListener(Event.ENTER_FRAME, function (event:Event) me.move());

Jan

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
In reply to this post by alexander.konotop
Alexander Konotop wrote
Are You sure about this?:
Lib.current.addEventListener(Event.ENTER_FRAME, function () move());
Maybe just this way?:
Lib.current.addEventListener(Event.ENTER_FRAME, move);
or:
Lib.current.addEventListener(Event.ENTER_FRAME, this.move);

P.S. Maybe I'm wrong about what I wrote above but addEventListener is
definitely a static function. I think, You have to think further
keeping this info in mind and You'll find a solution - it looks like
You have some kind of OOP brain-exploding issue :-).
Your solution is not working either. :(

There is a working solution in the next reply.
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
In reply to this post by Jan_Flanders

Jan_Flanders wrote:

>
> var me = this;
> Lib.current.addEventListener(Event.ENTER_FRAME, function (event:Event)
> me.move());
>
> Jan
>
> --
> haXe - an open source web programming language
> http://haxe.org
>


--
View this message in context: http://haxe.1354130.n2.nabble.com/Why-do-a-class-without-static-methods-or-variables-can-return-an-error-Cannot-access-VAR-NAME-in-sta-tp6587518p6587774.html
Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
In reply to this post by Jan_Flanders
Jan_Flanders wrote
var me = this;
Lib.current.addEventListener(Event.ENTER_FRAME, function (event:Event)
me.move());

Jan

--
haXe - an open source web programming language
http://haxe.org
Thanks Jan. It's working.

Can you explain why do I need to define a separate Var for "this" object? instead of direct call this.move, please?

It's too confusing for me, and seems illogical.

/Artyom S.
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Baluta Cristian
after you change the listener as above you need this change too:

function move(e:Event)
       {
               clip.x += Math.cos(direction) * speed;
               clip.y += Math.sin(direction) * speed;
       }


On Fri, Jul 15, 2011 at 9:43 PM, Artyom Silivonchik <[hidden email]> wrote:

Jan_Flanders wrote:
>
> var me = this;
> Lib.current.addEventListener(Event.ENTER_FRAME, function (event:Event)
> me.move());
>
> Jan
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

Thanks Jan. It's working.

Can you explain why do I need to define a separate Var for "this" object?
instead of direct call this.move, please?

It's too confusing for me, and seems illogical.

/Artyom S.

--
View this message in context: http://haxe.1354130.n2.nabble.com/Why-do-a-class-without-static-methods-or-variables-can-return-an-error-Cannot-access-VAR-NAME-in-sta-tp6587518p6587787.html
Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org



--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Baluta Cristian
I was referring to this one: Lib.current.addEventListener(Event.ENTER_FRAME, move);

Btw, why did you approximated the PI? You can use it from Math.PI


On Sat, Jul 16, 2011 at 4:48 AM, Baluta Cristian <[hidden email]> wrote:
after you change the listener as above you need this change too:

function move(e:Event)

       {
               clip.x += Math.cos(direction) * speed;
               clip.y += Math.sin(direction) * speed;
       }


On Fri, Jul 15, 2011 at 9:43 PM, Artyom Silivonchik <[hidden email]> wrote:

Jan_Flanders wrote:
>
> var me = this;
> Lib.current.addEventListener(Event.ENTER_FRAME, function (event:Event)
> me.move());
>
> Jan
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

Thanks Jan. It's working.

Can you explain why do I need to define a separate Var for "this" object?
instead of direct call this.move, please?

It's too confusing for me, and seems illogical.

/Artyom S.

--
View this message in context: http://haxe.1354130.n2.nabble.com/Why-do-a-class-without-static-methods-or-variables-can-return-an-error-Cannot-access-VAR-NAME-in-sta-tp6587518p6587787.html
Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org



--
Băluță Cristian
http://ralcr.com
http://imagin.ro



--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
In reply to this post by Baluta Cristian
Baluta Cristian wrote
after you change the listener as above you need this change too:

function move(e:Event)
       {
               clip.x += Math.cos(direction) * speed;
               clip.y += Math.sin(direction) * speed;
       }


--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
Thanks a million. That's what I was missing.

Finally we have a working and logical solution.

Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Artyom Silivonchik
In reply to this post by Baluta Cristian
Baluta Cristian wrote
I was referring to this one: Lib.current.addEventListener(Event.ENTER_FRAME,
move);

Btw, why did you approximated the PI? You can use it from Math.PI

--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
This approximation was suitable for my needs, but you are right - it will be better to use Math.PI anyway (since it won't lead to defects in future). Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Why do a class without static methods or variables can return an error "Cannot access VAR_NAME in static function"?

Yanis Benson

To clarify why originally code wasn't working: the anonymous function
function(_) move()
is the static function compiler is complaining about. All anonymous functions are static, actually, and don't have an idea about your this. By doing
    var me = this;
and using
    me.move()
you push this instance to it's scope(as a variable of course).

On 16 Jul 2011 09:39, "Artyom Silivonchik" <[hidden email]> wrote:
>
> Baluta Cristian wrote:
>>
>> I was referring to this one:
>> Lib.current.addEventListener(Event.ENTER_FRAME,
>> move);
>>
>> Btw, why did you approximated the PI? You can use it from Math.PI
>>
>> --
>> Băluță Cristian
>> http://ralcr.com
>> http://imagin.ro
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>
> This approximation was suitable for my needs, but you are right - it will be
> better to use Math.PI anyway (since it won't lead to defects in future).
> Thanks.
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Why-do-a-class-without-static-methods-or-variables-can-return-an-error-Cannot-access-VAR-NAME-in-sta-tp6587518p6588892.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> haXe - an open source web programming language
> http://haxe.org

--
haXe - an open source web programming language
http://haxe.org