views:

111

answers:

2

(I have asked this before but I dont think I was direct enough with my question and therefore it did not get resolved so here goes again!)

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. I have highlighted the two areas in which this occurs with asterisks:

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; 
} 
} 
} 
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; 
} 
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 think this is due to the non-existance of a Ball.as, when reading the tutorial I assumed it meant that I had to create a movie clip of a ball on stage and then export it for actionscript with the class name being Ball, however when flicking back through the book I saw that a Ball.as already existed, stating that I may need to use this again later on in the book, this read:

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();
 }
 }
 }

This managed to stop all the errors appearing however, it did not transmit any of the effects from Bubbles.as it just braught a Red Ball on the center of the stage. How would I alter this code in order to work in favour of Bubbles.as?

Please Help! Thanks!

A: 

It ran fine for me, however I needed to make some adjustments to the code you've provided - I noticed that from your Bubbles class you are trying to access Ball's properties that are set as private (radius, color), so either set them public or create the proper getter/setter methods for that matter.

public var radius:Number;
public var color:uint;

or if you want to use getter/setter functions

private var _radius:Number;
private var _color:uint;

public function get radius():Number
{
   return _radius;
}
public function set radius(val:Number):void
{
   _radius = val;
}

public function get color():uint
{
   return _color;
}
public function set color(val:uint):void
{
   _color= val;
}

After that change was made, I just set Bubbles as my FLA document root, and bam!

falomir
oh my falomir you really helped me out thank you so much, is it rude if i ask you one last question? You got it working right? so you know what it looks like is there anyway i could make the static ball in the center follow the mouse movement???
charmaine
A: 

An instance of Ball is needed. That what new Ball() does. Make an instance of the class Ball.as. So yes Ball.as is needed. I ran the code using Eclipse with Flex SDK and it worked for me. Could you put the code as it appears in your code editor (without asterisks?) Here is the screen shot of what I saw.

phwd