0

Basically i am working through a book called..Foundation Actionscript 3.0 Animation, making things move.

i am now on Chapter 9 - collision detection. On two lines of my code i get the 1135 error, letting me know that i have an incorrect number of arguments. Can anybody help me out on why this may be?

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    public class Bubbles extends Sprite
    {
        private var balls:Array;
        private var numBalls:Number = 10;
        private var centerBall:Ball;
        private var bounce:Number = -1;
        private var spring:Number = 0.2;

        public function Bubbles()
        {
            init();
        }

        private function init():void
        {
            balls = new Array();
            centerBall = new Ball(100, 0xcccccc);
            addChild(centerBall);
            centerBall.x = stage.stageWidth / 2;
            centerBall.y = stage.stageHeight / 2;
            for(var i:uint = 0; i < numBalls; i++)
            {
                var ball:Ball = new Ball(Math.random() *
                40 + 5,
                Math.random() * 0xffffff);
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                ball.vx = Math.random() * 6 - 3;
                ball.vy = Math.random() * 6 - 3;
                addChild(ball);
                balls.push(ball);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(event:Event):void
        {
            for(var i:uint = 0; i < numBalls; i++)
            {
                var ball:Ball = balls[i];
                move(ball);
                var dx:Number = ball.x - centerBall.x;
                var dy:Number = ball.y - centerBall.y;
                var dist:Number = Math.sqrt(dx * dx + dy * dy);
                var minDist:Number = ball.radius + centerBall.radius;
                if(dist < minDist)
                {
                    var angle:Number = Math.atan2(dy, dx);
                    var tx:Number = centerBall.x +
                    Math.cos(angle) * minDist;
                    var ty:Number = centerBall.y +
                    Math.sin(angle) * minDist;
                    ball.vx += (tx - ball.x) * spring;
                    ball.vy += (ty - ball.y) * spring;
                }
            }
       }
       // Having Trouble Here:
       private function move(ball:Ball):void
       {
           ball.x += ball.vx;
           ball.y += ball.vy;
           if(ball.x + ball.radius > stage.stageWidth)
           {
               ball.x = stage.stageWidth - ball.radius;
               ball.vx *= bounce;
           }
           else if(ball.x - ball.radius < 0)
           {
               ball.x = ball.radius;
               ball.vx *= bounce;
           }
           // Having Trouble Here:
           if(ball.y + ball.radius > stage.stageHeight)
           {
               ball.y = stage.stageHeight - ball.radius;
               ball.vy *= bounce;
           }
           else if(ball.y - ball.radius < 0)
           {
               ball.y = ball.radius;
               ball.vy *= bounce;
           }
        }
    }
}

I have indicated the lines i am having trouble with.

4
  • When I created my own Ball class to test out the code above, it doesn't throw any compilation errors. Also the asterisked lines above don't seem to be possible sources of error to me. Maybe you might want to edit your question and add the code of the Ball class?
    – anonymous
    May 17, 2010 at 1:39
  • Agreed. The compiler may be complaining about those lines, but the cause of the trouble is elsewhere..
    – fenomas
    May 17, 2010 at 1:45
  • 1
    I bet that the constructor of Ball takes no arguments.
    – Amarghosh
    May 17, 2010 at 6:44
  • mr roy...i know this might sound stupid cuz im new to flash As3...when you say Ball class what do you mean! (SO SORRY I MUST SOUND RETARDED) what i have done is created a ball movie clip on stage and the exported it for actionscript with the class name being Ball! is this wrong..do i need to do something compleatly different?
    – charmaine
    May 17, 2010 at 21:13

1 Answer 1

0

The following lines are the culprits.

centerBall = new Ball(100, 0xcccccc);
var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xffffff);

You're trying to pass arguments to Ball's constructor that takes zero arguments. Change them to

centerBall = new Ball();
var ball:Ball = new Ball();

respectively and it'll work. If you really want to pass initialization details, create a Ball.as file and assign it to the movie clip on the stage. Now declare the constructor of Ball class to accept these two arguments and initialize the variables (radius and color) inside the constructor.


Ball.as would look something like

package
{
  public class Ball extends MovieClip
  {
    public function Ball(r:Number, col:uint)
    {
      this.radius = r;
      this.color = col;
    }
  }
}
4
  • What would the code be for the Ball.as be? plus ive already asigned this code Bubbles.as to the movie clip on stage how would i get around this?
    – charmaine
    May 18, 2010 at 9:34
  • Edit the ball symbol and assign Ball as the actionscript class name for that.
    – Amarghosh
    May 18, 2010 at 10:58
  • package { import flash.display.Sprite; public class Ball extends Sprite { private var radius:Number; private var color:uint; public var vx:Number=0; public var vy:Number=0; public function Ball(radius:Number=40, color:uint=0xff0000) { this.radius=radius; this.color=color; init(); } public function init():void { graphics.beginFill(color); graphics.drawCircle(0, 0, radius); graphics.endFill(); } } }
    – charmaine
    May 18, 2010 at 12:12
  • i read thro the book again and this was meant to be the Ball.as But when i run it as a swf. there are no errors...just a red ball on stage with no movement at all..what am i doin wrong now?
    – charmaine
    May 18, 2010 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.