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
Braniac
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?

Known Participant
February 23, 2014

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.


Yes it calls a new one every 1.5 seconds and yes every platform i want to perform a hittest on the character because the platforms have gaps between them which the character must go through so i want it to be able to do something if one of these are hit. And no it doesnt matter if they are behind because they dont overlap and nothing is behind or infront of them, but is there a way i can put them on top so i can add a background texture? And when its in the one class only one appears and its always in the same spot and i dont think the randomiser for positions is working. probably because my sendEnemy function is still using the Platforms class. Im not sure if they are stacking actually because there is a transparent piece between it which isnt filled but the randomiser isnt working so they might be ontop of eachother. This is what my Platforms class contains at the moment. All the guts were moved to the document class.

package 

{

          import flash.display.DisplayObject;

          import flash.display.Stage;

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.utils.Timer;

 

          public class Platforms extends MovieClip

          {

          }

}

And no they are not moving at all