00:00
00:00
Newgrounds Background Image Theme

ReneePrower just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 object warp-on-collide onlyonce

980 Views | 2 Replies
New Topic Respond to this Topic

In my top-down as3 game, collision between the player and a warp point works once, changing the background and moving the player, and then back again on colliding with the exit warp. When re-colliding with the first warp point again, the collision isn't detected and the warp + background change just doesn't happen. Heres the code.


Sorry if its very specific, maybe someone out there knows.


if you think its stupid to even write flash just dont bother replying pls >:(


stage.addEventListener(Event.ENTER_FRAME, testCollision);

function testCollision(e:Event):void{

    if ( warp1.hitTestPoint(player.x, player.y, true)){
        
        collideWarp1 = true;

        } else { collideWarp1 = false; }

    if ( warp2.hitTestPoint(player.x, player.y + 10.5, true)){
        
        collideWarp2 = true;
        
        } else { collideWarp2 = false; }
}

Then these two triggers are posted in the walk down and walk up prompts.

// walking down / exiting house
if (collideWarp2){
      doWarp2(); 
}
// walking up / warping into house
if (collideWarp1){
      doWarp1(); 
}
// referencing these functions

function doWarp1():void{
        
            // set collision box and background to inside the house
            collide1.gotoAndPlay(2);
            BG1.gotoAndPlay(2);
            
            gotoAndStop(2);
            
            // warp player to inside door location
            player.x = 227.00;
            player.y = 195;

            // remove door
            door1.visible = false;
     }

function doWarp2():void{

        // set collision map and background to overworld    
        collide1.gotoAndPlay(1);
        BG1. gotoAndPlay(1);

        // set player position at the door
        player.x = 383.9 ;
        player.y = 241.95;
        
        gotoAndStop(1);

        //bring back in door and reset its anim to be closed.
        door1.visible = true;
        door1Open = false;
        door1.gotoAndStop(1);
   }


It has no problem using the same boolean system to constantly detect all of the other collisions of the game [player + collision box] So I dont understand why the warp stops working after one test, even though its triggered within the test collision function which supposedly triggers every ENTER_FRAME [ constantly ]


BBS Signature

Response to AS3 object warp-on-collide onlyonce 2020-03-19 20:44:04


Would you be able to post an example FLA on dumping grounds demonstrating the issue?


Like @mike said, we can't say for sure why this is happening unless we see an FLA - we can only guess as to why, but ultimately it's not a good choice to use frame scripts in Flash, and you should use a document class instead, otherwise there could be issues with handling variables and objects on the stage.


[Side note: you don't need to have the code:

  if ( warp1.hitTestPoint(player.x, player.y, true)){
        
        collideWarp1 = true;

        } else { collideWarp1 = false; }

as this can be collapsed into

collideWarp1 = warp1.hitTestPoint(player.x, player.y, true)

but this might just be a coding style thing]


That being said, here's a couple of things that could help us locate the source of the problem:


  • Where are warp1 and warp2 located?
  • Are they on the same frame or different frames?
  • What is this with respect to? Is this code on the main timeline, or in the player?
  • Is gotoAndStop(1) and gotoAndStop(2) for the main timeline, or a different timeline?
  • Are there any references that are unaccounted for? (which is basically what the first 4 questions are about)
  • Suppose you run the following code both before and after the warp (i.e. inside the doWarp functions and outside):
trace(warp1, warp2, MovieClip(this).currentFrame)
trace(collideWarp1, collideWarp2, player.x, player.y)
trace(BG1, collide1, door1, door1Open)
trace(stage.hasEventListener(Event.ENTER_FRAME))
  • What do you get?

Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature