Skip to main content
Known Participant
February 22, 2014
Question

Accessing the stage from a class

  • February 22, 2014
  • 1 reply
  • 1142 views

Hello. I have my character on the stage and the code controlling my character in the document class. In the document class i have a function that dynamically spawns platforms that come down the screen by loading my Platforms.as which has all the code for the random spawning locations and whatnot. How can i create a collision detection between these dynamic platforms and my character?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
February 22, 2014

If you already have access to the character in the document class, and you create the platforms in the document class, then all you should need to is is continuously execute character.hitTestObject(platforms).  You can place the platforms in an array and loop thru the array to check when a collision occurs.

Known Participant
February 22, 2014

thankyou for your reply. ok so ive put my platform code into my document class but it doesnt work because i had a function in the document class to call them to the stage it went like this

var enemy = new Platforms();

stage.addChildAt(enemy, 0);

that would call the platforms class and reference it as enemy then spawn an enemy every time the function was called. how do i do this within the class?

Ned Murphy
Legend
February 23, 2014

Alright thanks that makes sence but im kinda new to this. How would i implement this in my document class? at the moment it just spawns one platform and it doesnt move.

package 

{

 

          import flash.display.MovieClip;

          import flash.events.MouseEvent;

 

 

          public class OddDog extends MovieClip

          {

                                        static var PlatformTimer:Timer;

                                        var goRight:Boolean = true;

                                        var goLeft:Boolean = false;

                                        var speed:Number;

                                        static var list:Array = new Array();

                                        var shootTimer:Timer;

 

                              public function OddDog()

                              {

                                        //platform.visible = false;

                                        changer.visible = false;

                                        char.visible = false;

                                        tap.visible = false;

                                        startbutton.addEventListener(MouseEvent.CLICK, startButton);

                                        tap.addEventListener(MouseEvent.CLICK, tapevent);

                                        char.addEventListener(Event.ENTER_FRAME , oneventfunction);

                                        changer.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);

                              function movetimerfunction(eventObject:Event):void

                              {

                                                  if(hitreg.visible == false && goLeft == true) char.x +=15;

                                                   if(hitreg.visible == false && goRight == true) char.x -=15;

                              }

                              function Platforms()

                              {

                              speed = -10

                              list.push(this);

                              this.y = 0;

                              this.x = Math.random() * 340 + 150

                              addEventListener("enterFrame", enterFrame);

                              var interval:Number = 50;

                              shootTimer = new Timer(interval);

                              shootTimer.start();

                              }

                              function enterFrame(e:Event)

                              {

                              this.y -=speed;

                              if (this.y < -100)

                              {

                                        kill();

                                        return;

                              }

 

                              if (this.hitTestObject(char))

                              {

                                        char.gotoAndStop('right')

                              }

                                        function kill()

                              {

                              removeEventListener("enterFrame", enterFrame);

                              stage.removeChild(this);

                              for (var i in list)

                              {

                                                  if (list == this)

                                                  {

                                                            delete list;

                                                  }

                                        }

                              }

                    }

                    function mousedown(e:MouseEvent):void

                              {

                                                  if (goRight == true && goLeft == false)

                              {

                                                  goRight = false;

                                                  goLeft = true;

                              }

                              else

                              {

                                                  mousedown2(null);

                              }

                              }

                              function mousedown2(e:MouseEvent):void

                              {

                                                  if (goRight == false && goLeft == true)

                              {

                                                  goRight = true;

                                                  goLeft = false;

                              }

                              }

                              function hittestright(e:Event):void

                              {

                                                    if(char.hitTestObject(hitregright))

                           {

                                                     char.gotoAndStop('left');

                                        goLeft = false;

                                                     goRight = false;

                           }

                              }

                              function hittestleft(e:Event):void

                              {

                                           if(char.hitTestObject(hitregleft))

                           {

                                           char.gotoAndStop('right');

                               goLeft = false;

                                           goRight = false;

                           }

                              }

                              function oneventfunction(e:Event):void

                              {

                                                  movetimerfunction(null);

                                                  hittestright(null);

                                                  hittestleft(null);

                              }

                              }

                              private function startButton(event:MouseEvent):void

                              {

                                                  char.gotoAndStop('idle');

                                                  char.visible = true;

                                                  tap.visible = true;

                                                  startbutton.visible = false;

                                                  odddogsplash.visible = false;

                              }

                              private function tapevent(event:MouseEvent):void

                              {

                                                  PlatformTimer = new Timer(1500);

                                                  PlatformTimer.addEventListener("timer", sendEnemy);

                                                  PlatformTimer.start();

                                                  hitreg.visible = false;

                                                  changer.visible = true;

                                                  tap.visible = false;

                    }

                    function sendEnemy(e:Event)

                    {

                              var enemy = new Platforms();

                              Platforms(null);

                              stage.addChildAt(enemy, 0);

                    }

          }

 

}


Can you clarify some things for me... does your Platforms class create a single instance of a Platforms (one platform thingy)?  I get the impression that your timer is creating a new one of them every 1.5 seconds based on the code you show just above between your tapevent and sendEnemy functions.

If so, is each Platforms (enemy) object what you want to perform a hitTest against the character?  

Just in case it matters, when you use addChildAt(enemy, 0) you are sending the newly created enemy object to the bottom of the disolay list, meaning it will be sitting behind any other content that you have on the stage... just in case you expect to see more of them.  If these enemies are not moving and they are supposed to be, they are likely stacking up one atop the other and appear to be just one.