8 Replies - 1873 Views - Last Post: 15 February 2009 - 07:34 AM

#1 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Linking external class AS with timeline AS

Post icon  Posted 13 February 2009 - 07:09 PM

Hey,
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

Is This A Good Question/Topic? 0
  • +

Replies To: Linking external class AS with timeline AS

#2 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 06:23 AM

From the lack of replies I'm sensing that I'm going about this completely the wrong way?
I just want multiple instances of the ground rectangle, each with the same hit test so that the character stops moving when it hits any section of the ground.
I don't want to repeat the same hittest code over and over for each rectangle, how would I go about this?
Was This Post Helpful? 0
  • +
  • -

#3 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 07:29 AM

When I say rectange, this is just as a test, I will eventually be using library MCs as graphics. So I can't use the draw graphics in ActionScript.
I have 3 different "ground" movie clips, I want them all to do the same hittest against the character.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,143
  • Joined: 07-September 06

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 11:28 AM

Okay, sorry about the time it took for someone to reply to you.

Basically, classes in AS aren't allowed to access the main stage. So, if you want the class to be able to interact with what is happening on the stage you have to pass everything in as a variable.

package {
  import flash.display.MovieClip;
  import flash.events.Event;
  public class Ground extends MovieClip Event{
	private obj:MovieClip;
	public function Ground(mc:MovieClip){
	  obj = mc;
	  stage.addEventListener(Event.ENTER_FRAME, hitCheck);
	}
	public function hitCheck(event:Event):void{
	  if (obj.hitTestObject(this)){
		obj.y = this.y - (this.height*1.4);
		jump = false;
		falling = false;
		velocity = 20;
	  }
	  else if (!obj.hitTestObject(this) && !jump){
		falling = true;
		jump = false;
		obj.y += 16;
	  }
	}
  }
}



It will need to look a little like so, though I don't think that will work without a little work.

Also, you will have to import the package into your main fla file.

That can be done like so:
import Ground;


That will import the file Ground which is in the same folder as the main movie.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#5 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 02:29 PM

Firstly, thank you for taking the time to help me out. I'm perhaps a little over my head but im learning! :)

Yes I understand that there were some issues in accessing the stage objects so I've put all the AS which controls the ball and holds the vars in the document class "Ball.as".

I've declared the variable " _ball_mc" to store my ball MovieClip.

And I've defined the variable in the constructor method, however I presume the path is now wrong, as we're no longer accessing a stage object with an instance name, we need to access the Ball class?

_ball_mc = parent.getChildByName("ball_mc") as MovieClip;


What would be the correct version of the above code?

So when the hittest runs in the other class "Ground.as" tries to change a bunch of vars in the document class "Ball.as", but an error says that they are undefined.

How can I get the class "Ground.as" to adjust these vars in the document class "Ball.as"?

Thanks again for your help, its MASSIVELY appreciated!

Here's the code for the document class "Ball.as"


package
{
	import flash.ui.Keyboard;
	import flash.events.*;
 import flash.display.*;

 
 public class Ball extends MovieClip
 {  
  public var leftDown:Boolean = false;
  public var rightDown:Boolean = false;
  public var jump:Boolean = false;
  public var velocity:Number = 20;
  public var falling:Boolean = false;
  public var speed:int = 5;
 
  public function Ball()
  {   
   stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
   stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
   stage.addEventListener(Event.ENTER_FRAME, moveChar);
  }
  
  public 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;
   }
  }
   
  public function upDetect(event:KeyboardEvent):void 
  {
   if(event.keyCode == Keyboard.LEFT) 
   {
	leftDown = false;
   }
   if(event.keyCode == Keyboard.RIGHT) 
   {
	rightDown = false;
   }
  }
  
  public function moveChar(event:Event):void
  {
   if (leftDown) 
   {
	this.x -= speed;
   }
   if (rightDown) 
   {
	this.x += speed;
   }
   if (jump) 
   {
	this.y -= velocity;
	velocity -= 1.5;
   }
  }
 }
}




And here is the other class "Ground.as":


package 
{
  import flash.display.MovieClip;
  import flash.events.Event;
  
  public class Ground extends MovieClip
  {
	private var _ball_mc:MovieClip;
 
	public function Ground()
 {
	  _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
	  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;
	  }
	}
  }
}





I have 2 movieclips on the stage, ball and ground. Both are linked to their respective classes.
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,143
  • Joined: 07-September 06

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 04:53 PM

Okay, I believe you will want this:
_ball_mc = parent.getChildByName("ball_mc") as MovieClip


to read:
_ball_mc = parent.getChildByName("ball_mc") as Ball


Then later on you are having issues with editing the variables. This is because classes whose functions use variables search for the variables first in the function, then in the class. The problem here is that neither the function hitCheck nor Ground have the variable jump, falling etc.

So, you have to tell it where to look for the variable, like so:
public function hitCheck(event:Event):void{
  if (_ball_mc.hitTestObject(this)){
	_ball_mc.y = this.y - (this.height*1.4);
	_ball_mc.jump = false;
	_ball_mc.falling = false;
	_ball_mc.velocity = 20;
  }
  else if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump){
	_ball_mc.falling = true;
	_ball_mc.jump = false;
	_ball_mc.y += 16;
  }
}



That way it knows that the variables are located in _ball_mc and not Ground.

Again, I haven't tested this code so it may not be perfect, but it should be closer.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#7 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 05:39 PM

Thanks buddy, almost there, I think!

Everything you suggested worked perfectly. However, it's having some problems with the ENTERFRAME hittest. When I run the movie I get the following error repeating constantly while the ENTERFRAME runs:

Type Error: Error #1009: Cannot access a property or method of a null object reference. at Ground/hitcheck()

So something is wrong with my hitcheck function, but I can't see what?! Is the addEventListener in the wrong place?

Thanks again for your help, your a star!

Updated Ground.as:

package 
{
  import flash.display.MovieClip;
  import flash.events.Event;
 
  public class Ground extends MovieClip
  {
	private var _ball_mc:MovieClip;
 
	public function Ground()
 {
	  _ball_mc = parent.getChildByName("ball_mc") as Ball;
	  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);
		_ball_mc.jump = false;
		_ball_mc.falling = false;
		_ball_mc.velocity = 20;
	  }
 
	  else if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
   {
	   _ball_mc.falling = true;
	   _ball_mc.jump = false;
	   _ball_mc.y += 16;
	  }
 
	}
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,143
  • Joined: 07-September 06

Re: Linking external class AS with timeline AS

Posted 14 February 2009 - 06:35 PM

Okay, the problem is most likely in this line:

stage.addEventListener(Event.ENTER_FRAME, hitCheck);



So, try the following and see if one works:
stage.addEventListener(Event.ENTER_FRAME, this.hitCheck);



or
addEventListener(Event.ENTER_FRAME, this.hitCheck);



or
addEventListener(Event.ENTER_FRAME, hitCheck);



Hope that works for you :)
Was This Post Helpful? 0
  • +
  • -

#9 ryrocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-February 09

Re: Linking external class AS with timeline AS

Posted 15 February 2009 - 07:34 AM

Unfortunately that didn't work :(
They all returned the same error.
i tried removing that line causing the problems
stage.addEventListener(Event.ENTER_FRAME, hitCheck);

and replaced it with
trace(_ball_mc);

The output returns "null".My guess is that the ground object is instantiating on the stage before the ball object.
Do you know of a way around this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1