A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Detect between frames

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3

    Detect between frames

    Hi

    Sorry maybe this is foolish question.This is first my post.

    How do i get detect between frames in move character (like platformer) in end of width or height frame 1 and appear at frame 2 at same position?
    like classic Flashback or Another world or Prince of persia platformer games.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    I assume you mean you want to detect when the character has hit the right or left edge of the screen, record their position and then reset them to the opposite side for the next screen.

    The simplest will be to use the Enter Frame event. It fires every frame so if your FLA is set to the 24 frames per second, the enter frame event will fire 24 times per second. Each time it fires, you can check the hero's position to see if you need to do something. I don't know if you are using code on the timeline or in a document class so I'll assume it's timeline.
    Code:
    // on frame 1, begin by telling flash you'd like to respond to ENTER_FRAME
    //onFrame is the name of the function we'd like called each frame
    stage.addEventListener(Event.ENTER_FRAME, onFrame);
    
    // now we need to define some variables that hold the value of the players position
    var playerX:Number;
    var playerY:Number;
    
    // here we define the onFrame function
    function onFrame(event:Event):void{
      // here we check if the player has hit the right side. You can add another if() statement to see if he hit the left side
      if(myHero.x >= stage.StageWidth){
        // record the players position
        playerX = myHero.x;
        playerY = myHero.y;
       // here you would add the code that changes the game screen
       // if you had to load the screen for some reason, you could respond to it finishing loading by using our recorded position to place the hero
       // lets pretend your load calls a function named onScreenLoad(). If you change screens immediately by gotoAndStop or instantiating something from the library
       // the contents of onScreenLoad() would go on the next line.
      }
    }
    
    // lets say this is called after a screen loads
    function onScreenLoad():void{
      // if the player had hit the right edge, then we would need to place him on the left. Just to be safe we set the Y too.
      myHero.x = 0;
      myHero.y = playerY;
    }
    That's really a rough example of how you'd go about it. There are many variables depending on way you're building your game so you will need to modify this concept to suit your needs.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Ok.many thanks.but when i move to right, myHero dont appear at left edge of frame 2 .How can i fix it?

  4. #4
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Could be many reasons for that but I have no idea how you've assembled your timeline. You'll have the most control by putting all your screens in one movieclip and your hero in another. Basically try to keep you main timeline only 1 frame long. Then if your hero is one layer and the screens in another you can change screens by myScreens.gotoAndStop(2) and change hero position with myHero.x = 100.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Is it auto detect for frame 2?

    Sorry but i think i need a example.If you don't mind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center