dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [CS3] Drag to fire Balls.

  1. #1
    Member
    Join Date
    Oct 2006
    Location
    UK
    Posts
    43

    [CS3] Drag to fire Balls.

    Basically Im trying to recreate this game called shuffle found on the shockwave site. http://www.shockwave.com/gamelanding/shuffle.jsp

    Ive taken a fantastic bit of code from http://www.krazydad.com/bestiary/ called "Superball" which gives a basic ball environment simular to the one in the game. I have edited it a little but am struggling to convert it now.

    At the moment you "toss" the balls around the screen but I need to edit this into a drag back and release to select the power and direction, like the above game.

    Code:
    MovieClip.prototype.pressBall = function()
    {
    	this.isDragging = true;
    	this.vx = this.vy = 0;
    	this.lmx = _root._xmouse;
    	this.lmy = _root._ymouse;
    	this.startDrag(false);
    }
    
    MovieClip.prototype.releaseBall = function()
    {
    	this.isDragging = false;
    	this.stopDrag();
    }
    
    MovieClip.prototype.moveBall = function()
    {
    	// compute forces on ball
    	if (!this.isDragging) {
    		this.vx *= dampK;	// damping due to air resistance
    		this.vy *= dampK;
    
    		this.vy += gravK;	// gravity
    		
    		// bounce off floor and ceiling
    		if (this._y+ballRadius + this.vy >= SH ||
    			(this._y-ballRadius) + this.vy < 0)
    		{
    			this.vy *= -dampCollision;
    		}
    		// bounce off right and left walls
    		if ((this._x-ballRadius) + this.vx <= 0 ||
    			 this._x+ballRadius + this.vx >= SW)
    		{
    			this.vx *= -dampCollision;
    		}
    		this._x += this.vx;
    		this._y += this.vy;
    	}
    	else {
    		// Set vx,vy to correspond to mouse movement, enabling user to 'toss' ball
    		this.vx *= dampK;
    		this.vy *= dampK;
    		this.vx = _root._xmouse - this.lmx;
    		this.vy = _root._ymouse - this.lmy;
    		this.lmx = _root._xmouse;
    		this.lmy = _root._ymouse;
    	}
    }
    I think this is the relevent peice of code. Any ideas from anyone?

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    There are basically three fixes you need to do. First, comment out the lines that read

    this.startDrag(false);

    and

    this.stopDrag();

    Next, near the bottom of the script there are two lines that read:
    Code:
    		this.vx = _root._xmouse - this.lmx;
    		this.vy = _root._ymouse - this.lmy;
    Change then to something like:
    Code:
    		this.vx = (this.lmx - _root._xmouse) * 1.5;
    		this.vy = (this.lmy - _root._ymouse) * 1.5;
    This reverses the direction of the force (as in the game you are trying to emulate) and magnifies the force 1.5 times.

    You'll also need to add code that renders an arrow that begins at this._x and this._y and whose point is centered on this._x + this.vx, this._y + this.vy.

    I'll let you figure that part out.

  3. #3
    Member
    Join Date
    Oct 2006
    Location
    UK
    Posts
    43
    Thats excellent. Is there a way I could change it so it doesnt react to the speed that the mouse is moved back and rather the distance it is from the ball?

    Also, while im here, am I going about recreating that game in the correct way? What would you recommend?

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    To react to relative mouse position, use this

    Code:
    this.vx = (this._x - _root._xmouse) * 1.5;
    this.vy = (this._y - _root._ymouse) * 1.5;
    To answer your second question. I'm not sure there is a correct way.

    I'm a bit concerned that you're using this code without truly understanding it - this may cause you trouble in the future. You should take the time to gain understanding of what the code is doing. For example, why did the fixes I gave you work? I could tell you, but can you figure this out on your own?

    If there is a correct way, you will only truly discover it by stumbling around a bit in the dark, that is, through experience.

    Last edited by jbum; 03-14-2008 at 06:31 PM.

  5. #5
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    hey jbum! nice to see you here, man

    that's it. Take care

    gparis

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