A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] [F8] Making a Character "Teleport"

  1. #1
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58

    resolved [RESOLVED] [F8] Making a Character "Teleport"

    Hey all,

    I'm having trouble getting this character in my side-scrolling platform game to teleport. Basically there are going to be a lot of walls and when you walk into one, you see an effect and then the camera and the character should pan over to another room.

    I am trying to accomplish this using tweens. The following code is my function; can anyone tell me what I'm doing wrong? It traces correctly (though the hit seems to be a little weird.)

    PHP Code:
    if (g.wall1_1.hitTest(this._x-20this._ytrue)){
    trace("PORT");
    var 
    _root.char;
    var 
    home _root;
    c.gotoAndPlay("port");
    var 
    teleportX:Tween = new Tweenc"_xPos"regular.easeInOutc._x, -901true );
    var 
    teleportY:Tween = new Tweenc"_yPos"regular.easeInOutc._y2501true );
    var 
    rootTeleportX:Tween = new Tweenhome"_xPos"regular.easeInOuthome._x, -901true );
    var 
    rootTeleportY:Tween = new Tweenhome"_yPos"regular.easeInOuthome._y2501true );


  2. #2
    Senior Member AzraelKans's Avatar
    Join Date
    May 2002
    Location
    Hell... with frequent access to heaven ;)
    Posts
    409
    Well I personally wouldnt use tweens at all. simply use a math interpolations to move the camera around. like this:
    Code:
    	function ease(a, b, easeSpeed) {
    		var c:Number;
    		c = b-a;
    		if (c == 0) {
    			return 0;
    		}
    		return c/easeSpeed;
    	}
    the way to use it is like this:
    Code:
    
    //x=ease(x, destX, 5);
    
    //Correction!
    
    //OOPS!
    //sorry this is the correct code to use
    
    x += ease(x, tx, 5);
    That code is what I use to move my characters around in my game, Ease returns a value between a and b and that value gets smaller each time you call it.
    Last edited by AzraelKans; 01-23-2008 at 03:43 PM.

  3. #3
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58
    I'm not sure if that will work for me;

    I need the character to disappear and then reappear at the new spot. I was planning to use the "onMotionStopped" property for this. Is there something similar using your code?

  4. #4
    do your smiles love u? slicer4ever's Avatar
    Join Date
    Dec 2005
    Location
    in a random occurance
    Posts
    475
    AzraelKans

    when c = 0 u shoud return b not 0 otherwise the x position well suddenly jump

    pakorn if ur planning to make him disappear than just turn visibility off when he hits the wall position him at his new spot (if u want the camera to move to that spot use azraelkans method) than when the camra's there just turn visiblity back on

  5. #5
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58
    I don't think I'm using this correctly...I keep getting errors and it doesn't seem to be doing it properly.

    Can I see an example using plugged in variables and numbers? I don't know where I should be using "A, B or C" and where I should be using an actual number.

  6. #6
    do your smiles love u? slicer4ever's Avatar
    Join Date
    Dec 2005
    Location
    in a random occurance
    Posts
    475
    a is the current position of your mc(x or y), b is where you want your mc to go, and c is the speed at which you want it to travel their what is returned is the new position of your object

    give me 5 min and i'll write a small script using that method

    ok here: .fla/.swf

    i had to use a diffrent method since AzraelKans was producing a weird result anyways this is a substitute
    only shows for the x-axis movement you would just repeat for y
    Last edited by slicer4ever; 01-23-2008 at 06:29 PM.

  7. #7
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58
    "weird.rar: The archive is either in unknown format or damaged"

    I have Winrar... this file won't extract though. It gives this error message plus a popup that says no archives found...

  8. #8
    do your smiles love u? slicer4ever's Avatar
    Join Date
    Dec 2005
    Location
    in a random occurance
    Posts
    475
    i dl'ed it and extracted it fine, it should be about 7kb

    edit: try this link
    .fla

  9. #9
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58
    So it still doesn't seem to be working. He just kind of teleports back to where he just hit the wall, so he's just kind of right there, "glitching out."

    Maybe I oughta post the code as I am using it, see if something is wonky...

    PHP Code:
    if (g.wall1_1.hitTest(this._x-20this._ytrue)){
            
    trace("PORT");
        
    c.speed 0;
        var 
    _root.char;
        var 
    home _root;
        
    c.gotoAndPlay("port");
        
    move_to_object = function(abspeed){
            if(
    b>a){
                
    speed*=1;
            }else{
                
    speed*=-1;
                }
            if(
    a>b-(speed*2) && a<b+(speed*2)){
                return 
    a;
                }
            
    a+=speed;
            return 
    a;
            }
        
    main = function(){
            
    _root._x move_to_object(_root._xg.dest1_1._x10);
            }
        
    main();

    I think it's because this code is executed on a hitTest, and so as soon as the MC moves off the hit area the code stops running. . . how can I make it loop so that it runs until he hits the destination area?

    I tried a while loop but it freezes the .swf

    combined posts - Tony
    Last edited by tonypa; 01-25-2008 at 04:19 AM.

  10. #10
    do your smiles love u? slicer4ever's Avatar
    Join Date
    Dec 2005
    Location
    in a random occurance
    Posts
    475
    your theory is absolutely correct here is how to do it so that he well continue:

    (o and i'm ganna clean up the code a tad bit)

    Code:
    var move_to_dest = false;
    
    move_to_object = function(a, b, speed){
         if(b>a){
              speed*=1;
         }else{
              speed*=-1;
         }
         if(a>b-(speed*2) && a<b+(speed*2)){
              return a;
              //since move_to_dest was declared outside this method we can call to it inside the method
              move_to_dest = false;
         }
         a+=speed;
         return a;
    }
    
    
    if (g.wall1_1.hitTest(this._x-20, this._y, true)){
         trace("PORT");
         c.speed = 0;
         var c = _root.char;
         var home = _root;
         c.gotoAndPlay("port");
         //this basically allows us to tell the program hey we wanna move to the destination
         move_to_dest = true;
    } 
    
    //this moves us to that destination=-)
    if(move_to_dest==true){
         _root._x = move_to_object(_root._x, g.dest1_1._x, 10);
    }

    u never want to call a while loop inside a mc the only time i ever do is on the frame code any other time it just screws the program over instead since flash alwaysloops to each movie clip(and you can see this above) try doing the loop using varibles and if statements to tell the program how to react to get from a-b and maybe even c
    Last edited by slicer4ever; 01-25-2008 at 07:11 AM.

  11. #11
    Member
    Join Date
    Aug 2006
    Location
    Philadelphia, PA
    Posts
    58
    OK, I think I got it working! I had to change the var move_to_dest in its initial declaration, so instead of saying var move_to_dest = false; I had to just declare the variable (otherwise it was still stopping movement when the guy moved out of the hit area.)

    Now, though, I have to figure out how to declare him stopping...

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