dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Stoping Ball at Mouse Location

  1. #1
    Senior Member
    Join Date
    May 2007
    Posts
    102

    Stoping Ball at Mouse Location

    Hi,

    i have made a simple application to move a ball to the location of the mouse. I am trying to adjust my app so that when the ball does move in the same position as my mouse location, it should remove the listener showing that the ball does reach my mouse's destination.


    Code:
    package  
    {
    	
    	/**
    	 * Zenos.as is meant to show how you can demonstrate friction using the Zenos Paradox theory
    	 * @author Luong Vuong
    	 * Date created: 21/12/2009
    	 * Last modified: 22/12/2009
    	 */
    	
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	
    	public class Zenos extends Sprite 
    	{
    		private var ball:MovieClip;
    		
    		public function Zenos() :void 
    		{
    			ball = new Ball();
    			ball.x = ball.y = 100;
    			 
    			construct();
    			
    			 
    		}
    		
    			 
    		// Add listeners and add UI to display list
    		public function construct() :void
    		{
    			addChild(ball);
    			addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
    				 
    			 
    
    		}
    		
    		public function onLoop(evt:Event) :void
    		{
    			ball.x += velFriction(ball.x, mouseX, 5);
    			ball.y += velFriction(ball.y, mouseY, 5);
    			
    			// remove event listener when ball comes closer to destination i.e 0 meaning both mouse & ball are in same location
    			if (ball.x < 0 || ball.y < 0 ) 
    			{
    				removeEventListener(Event.ENTER_FRAME, onLoop);
    				parent.(removeChild(this));
    			}
    		}
    		
    		
    		public function velFriction(orig:Number, dest:Number, coef:Number) :Number
    		{ 	
    			
    				var distance:Number = (dest - orig) / coef;
    				trace(distance);
    				return distance;
    			    
    
    		}
    	}
     
    }
    
    /*var distance:Number = (dest - orig) / coef;
    			
    			if (distance < 0)
    			{
    				 this.removeEventListener(Event.ENTER_FRAME, onLoop);
    			}
    			else
    			{
    				return distance ;
    			}
    			 */
    The problem i am having is that when the ball does reach my mouse location it still keeps going.

    I have tried to modify the condition to this:

    Code:
    if (ball.x == mouseX || ball.y == mouseY ) 
    			{
    				removeEventListener(Event.ENTER_FRAME, onLoop);
    				parent.(removeChild(this));
    			}
    and to this:

    Code:
    if (ball.x == mouseX || ball.y == mouseY || ball.x == stage.stageWidth || ball.y == stage.stageHeight) 
    			{
    				removeEventListener(Event.ENTER_FRAME, onLoop);
    				parent.(removeChild(this));
    			}
    however doing so results in the following error message:

    [Fault] exception, information=TypeError: Error #1123: Filter operator not supported on type flash.display.Stage.
    Fault, onLoop() at Zenos.as:49
    How can i ensure that when the ball does reach the mouse location, it stops perfectly?

  2. #2
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    I think you have made this many times more complicated than it already is. I don't know AS 3.0, but here is how it should work for AS 2.0.

    PHP Code:
    var friction:Number 5;
    var 
    decay:Number 10;
    var 
    buffer:Number 10;
    ball.vy 0;
    ball.vx 0;
    _root.onEnterFrame = function() {
        if (!
    hit == true) {
            
    dx _xmouse+(_xmouse-ball._x)*buffer;
            
    dy _ymouse+(_ymouse-ball._y)*buffer;
            
    ball.vx += (dx-ball._x)/friction;
            
    ball.vy += (dy-ball._y)/friction;
        }
        
    ball.vx/=decay;
        
    ball.vy/=decay;
        
    ball._x += ball.vx;
        
    ball._y += ball.vy;
        if (
    ball.hitTest(_xmouse_ymousetrue)) {
            
    hit true;
        }
    }; 
    Syntax Error

  3. #3
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by Adamkyler View Post
    I think you have made this many times more complicated than it already is. I don't know AS 3.0, but here is how it should work for AS 2.0.

    PHP Code:
    var friction:Number 5;
    var 
    decay:Number 10;
    var 
    buffer:Number 10;
    ball.vy 0;
    ball.vx 0;
    _root.onEnterFrame = function() {
        if (!
    hit == true) {
            
    dx _xmouse+(_xmouse-ball._x)*buffer;
            
    dy _ymouse+(_ymouse-ball._y)*buffer;
            
    ball.vx += (dx-ball._x)/friction;
            
    ball.vy += (dy-ball._y)/friction;
        }
        
    ball.vx/=decay;
        
    ball.vy/=decay;
        
    ball._x += ball.vx;
        
    ball._y += ball.vy;
        if (
    ball.hitTest(_xmouse_ymousetrue)) {
            
    hit true;
        }
    }; 
    I don't know about AS 2.0 but that to me looks more complex than my if statements to be honest.

  4. #4
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    really?

    You can make it a lot more simple if you take out velocity. This is only 9 lines, yours uses all sorts of weird imports and fancy doodads and those pesky displays.

    PHP Code:
     _root.onEnterFrame = function() { 
        if (!
    hit == true) { 
            
    ball._x _xmouse+(_xmouse-ball._x); 
            
    ball._y _ymouse+(_ymouse-ball._y); 
        } 
        if (
    ball.hitTest(_xmouse_ymousetrue)) { 
            
    hit true
        } 
    }; 
    Syntax Error

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