Results 1 to 9 of 9

Thread: snooker (pool) bal action - help!

  1. #1

    snooker (pool) bal action - help!

    I need to find an effect which causes a triangle of 15 red balls react to eachother when a cueball hits them as in a snooker game. No input is needed from a viewer as the animation is for a title page on my website.

    I am relatively new to flash and I am using Flash 8, anyone out there could help me?

  2. #2
    it sounds like you are not going for AS but a simple animation, and in that case, this is the wrong section. Anyway, you should learn about motion tweening, as that is what you need at this point.
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  3. Quote Originally Posted by bluemagica View Post
    it sounds like you are not going for AS but a simple animation, and in that case, this is the wrong section. Anyway, you should learn about motion tweening, as that is what you need at this point.
    Hmmm, I'd code it, but that's just because I code a lot and never animate.

    But to explain the whole thing right here.. that's to much to ask . I bet you can find some easy tutorials for a snooker game and apply that.

  4. #4
    I agree that if you need just an animation, then you probably don't need any robust code. However, seeing a simulation will help you create your animation. Here's an old pool game I wrote 3-4 years ago (the coding is ugly, and this is just the basic engine, but it's basically functional). It uses 9 balls, but 15 balls can be used just as easily.
    PHP Code:
    _root.friction=0.98;
    _root.ballsLeft=9;
    _root.breaker=false;

    MovieClip.prototype.moveBalls=function(vx,vy){
        
    this.vx=vx;
        
    this.vy=vy;
        
    this.onEnterFrame=function(){
            
    // check pockets (buggy, need to make pocket areas larger)
            
    if((this._x<31 && (this._y<31 || this._y>469)) || (this._x>869 && (this._y<31 || this._y>469)) || (this._x>439 && this._x<461 && (this._y<31 || this._y>469))){
                if(
    this.ballNumber>0){
                    
    _root.ballsLeft--;
                    if(
    _root.ballsLeft==0){
                        
    _root.play();
                    }
                    
    removeMovieClip(this);
                }else{
                    
    this.positioned=false;
                    
    this.vx=this.vy=0;
                    
    this._x=Stage.width/4;
                    
    this._y=Stage.height/2;
                }
            }
            if(
    Math.abs(this.vx)>0.1){this.vx*=_root.friction;}else{this.vx=0;}
            if(
    Math.abs(this.vy)>0.1){this.vy*=_root.friction;}else{this.vy=0;}
            if( 
    this._x this.vx Stage.width 10 this._width || this._x this.vx 10 this._width ){
                
    this.vx*=(-1);
            }else{
                
    this._x+=this.vx;
            }
            if( 
    this._y this.vy Stage.height 10 this._height || this._y this.vy 10 this._height ){
                
    this.vy*=(-1);
            }else{
                
    this._y+=this.vy;
            }
        };
    };

    // pockets
    px=[10,Stage.width/2,Stage.width-10,10,Stage.width/2,Stage.width-10];
    py=[10,5,10,Stage.height-10,Stage.height-5,Stage.height-10];
    for(
    p=0;p<6;p++){
        
    createEmptyMovieClip('p'+p,4+p);
        
    with(eval('p'+p)){
            
    lineStyle(40);
            
    lineTo(1,0);
            
    _x=px[p];
            
    _y=py[p];
        }
    }

    // rails
    createEmptyMovieClip('rails',3);
    with(rails){
        
    lineStyle(2);
        
    moveTo(10,10);
        
    lineTo(Stage.width-10,10);
        
    lineTo(Stage.width-10,Stage.height-10);
        
    lineTo(10,Stage.height-10);
        
    lineTo(10,10);
    }

    // balls
    ballcolors=['0xf6f6f6','0xffff00','0x0000ff','0xff0000','0x990099','0xffa500','0x008000','0xa52a2a','0x000000','0xffff00'];
    for(
    ballcount=0;ballcount<10;ballcount++){
        
    createEmptyMovieClip('b'+ballcount,10+ballcount);
        eval(
    'b'+ballcount).ballNumber=b;
        
    with(eval('b'+ballcount)){
            
    a=b=13;
            
    colors =[ 0xffffff,ballcolors[ballcount]*];
            
    alphas =[ 100,100 ];
            
    ratios =[ 0,255 ];
            
    matrix ={matrixType:'box',x:-a/1.5,y:-b*1.5,w:a*2,h:b*2,r:(90/180)*Math.PI };
            if(
    ballcount==9){
                
    colors =[0xffffff,0xffffff,0xffff00,0xffff00,0xffffff,0xffffff];
                
    alphas =[100,100,100,100,100,100];
                
    ratios =[0,85,86,170,171,255];
                
    matrix ={matrixType:'box',x:-a/1.5,y:-13,w:a*2,h:b*2,r:(90/180)*Math.PI };
            }
            
    lineStyle(0.5,ballcolors[ballcount]);
            if(
    ballcount<9){
                
    beginGradientFill('radial',colors,alphas,ratios,matrix );
            }else{
                
    beginGradientFill('linear',colors,alphas,ratios,matrix );
            }
            
    j=a*0.70711;
            
    n=b*0.70711;
            
    i=j-(b-n)*a/b;
            
    m=n-(a-j)*b/a;
            
    moveTo(a,0);
            
    curveTo(a,-m,j,-n);
            
    curveTo(i,-b,0,-b);
            
    curveTo(-i,-b,-j,-n);
            
    curveTo(-a,-m,-a,0);
            
    curveTo(-a,m,-j,n);
            
    curveTo(-i,b,0,b);
            
    curveTo(i,b,j,n);
            
    curveTo(a,m,a,0);
            
    endFill();
            switch(
    ballcount){
                case 
    _x=Stage.width/4_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4;_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4+23_y=Stage.height/2-_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+23_y=Stage.height/2+_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+46_y=Stage.height/2-_height; break;
                case 
    _x=Stage.width-Stage.width/4+46;_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4+46_y=Stage.height/2+_height; break;
                case 
    _x=Stage.width-Stage.width/4+69_y=Stage.height/2+_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+69_y=Stage.height/2-_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+91_y=Stage.height/2; break;
            }
        }
        if(
    ballcount>0){
            eval(
    'b'+ballcount).moveBalls(0,0);
        }else{
            eval(
    'b'+ballcount).positioned=false;
        }
    }

    // cue ball, stick and pointer
    eval('b'+0).onPress=function(){
        if(
    this.positioned){
            
    this.lastX=this._x;
            
    this.lasty=this._y;
            
    this.onEnterFrame=function(){
                
    with(_root){
                    
    clear();
                    
    lineStyle(4);
                    
    moveTo(this._x,this._y);
                    
    lineTo(this._xmouse+this._x,this._ymouse+this._y);
                    
    createEmptyMovieClip('pointer',10000);
                    
    pointer.lineStyle(1);
                    
    pointer._x=this._x;
                    
    pointer._y=this._y;
                    
    pointer.lineTo(-900,0);
                    
    pointer._rotation=Math.atan2(-this._ymouse,-this._xmouse)*180/Math.PI-180;
                }
            };
        }else{
            if(
    _root.breaker){
                
    this.startDrag(true);
            }else{
                
    this.startDrag(true,10,10,Stage.width/4,Stage.height-10);
            }
        }
    };

    // cue ball break
    eval('b'+0).onRelease=b0.onReleaseOutside=function(){
        if(!
    _root.breaker){_root.breaker=true;}
        if(
    this.positioned){
            
    _root.clear();
            
    delete this.onenterframe;
            
    removeMovieClip(pointer);
            
    this.svx=(-this._xmouse)/5;
            
    this.svy=(-this._ymouse)/5;
            
    this.moveBalls(this.svx,this.svy);
            
    Mouse.hide();
        }else{
            
    this.positioned=true;
            
    stopDrag();
        }
    };

    // collision detection
    createEmptyMovieClip('collision',2);
    collision.onEnterFrame=function(){
        
    this.totalV=0;
        for(
    b=0;b<10;b++){
            
    this.totalV+=((eval('b'+b).vx)+(eval('b'+b).vy));
        }
        if(
    this.totalV==0){Mouse.show();}
        for (var 
    ballcount>= 1; --i) { 
            var 
    x1 = eval('b'+i)._x
            var 
    y1 = eval('b'+i)._y
            for (var 
    i-1>= 0; --j) { 
                var 
    dx = eval('b'+j)._x x1
                var 
    dy = eval('b'+j)._y y1
                var 
    dist Math.sqrt(dx*dx+dy*dy); 
                 
                if (
    dist <= eval('b'+i)._width) { 
                 
                
    radius = eval('b'+i)._width/2

                
    normalX dx/dist
                
    normalY dy/dist
                
    midpointX = (x1+eval('b'+j)._x)/2
                
    midpointY = (y1+eval('b'+j)._y)/2
                eval(
    'b'+i)._x midpointX-normalX*radius
                eval(
    'b'+i)._y midpointY-normalY*radius
                eval(
    'b'+j)._x midpointX+normalX*radius
                eval(
    'b'+j)._y midpointY+normalY*radius
                
    dVector = (eval('b'+i).vx-eval('b'+j).vx)*normalX+(eval('b'+i).vy-eval('b'+j).vy)*normalY
                
    dvx dVector*normalX
                
    dvy dVector*normalY
                eval(
    'b'+i).vx -= dvx
                eval(
    'b'+i).vy -= dvy
                eval(
    'b'+j).vx += dvx
                eval(
    'b'+j).vy += dvy
             
                } 
            } 
        } 
    };

    stop(); 

  5. #5
    Quote Originally Posted by Jerryscript View Post
    I agree that if you need just an animation, then you probably don't need any robust code. However, seeing a simulation will help you create your animation. Here's an old pool game I wrote 3-4 years ago (the coding is ugly, and this is just the basic engine, but it's basically functional). It uses 9 balls, but 15 balls can be used just as easily.
    PHP Code:
    _root.friction=0.98;
    _root.ballsLeft=9;
    _root.breaker=false;

    MovieClip.prototype.moveBalls=function(vx,vy){
        
    this.vx=vx;
        
    this.vy=vy;
        
    this.onEnterFrame=function(){
            
    // check pockets (buggy, need to make pocket areas larger)
            
    if((this._x<31 && (this._y<31 || this._y>469)) || (this._x>869 && (this._y<31 || this._y>469)) || (this._x>439 && this._x<461 && (this._y<31 || this._y>469))){
                if(
    this.ballNumber>0){
                    
    _root.ballsLeft--;
                    if(
    _root.ballsLeft==0){
                        
    _root.play();
                    }
                    
    removeMovieClip(this);
                }else{
                    
    this.positioned=false;
                    
    this.vx=this.vy=0;
                    
    this._x=Stage.width/4;
                    
    this._y=Stage.height/2;
                }
            }
            if(
    Math.abs(this.vx)>0.1){this.vx*=_root.friction;}else{this.vx=0;}
            if(
    Math.abs(this.vy)>0.1){this.vy*=_root.friction;}else{this.vy=0;}
            if( 
    this._x this.vx Stage.width 10 this._width || this._x this.vx 10 this._width ){
                
    this.vx*=(-1);
            }else{
                
    this._x+=this.vx;
            }
            if( 
    this._y this.vy Stage.height 10 this._height || this._y this.vy 10 this._height ){
                
    this.vy*=(-1);
            }else{
                
    this._y+=this.vy;
            }
        };
    };

    // pockets
    px=[10,Stage.width/2,Stage.width-10,10,Stage.width/2,Stage.width-10];
    py=[10,5,10,Stage.height-10,Stage.height-5,Stage.height-10];
    for(
    p=0;p<6;p++){
        
    createEmptyMovieClip('p'+p,4+p);
        
    with(eval('p'+p)){
            
    lineStyle(40);
            
    lineTo(1,0);
            
    _x=px[p];
            
    _y=py[p];
        }
    }

    // rails
    createEmptyMovieClip('rails',3);
    with(rails){
        
    lineStyle(2);
        
    moveTo(10,10);
        
    lineTo(Stage.width-10,10);
        
    lineTo(Stage.width-10,Stage.height-10);
        
    lineTo(10,Stage.height-10);
        
    lineTo(10,10);
    }

    // balls
    ballcolors=['0xf6f6f6','0xffff00','0x0000ff','0xff0000','0x990099','0xffa500','0x008000','0xa52a2a','0x000000','0xffff00'];
    for(
    ballcount=0;ballcount<10;ballcount++){
        
    createEmptyMovieClip('b'+ballcount,10+ballcount);
        eval(
    'b'+ballcount).ballNumber=b;
        
    with(eval('b'+ballcount)){
            
    a=b=13;
            
    colors =[ 0xffffff,ballcolors[ballcount]*];
            
    alphas =[ 100,100 ];
            
    ratios =[ 0,255 ];
            
    matrix ={matrixType:'box',x:-a/1.5,y:-b*1.5,w:a*2,h:b*2,r:(90/180)*Math.PI };
            if(
    ballcount==9){
                
    colors =[0xffffff,0xffffff,0xffff00,0xffff00,0xffffff,0xffffff];
                
    alphas =[100,100,100,100,100,100];
                
    ratios =[0,85,86,170,171,255];
                
    matrix ={matrixType:'box',x:-a/1.5,y:-13,w:a*2,h:b*2,r:(90/180)*Math.PI };
            }
            
    lineStyle(0.5,ballcolors[ballcount]);
            if(
    ballcount<9){
                
    beginGradientFill('radial',colors,alphas,ratios,matrix );
            }else{
                
    beginGradientFill('linear',colors,alphas,ratios,matrix );
            }
            
    j=a*0.70711;
            
    n=b*0.70711;
            
    i=j-(b-n)*a/b;
            
    m=n-(a-j)*b/a;
            
    moveTo(a,0);
            
    curveTo(a,-m,j,-n);
            
    curveTo(i,-b,0,-b);
            
    curveTo(-i,-b,-j,-n);
            
    curveTo(-a,-m,-a,0);
            
    curveTo(-a,m,-j,n);
            
    curveTo(-i,b,0,b);
            
    curveTo(i,b,j,n);
            
    curveTo(a,m,a,0);
            
    endFill();
            switch(
    ballcount){
                case 
    _x=Stage.width/4_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4;_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4+23_y=Stage.height/2-_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+23_y=Stage.height/2+_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+46_y=Stage.height/2-_height; break;
                case 
    _x=Stage.width-Stage.width/4+46;_y=Stage.height/2; break;
                case 
    _x=Stage.width-Stage.width/4+46_y=Stage.height/2+_height; break;
                case 
    _x=Stage.width-Stage.width/4+69_y=Stage.height/2+_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+69_y=Stage.height/2-_height/2; break;
                case 
    _x=Stage.width-Stage.width/4+91_y=Stage.height/2; break;
            }
        }
        if(
    ballcount>0){
            eval(
    'b'+ballcount).moveBalls(0,0);
        }else{
            eval(
    'b'+ballcount).positioned=false;
        }
    }

    // cue ball, stick and pointer
    eval('b'+0).onPress=function(){
        if(
    this.positioned){
            
    this.lastX=this._x;
            
    this.lasty=this._y;
            
    this.onEnterFrame=function(){
                
    with(_root){
                    
    clear();
                    
    lineStyle(4);
                    
    moveTo(this._x,this._y);
                    
    lineTo(this._xmouse+this._x,this._ymouse+this._y);
                    
    createEmptyMovieClip('pointer',10000);
                    
    pointer.lineStyle(1);
                    
    pointer._x=this._x;
                    
    pointer._y=this._y;
                    
    pointer.lineTo(-900,0);
                    
    pointer._rotation=Math.atan2(-this._ymouse,-this._xmouse)*180/Math.PI-180;
                }
            };
        }else{
            if(
    _root.breaker){
                
    this.startDrag(true);
            }else{
                
    this.startDrag(true,10,10,Stage.width/4,Stage.height-10);
            }
        }
    };

    // cue ball break
    eval('b'+0).onRelease=b0.onReleaseOutside=function(){
        if(!
    _root.breaker){_root.breaker=true;}
        if(
    this.positioned){
            
    _root.clear();
            
    delete this.onenterframe;
            
    removeMovieClip(pointer);
            
    this.svx=(-this._xmouse)/5;
            
    this.svy=(-this._ymouse)/5;
            
    this.moveBalls(this.svx,this.svy);
            
    Mouse.hide();
        }else{
            
    this.positioned=true;
            
    stopDrag();
        }
    };

    // collision detection
    createEmptyMovieClip('collision',2);
    collision.onEnterFrame=function(){
        
    this.totalV=0;
        for(
    b=0;b<10;b++){
            
    this.totalV+=((eval('b'+b).vx)+(eval('b'+b).vy));
        }
        if(
    this.totalV==0){Mouse.show();}
        for (var 
    ballcount>= 1; --i) { 
            var 
    x1 = eval('b'+i)._x
            var 
    y1 = eval('b'+i)._y
            for (var 
    i-1>= 0; --j) { 
                var 
    dx = eval('b'+j)._x x1
                var 
    dy = eval('b'+j)._y y1
                var 
    dist Math.sqrt(dx*dx+dy*dy); 
                 
                if (
    dist <= eval('b'+i)._width) { 
                 
                
    radius = eval('b'+i)._width/2

                
    normalX dx/dist
                
    normalY dy/dist
                
    midpointX = (x1+eval('b'+j)._x)/2
                
    midpointY = (y1+eval('b'+j)._y)/2
                eval(
    'b'+i)._x midpointX-normalX*radius
                eval(
    'b'+i)._y midpointY-normalY*radius
                eval(
    'b'+j)._x midpointX+normalX*radius
                eval(
    'b'+j)._y midpointY+normalY*radius
                
    dVector = (eval('b'+i).vx-eval('b'+j).vx)*normalX+(eval('b'+i).vy-eval('b'+j).vy)*normalY
                
    dvx dVector*normalX
                
    dvy dVector*normalY
                eval(
    'b'+i).vx -= dvx
                eval(
    'b'+i).vy -= dvy
                eval(
    'b'+j).vx += dvx
                eval(
    'b'+j).vy += dvy
             
                } 
            } 
        } 
    };

    stop(); 
    amazing coding , but ball is not moving , plz help me

  6. #6
    Sorry, forgot the instructions.

    First, click on the cue ball to position it anywhere on the left third of the table. Then click on the cue ball, and drag the mouse to set power and direction. Release to hit the cue ball. Once the balls stop moving completely, you can click and drag on the cue ball again to shoot.

    Here's a sample using the exact code above and nothing else:
    http://www.jerryscript.hostrocket.co...ples/9ball.swf

  7. Quote Originally Posted by Jerryscript View Post
    Sorry, forgot the instructions.

    First, click on the cue ball to position it anywhere on the left third of the table. Then click on the cue ball, and drag the mouse to set power and direction. Release to hit the cue ball. Once the balls stop moving completely, you can click and drag on the cue ball again to shoot.

    Here's a sample using the exact code above and nothing else:
    http://www.jerryscript.hostrocket.co...ples/9ball.swf
    I haven't looked at the code, but trying out the game I noticed that the balls check for collisions every loop, which means that balls get into each other and then bounce, which means it's a very crappy engine .

  8. #8
    I'm just curious, how many people have stopped using this forum thanks to your sweet comments?

    Add one to the list

  9. Quote Originally Posted by Jerryscript View Post
    I'm just curious, how many people have stopped using this forum thanks to your sweet comments?

    Add one to the list
    Gee, what's your problem? It's just that I expected more from someone like you, sorry if I hurt your feelings or something :/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012