Skip to main content
Participant
June 17, 2014
Answered

Gravity in as2

  • June 17, 2014
  • 1 reply
  • 443 views

Hey I'm new at AS, and i have been trying to make an object (a ball) fall within gravity but nothing happens when i'm running the swf.

here's the code:

onClipEvent (load) {

    fallMax = 0;

}

onClipEvent (enterFrame) {

    if (ball._y < 300)

    {

        _y += fallMax;

        if (fallMax < 10)

        {

            fallmax++;

        }

    }

}

what am i doing wrong?

This topic has been closed for replies.
Correct answer kglad

you probably have some scope problems.  but rather than correct those, you should remove all code from objects and attach code to timelines:

var fallMax:Number=0;

this.onEnterFrame=gameloopF;

function gameloopF():Void{

if(ball._y<300){

ball._y+=fallMax

if(fallMax<10){

fallMax++;

}

} else {

delete this.onEnterFrame;

}

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 17, 2014

you probably have some scope problems.  but rather than correct those, you should remove all code from objects and attach code to timelines:

var fallMax:Number=0;

this.onEnterFrame=gameloopF;

function gameloopF():Void{

if(ball._y<300){

ball._y+=fallMax

if(fallMax<10){

fallMax++;

}

} else {

delete this.onEnterFrame;

}

}

Elon GAuthor
Participant
June 18, 2014

Thank you

kglad
Community Expert
Community Expert
June 18, 2014

you're welcome.