I've just started using classes while developing my basic platform game in Flash AS3.
On the stage I have a character, instance name 'ball_mc'. I have AS in the time line which controls the characters movements.
I have an external AS file named 'Ground.as' which is basically an ENTERFRAME hittest which stops the character from moving when the character 'hits' the ground.
I have placed a rectangle MC on the stage to represent the ground and linked it to the external AS file 'Ground.as'
The Code for Ground.as is as follows:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Ground extends MovieClip Event
{
public function Ground()
{
stage.addEventListener(Event.ENTER_FRAME, hitCheck);
public function hitCheck(event:Event):void
{
if (ball_mc.hitTestObject(this))
{
ball_mc.y = this.y - (this.height*1.4);
jump = false;
falling = false;
velocity = 20;
}
else if (!ball_mc.hitTestObject(this) && !jump)
{
falling = true;
jump = false;
ball_mc.y += 16;
}
}
}
}
}
When I test the movie I get a series of errors,telling me that the variables and objects are undefined. for example: "access of undefined property ball_mc".
This is because all the vars and ball_mc is declared in the timeline AS.
So, from the external AS file Ground.as, how do I relate to objects on the stage and AS coding in the timeline?
Here's my coding in the time line:
var leftDown:Boolean = false;
var rightDown:Boolean = false;
var jump:Boolean = false;
var velocity:Number = 20;
var falling:Boolean = true;
var speed:int = 5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
function downDetect(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
leftDown = true;
}
if(event.keyCode == Keyboard.RIGHT)
{
rightDown = true;
}
if(event.keyCode == Keyboard.UP && !falling)
{
jump = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
function upDetect(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
leftDown = false;
}
if(event.keyCode == Keyboard.RIGHT)
{
rightDown = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void
{
if (leftDown)
{
ball_mc.x -= speed;
}
if (rightDown)
{
ball_mc.x += speed;
}
if (jump)
{
ball_mc.y -= velocity;
velocity -= 1.5;
}
}
Thanks in advance,
Dave

New Topic/Question
Reply



MultiQuote




|