A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Problem with parralax scrolling and camera easing

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Posts
    238

    Problem with parralax scrolling and camera easing

    Hey All,


    I am trying to whip up a template for a side scrolling/exploration engine. Basically as you walk there are different depth levels that scroll at different rates; also there is a camera easing effect as you walk. You can also "warp" from one region to the next by walking through a door at the end of a region.

    The problem I am having is that sometimes the foreground and backgrounds continue to creep left or right after the user has stopped moving... and I can't seem to counter it no matter what I try. Can some of you savvy coders look at the attached FLA file (i commented stuff on the problem) and try to help me figure this out please. Note: the problem seems to be more apparent after you've "warped" a few times. Try rapidly going right and left near the area where the easing starts and stops to make the glitch appear. It may sound confusing but its easy to see; just run the movie and play around for a little while.

    Thank you very much!!
    Attached Files Attached Files

  2. #2
    Member
    Join Date
    Nov 2003
    Posts
    90
    I ran it and saw no problems... even after warping several times everything slowed down properly.

  3. #3
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Keep trying...trust me it happens; albeit very gradually. In the action script on the first frame you will see that i have a correction factor function (that i totally just jerry-rigged...I'm sure its not the real solution). If you remove that function as I suggest in the comments; you will see the problem appear much faster. Thanks.

  4. #4
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    This issue has been plaguing me for months...please help! Thanks.

  5. #5
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Most FKers (including myself, although that's because Flash 8 takes an ungodly amount of time to load) will not download FLAs. Post some of your code instead.
    http://www.birchlabs.co.uk/
    You know you want to.

  6. #6
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Quote Originally Posted by VENGEANCE MX
    Most FKers (including myself, although that's because Flash 8 takes an ungodly amount of time to load) will not download FLAs. Post some of your code instead.

    Hey Vengeance; long time no see! Yeah, I know, but I feel this is a problem where you really need to see it to understand whats going on; the file is lightweight too. Anyhow... Here is the code for the movement of the guy and the camera:

    code:


    view = { w: 550 };
    ground = { mc: groundMc, margin: groundMc.BGground._width * .5};
    dude = { mc: groundMc.dudeMc, Xspeed : 10, Yspeed : 2, Xdirection: 0, Ydirection: 0 };
    cam = { x: view.w * .5, to: view.w * .5, margin: view.w, easing: .2 };

    this.onEnterFrame = function ()
    {
    MyRoot.ground = { mc: groundMc, margin: groundMc.BGground._width * .5};
    moveDude();
    moveCamera();
    }

    //
    function moveCamera() {
    var d = dude;
    var g = ground;
    var c = cam;
    var sG = g.mc._x;
    var sFarG = g.mc.FarGround._x;
    var sForG = g.mc.ForGround._x;
    var sSky = g.mc.sky._x; //save last positions
    //
    var p = { x: 0, y: 0 };
    d.mc.localToGlobal(p);
    c.to = p.x ;
    //
    var m = (c.to - c.x) * cam.easing;
    g.mc._x -= m;
    g.mc.sky._x +=m;
    g.mc.FarGround._x += m *.9;
    g.mc.ForGround._x -= m *.5;

    //
    var b = g.mc.BGground.getBounds(this);
    //trace (b.xMax - c.margin);
    //
    if (b.xMin > 0) {
    m = b.xMin; //stop roundoff error
    g.mc._x -= b.xMin;
    g.mc.sky._x += b.xMin;
    g.mc.FarGround._x += b.xMin *.9;
    g.mc.ForGround._x -= b.xMin *.5;
    }
    if (b.xMax < c.margin) {
    m = b.xMax - c.margin; //stop roundoff error
    g.mc._x -= b.xMax - c.margin;
    g.mc.sky._x += b.xMax - c.margin;
    g.mc.FarGround._x += (b.xMax - c.margin) *.9;
    g.mc.ForGround._x -= (b.xMax - c.margin) *.5;
    }


    //this offset counters some of the problem; try removing this code to see problem worse
    if (SG - g.mc._x == 0) {
    g.mc.FarGround._x += (sFarG - g.mc.FarGround._x);
    g.mc.ForGround._x += (sForG - g.mc.ForGround._x);
    g.mc.sky._x += (sSky - g.mc.sky._x);
    }

    //trace (g.mc.sky._x);
    //var douche = b.xMax - c.margin;
    //trace (m *.9+ " : " + douche*.9); //round off error is part of the problem here!
    //trace (g.mc.FarGround._x);
    //trace (cam.x); //this aint moving, no problem here
    //trace (cam.to); //this aint moving, no problem here
    //trace (cam.margin); //this aint moving, no problem here
    //trace (c.to - c.x); //this aint moving, no problem here
    //trace (b.xMax);

    // the camera could be calibraiting for tiny offsets in the ground position
    // For and Far grounds won't get this!
    }
    //
    function moveDude() {
    var d = dude;
    var g = ground;
    var c = cam;
    var s = d.mc._x; // Save the last position
    var sy = d.mc._y; // last y position
    //var saveFarG = g.mc.FarGround._x;
    //var saveForG = g.mc.ForGround._x;
    //

    d.Xdirection = 0;
    d.Ydirection = 0;
    //
    if (MyRoot.AllowMove == 1) {

    if (Key.isDown(Key.RIGHT)) {
    MyRoot.rightmarker = 1;
    } else {
    MyRoot.rightmarker = 0;
    }

    if (Key.isDown(Key.LEFT)) {
    MyRoot.leftmarker = 2;
    } else {
    MyRoot.leftmarker = 0;
    }

    if (Key.isDown(Key.UP)) {
    MyRoot.upmarker = 4;
    } else {
    MyRoot.upmarker = 0;
    }

    if (Key.isDown(Key.DOWN)) {
    MyRoot.downmarker = 8;
    } else {
    MyRoot.downmarker = 0;
    }

    MyRoot.buttonTotal = MyRoot.rightmarker+MyRoot.leftmarker+MyRoot.upmark er+MyRoot.downmarker;

    switch (MyRoot.buttonTotal) {

    case 0 :
    break;

    case 1 :
    d.Xdirection = d.Xspeed;
    break;

    case 2 :
    d.Xdirection = -d.Xspeed;
    break;

    case 3 :
    break;

    case 4 :
    d.Ydirection = -d.Yspeed;
    break;

    case 5 :
    d.Xdirection = d.Xspeed;
    d.Ydirection = -d.Yspeed;
    break;

    case 6 :
    d.Xdirection = -d.Xspeed;
    d.Ydirection = -d.Yspeed;
    break;

    case 7 :
    d.Ydirection = -d.Yspeed;
    break;

    case 8 :
    d.Ydirection = d.Yspeed;
    break;

    case 9 :
    d.Xdirection = d.Xspeed;
    d.Ydirection = d.Yspeed;
    break;

    case 10 :
    d.Xdirection = -d.Xspeed;
    d.Ydirection = d.Yspeed;
    break;

    case 11 :
    d.Ydirection = d.Yspeed;
    break;

    case 12 :
    break;

    case 13 :
    d.Xdirection = d.Xspeed;
    break;

    case 14 :
    d.Xdirection = -d.Xspeed;
    break;

    case 15 :
    break;

    default :
    trace("we got a problem here");
    }

    d.mc._x += d.Xdirection; // Move the dude
    d.mc._y += d.Ydirection; // Move the dude


    //
    var b = d.mc.getBounds(g.mc); // Get the dude's bounds relative to the ground
    //
    if (b.xMin < -g.margin || b.xMax > g.margin) {
    d.mc._x = s;
    }
    if (d.mc._y < MyRoot.topboundary || d.mc._y > MyRoot.bottomboundary ) {
    d.mc._y = sy;
    }
    }

    }



  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Try this
    PHP Code:
    function moveCamera() {
        var    
    dude;
        var    
    ground;
        var    
    cam;
        
        var    
    = { x0y};
        
    d.mc.localToGlobal(p);
        
    c.to p.;
        var    
    Math.floor((c.to c.x) * cam.easing);
        
    g.mc._x -= m;
        
    g.mc.sky._x += m;    

        var    
    g.mc.BGground.getBounds(this);

        if (
    b.xMin 0) {
            var 
    Math.floor(b.xMin);
            
    g.mc._x -= a;
            
    g.mc.sky._x += a;
            
    g.mc.FarGround._x += 0.9;
            
    g.mc.ForGround._x -= 0.5;
        } 
        if (
    b.xMax c.margin) {
            var 
    Math.floor(b.xMax c.margin);
            
    g.mc._x -= a;
            
    g.mc.sky._x += a;
            
    g.mc.FarGround._x += 0.9;
            
    g.mc.ForGround._x -= 0.5;
        }
        
    g.mc.FarGround._x += *.9;    
        
    g.mc.ForGround._x -= *.5;


  8. #8
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Tony! Long time no see as well! I think you nailed it my friend; this appears to work. So it was simply a rounding error that was the problem... and you circumvented it with the Math.floor function...that was it?

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