A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: How to redirect a ball off a wall

  1. #1
    Junior Member
    Join Date
    Dec 2010
    ur mama's house :O

    Question How to redirect a ball off a wall

    I'm trying to make it so that when the ball hits the wall, it bounces off of it.. this is my code
    Actionscript Code:
    var elastic:MovieClip = _root.createEmptyMovieClip("elastic", 1);
    var point1:MovieClip = _root.attachMovie("sling", "point1", 3, {_x:50, _y:200});
    var point2:MovieClip = _root.attachMovie("sling", "point2", 4, {_x:450, _y:200});
    var gravity = 0.1;
    var angle1:Number = 0;
    var angle2:Number = 0;
    var radius:Number = 15.0;

    // Or whatever half the width of your ball is.
    var elasticCoefficient:Number = 0.0005;
    // This number will affect the stretchiness of the sling.  The greater the number
    // the tighter the elastic will be.                
    var released:Boolean = true;
    var forced:Boolean = false;
    var acc:Object = {x:0, y:0};
    var vel:Object = {x:0, y:0};

    _root.onMouseUp = function() {
        _root.released = true;
    ball.onPress = function() {
        _x = _root._xmouse;
        _y = _root._ymouse;
        _root.released = false;
        vel.x = 0;
        vel.y = 0;
    ball.onRelease = function() {
        _root.released = true;
    _root.onEnterFrame = function() {
        elastic.lineStyle(3, 0x000000);
        if (released) {
            ball._x += vel.x;
            ball._y += vel.y;
        if (ball._y>=187) {
            /* This area differs from the code in the tutorial.
            The reason for that is I didn't read the code, I just looked at the examples.
            I try to gain experience with problem-solving by doing things as close to by myself as possible. */

            forced = true;
            var x1:Number = ball._x-point1._x;
            var y1:Number = ball._y-point1._y;
            var x2:Number = point2._x-ball._x;
            var y2:Number = point2._y-ball._y;
            var distance1:Number = Math.sqrt(x1*x1+y1*y1);
            var distance2:Number = Math.sqrt(x2*x2+y2*y2);
            _root.angle1 = Math.atan2(y1, x1);
            _root.angle2 = Math.atan2(y2, x2);
            var xOffset:Number = Math.cos(angle1+Math.PI/2)*radius;
            var yOffset:Number = Math.sin(angle1+Math.PI/2)*radius;
            var xOffset2:Number = Math.cos(angle2+Math.PI/2)*radius;
            var yOffset2:Number = Math.sin(angle2+Math.PI/2)*radius;
            angle1 += Math.sin(radius/distance1);
            angle2 += Math.sin(radius/distance2)*-1;
            elastic.moveTo(point1._x, point1._y);
            elastic.lineTo(ball._x+xOffset, ball._y+yOffset);
            elastic.moveTo(point2._x, point2._y);
            elastic.lineTo(ball._x+xOffset2, ball._y+yOffset2);
        } else {
            forced = false;
            elastic.moveTo(point1._x, point1._y);
            elastic.lineTo(point2._x, point2._y);
        acc.x = 0;
        acc.y = 0;
        acc.y = gravity;
        if (released && forced) {
            /* This section applies the force of the sling to the ball in terms of acceleration based on the stretching of
            the sling, in the direction of the displacement, scaled by a coefficient that also encapsulates the mass of
            the ball. */

            acc.x += distance1*Math.sin(angle2)*elasticCoefficient;
            acc.y += -distance1*Math.cos(angle1)*elasticCoefficient;
            acc.x += distance2*Math.sin(angle1)*elasticCoefficient;
            acc.y += -distance2*Math.cos(angle2)*elasticCoefficient;
        if (released) {
            vel.x += acc.x;
            vel.y += acc.y;
        plus.onRelease = function() {
        elasticCoefficient /= .9;
    minus.onRelease = function() {
        elasticCoefficient *= .9;

    the ball's instance name is "ball" the wall's is "wall"....

  2. #2
    Junior Member
    Join Date
    Dec 2011
    understanding someone else code is much more difficult then writing this code from the scratch. I am sure you will do it yourself if you use the following algorithm:
    1. onEnterFrame check if the ball reached the wall either by using hitTest or comparing ball's coordinate against the wall coordinates.
    2. when the ball reaches the wall, reverse ball's X-speed if the wall is vertical or Y-speed if the wall is horisontal

    If the wall is not horisontal or vertical you have to use some trigonometry

  3. #3
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Theres some free ball bouncing off wall code action script on freeactionscript.com you can probably learn from that, that websites free open source files taught me alot

  4. #4
    shavingcream incarnate gukinator's Avatar
    Join Date
    Jul 2008
    Santa Cruz
    Heres a quick example i wrote that just has a simple ball bouncing off the bounding walls of the stage, you should be able to modify your code using my attached .fla as reference
    Attached Files Attached Files

Tags for this Thread

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