Linking external class AS with timeline AS

  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

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:

Code: [ Select ]
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;
}
}
}
}
}
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.events.Event;
  5. public class Ground extends MovieClip Event
  6. {
  7. public function Ground()
  8. {
  9. stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  10. public function hitCheck(event:Event):void
  11. {
  12. if (ball_mc.hitTestObject(this))
  13. {
  14. ball_mc.y = this.y - (this.height*1.4);
  15. jump = false;
  16. falling = false;
  17. velocity = 20;
  18. }
  19. else if (!ball_mc.hitTestObject(this) && !jump)
  20. {
  21. falling = true;
  22. jump = false;
  23. ball_mc.y += 16;
  24. }
  25. }
  26. }
  27. }
  28. }


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:

Code: [ Select ]
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;
}
}
  1. var leftDown:Boolean = false;
  2. var rightDown:Boolean = false;
  3. var jump:Boolean = false;
  4. var velocity:Number = 20;
  5. var falling:Boolean = true;
  6. var speed:int = 5;
  7. stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  8. function downDetect(event:KeyboardEvent):void
  9. {
  10. if(event.keyCode == Keyboard.LEFT)
  11. {
  12. leftDown = true;
  13. }
  14. if(event.keyCode == Keyboard.RIGHT)
  15. {
  16. rightDown = true;
  17. }
  18. if(event.keyCode == Keyboard.UP && !falling)
  19. {
  20. jump = true;
  21. }
  22. }
  23. stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
  24. function upDetect(event:KeyboardEvent):void
  25. {
  26. if(event.keyCode == Keyboard.LEFT)
  27. {
  28. leftDown = false;
  29. }
  30. if(event.keyCode == Keyboard.RIGHT)
  31. {
  32. rightDown = false;
  33. }
  34. }
  35. stage.addEventListener(Event.ENTER_FRAME, moveChar);
  36. function moveChar(event:Event):void
  37. {
  38. if (leftDown)
  39. {
  40. ball_mc.x -= speed;
  41. }
  42. if (rightDown)
  43. {
  44. ball_mc.x += speed;
  45. }
  46. if (jump)
  47. {
  48. ball_mc.y -= velocity;
  49. velocity -= 1.5;
  50. }
  51. }


Thanks in advance,

Dave
  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

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?
  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

My apologies if I haven't explained this very well. Let me start again (my approach has changed a lot since my initial post).

Im making a basic platform game
I have 3 movieclips on the stage:
-A circle which represents the character that the user controls
-2 rectangles which represent 2 different sections of the ground (both the same library movieclip)

My circle has the instance name of "ball_mc". The 2 ground movieclips dont have instance names because I will eventually have many more ground movieclips. I dont want to write a new chunk of code for each ground movieclip, I just want to link them all to the same class.

There is no AS on the stage. I have a document class named "Ball.as" this contains all the variables and functions which control the character. My circle movieclip links to this class.

My ground movieclip links to the "Ground.as" class. This class contains a hittest. Basically, "when ball hits this object, ball stops moving."
So this "Ground.as" class needs to be able to adjust variables in the "Ball.as" document class. So the vars need to be public.

Here's my code:
Ball.as document class:
Code: [ Select ]
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;
            }
        }
        
    }
}
  1. package
  2. {
  3.     import flash.ui.Keyboard;
  4.     import flash.events.*;
  5.     import flash.display.*;
  6.     public class Ball extends MovieClip
  7.     {     
  8.         public var leftDown:Boolean = false;
  9.         public var rightDown:Boolean = false;
  10.         public var jump:Boolean = false;
  11.         public var velocity:Number = 20;
  12.         public var falling:Boolean = false;
  13.         public var speed:int = 5;
  14.     
  15.         public function Ball()
  16.         {         
  17.             stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  18.             stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
  19.             stage.addEventListener(Event.ENTER_FRAME, moveChar);
  20.         }
  21.         
  22.         public function downDetect(event:KeyboardEvent):void
  23.         {
  24.             if(event.keyCode == Keyboard.LEFT)
  25.             {
  26.                 leftDown = true;
  27.             }
  28.             if(event.keyCode == Keyboard.RIGHT)
  29.             {
  30.                 rightDown = true;
  31.             }
  32.             if(event.keyCode == Keyboard.UP && !falling)
  33.             {
  34.                 jump = true;
  35.             }
  36.         }
  37.         
  38.         
  39.         public function upDetect(event:KeyboardEvent):void
  40.         {
  41.             if(event.keyCode == Keyboard.LEFT)
  42.             {
  43.                 leftDown = false;
  44.             }
  45.             if(event.keyCode == Keyboard.RIGHT)
  46.             {
  47.                 rightDown = false;
  48.             }
  49.         }
  50.         
  51.         public function moveChar(event:Event):void
  52.         {
  53.             if (leftDown)
  54.             {
  55.                 this.x -= speed;
  56.             }
  57.             if (rightDown)
  58.             {
  59.                 this.x += speed;
  60.             }
  61.             if (jump)
  62.             {
  63.                 this.y -= velocity;
  64.                 velocity -= 1.5;
  65.             }
  66.         }
  67.         
  68.     }
  69. }


Ground.as:
Code: [ Select ]
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);
        _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;
     }
    
    }
}
}
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.events.Event;
  5. public class Ground extends MovieClip
  6. {
  7.     private var _ball_mc:MovieClip;
  8.     
  9.     public function Ground()
  10.     {
  11.      _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  12.      stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  13.     }
  14.     
  15.     public function hitCheck(event:Event):void
  16.     {
  17.      if (_ball_mc.hitTestObject(this))
  18.      {
  19.         _ball_mc.y = this.y - (this.height*1.4);
  20.         _ball_mc.jump = false;
  21.         _ball_mc.falling = false;
  22.         _ball_mc.velocity = 20;
  23.      }
  24.     
  25.      else if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
  26.      {
  27.      _ball_mc.falling = true;
  28.      _ball_mc.jump = false;
  29.      _ball_mc.y += 16;
  30.      }
  31.     
  32.     }
  33. }
  34. }


As things stand now I am having trouble with my hittest. 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()

I tried removing the hittest causing the problems: "stage.addEventListener(Event.ENTER_FRAME, hitCheck);" And I 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?

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.