Skip to main content
December 12, 2012
Question

Weird delay coming from class code.

  • December 12, 2012
  • 1 reply
  • 485 views

Hey there,

     I'm working on a game where sets of platforms come onto the screen one after another creating and infinite runner. I'm currently doing this, maybe not the most effective way but it hasn't cause any greif until now, by having two trigger MovieClips far off stage to the left. When hit by a platform, the first one is supposed to move the next platform into line and move it left. The second, placed just beyond the first, then sends the set of platforms off into the distance.

     The issue that I'm running in to is, the second set of platforms spawns in where it should be, but then takes a second to move which makes the game completely impossible as the first platform slides out from under you.

     Below is my code for these two triggers. Thank you in advance.

This is the code for the class that spawns the next set of platforms.

package {

          import flash.display.*;

          import flash.events.*;

 

 

          public class PlatformReset extends MovieClip{

                    public var prevX:Number;

                    public var prevY:Number;

 

                    //construct function

                    public function PlatformReset():void

                    {

                              addEventListener(Event.ENTER_FRAME, collision);

                    }

                    private function collision(e:Event):void{

 

                              if(this.hitTestObject(MovieClip(root).Platform1)){

 

 

 

                                        MovieClip(root).Platform2.x = 3200;

                                        MovieClip(root).Platform2.y = 300;

                                        MovieClip(root).Platform2.x -= MovieClip(root).worldSpeed;

                                        trace("Platform2 Spawn");

 

 

 

                              }

                              else {

                                        prevX = MovieClip(root).Platform2.x;

                                        prevY = MovieClip(root).Platform2.y;

 

                              }

 

                              if(this.hitTestObject(MovieClip(root).Platform2)){

 

 

 

                                        MovieClip(root).Platform1.x = 3200;

                                        MovieClip(root).Platform1.y = 300;

                                        MovieClip(root).Platform1.x -= MovieClip(root).worldSpeed;

                                        trace("Platform1 Spawn");

 

 

 

 

                              }

                              else {

                                        prevX = MovieClip(root).Platform1.x;

                                        prevY = MovieClip(root).Platform1.y;

 

                              }

 

 

                    }

          }

 

}

This is the class that then moves the first.

package {

          import flash.display.*;

          import flash.events.*;

 

 

          public class Platform2ndTrigger extends MovieClip{

                    public var prevX:Number;

                    public var prevY:Number;

 

 

                    //construct function

                    public function Platform2ndTrigger():void

                    {

                              addEventListener(Event.ENTER_FRAME, collision);

                    }

                    private function collision(e:Event):void{

 

                              if(this.hitTestObject(MovieClip(root).Platform1)){

 

 

 

                                        MovieClip(root).Platform1.x = 0;

                                        MovieClip(root).Platform1.y = 1200;

 

                                        //MovieClip(root).Platform1.stop();

                                        MovieClip(root).Platform2.x -= MovieClip(root).worldSpeed;

                                        trace("Platform1 Hit 2nd Trigger and Moved");

                                                    

                                                    

 

                              }

                              else {

                                        //prevX = MovieClip(root).Platform1.x;

                                        //prevY = MovieClip(root).Platform1.y;

 

                              }

 

                              if(this.hitTestObject(MovieClip(root).Platform2)){

 

 

 

                                        MovieClip(root).Platform2.x = 0;

                                        MovieClip(root).Platform2.y = 1200;

 

                                        //MovieClip(root).Platform2.stop();

                                        MovieClip(root).Platform1.x -= MovieClip(root).worldSpeed;

                                        trace("Platform2 Hit 2nd Trigger and Moved");

 

 

                              }

                              else {

                                        //prevX = MovieClip(root).Platform2.x;

                                        //prevY = MovieClip(root).Platform2.y;

 

                              }

 

 

                    }

          }

 

}

This topic has been closed for replies.

1 reply

sinious
Legend
December 12, 2012

Wrapping things in classes like this is a bit odd but that aside.

What exactly are the classes "PlatformReset" and "Platform2ndTrigger" applied to? The hittests are constantly referencing what they're applied to (e.g. this.hitTestObject).

December 12, 2012

These are applied to objects that have been added to the scene far to the left of the stage. Is there a better way to test for collisions with something like this?

sinious
Legend
December 12, 2012

You have 2 classes running hittests in ENTER_FRAME that both directly affect Platform1 and Platform2.

If you're putting one of those classes on the object you move from platform to platform (like a character) and the other on a platform then both hittests will fire off at the same time. That's going to cause havoc. The reason why is both classes are directly coupled to controlling Platform1 and Platform2. They will fight with each other to set position at a very fast rate (ENTER_FRAME speed).

I don't have the time now to code an example of how I'd go about this but what I can do is recommend you experiment with using only one class for hittest detection. Either let the object using the platforms detect hits or let the platforms detect hits. Not both. Especially when both directly control the same objects. That will reduce your ENTER_FRAME looping to just one object and also only one class is now accessing objects directly. That should help you get started.

The only change is the branching you'd need to do in your single ENTER_FRAME loop. You'd be testing hit detection against different objects at different times based on circumstances. As soon as the first platform is hit, you set Platform2 to be the target to hittest against, then Platform3, 4, 5, etc.