dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: background move counter to mouse

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    11

    background move counter to mouse

    Hi all,

    i've got some code where a sprite follows the mouse around the screen. Now I want to also move the background so that it gives the impression of moving around a space. At the moment, the background moves ok but it stops when the mouse gets to the edge of the stage. Does anyone know how I can make the background continues moving once the mouse is at an edge to give the impression that the user is moving through space?

    I've attached the fla and the code below, which might help explain better.

    Thanks,
    J



    Code:
    var ball:Ball = new Ball();
    
    addChildAt(ball, 1);
    
    ball.x = stage.stageWidth/2;
    ball.y = stage.stageHeight/2;
    
    var distx:Number;
    var disty:Number;
    
    stage.addEventListener(Event.ENTER_FRAME, track);
    
    //-----------------------------------------------
    function track(e:Event):void{		
    	distx = ball.x - mouseX;
    	disty = ball.y - mouseY;
    
    	ball.x -= distx / 10;
       	ball.y -= disty / 10;
    
    	bGround.x += distx / 100;
    	bGround.y += disty / 100;
    		
    	Mouse.hide();
    }
    Attached Files Attached Files

  2. #2
    Senior Member Computer Dork's Avatar
    Join Date
    Mar 2001
    Location
    St. Louis
    Posts
    1,026
    Maybe set up a buffer around the edge of the stage - so that if the mouseX/Y touches this virtual border, your track function ignores the mouseX/Y, and saves the last known value of each?

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    This will bound your mouse coordinate to the stage dimensions so you always end up at the furthest over position in your calculation if you mouse beyond the stage.

    PHP Code:
    function track(e:Event):void{        
        var 
    mx:int mouseX;
        if(
    mouseX stage.stageWidthmx stage.stageWidth;
        if(
    mouseX 0mx 0;

        var 
    my:int mouseY;
        if(
    mouseY stage.stageHeightmy stage.stageHeight;
        if(
    mouseY 0my 0;

        
    distx ball.mx;
        
    disty ball.mx;

        
    ball.-= distx 10;
           
    ball.-= disty 10;

        
    bGround.+= distx 100;
        
    bGround.+= disty 100;
            
        
    Mouse.hide();


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