Hi,
I’m new to AS3 and flash but I managed to learn from alot of reading. I have some code uploaded at this link: http://www.chuayou.com/pages/programs.htm but I’m getting an error. Namely this is the error I’m receiving:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::TimerEvent@70180b1 to flash.utils.Timer.
at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
at flash.utils::Timer/flash.utils:Timer::tick()
This is the code for the main class that I’m using:
package com.chuayou {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.BitmapFilterQuality;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import fl.controls.NumericStepper;
import fl.controls.ComboBox;
import fl.controls.TextInput;
import fl.controls.Label;
import fl.data.DataProvider;
import fl.managers.StyleManager;
public class Bubbles extends Sprite {
// Global variables
private var balls:Array = new Array();
private var numBalls:uint = 10;
private var centerBall:Ball = new Ball(100, 0xCC0000, 0xF0FD79, [0, 255], 0.5);
private var bounce:Number = -0.7;
private var spring:Number = 0.15;
private var hiSpeed:Number = 3;
private var lowSpeed:Number = 1;
private var gravity:Number = 0.6;
private var mass:Number = 0;
private var friction:Number = 0.6;
private var angle:Number = 0;
private var angleSpeed:Number = 0.015;
private var go:Number = 0;
// Component variables
private var bg:Sprite = new Sprite();
private var ns_numBalls:NumericStepper = new NumericStepper();
private var cb_mass:ComboBox = new ComboBox();
private var cb_friction:ComboBox = new ComboBox();
private var cb_bounce:ComboBox = new ComboBox();
private var cb_spring:ComboBox = new ComboBox();
private var lbl_mass:Label = new Label();
private var lbl_friction:Label = new Label();
private var lbl_ballCount:Label = new Label();
private var lbl_bounce:Label = new Label();
private var lbl_spring:Label = new Label();
// Global constants
private const yAlign:Number = 20;
private const ylblAlign:Number = 2;
private const cbWidth:Number = 70;
private const xPad:Number = cbWidth + 10;
private const blockIndent:Number = -2;
private const top:Number = 50;
private const bottom:Number = stage.stageHeight;
private const left:Number = 0;
private const right:Number = stage.stageWidth;
private const centerY:Number = bottom / 2;
private const centerX:Number = right / 2;
public function Bubbles() {
init();
}
// START: INITIALIZATION //
private function init():void {
addChild(centerBall);
centerBall.x = centerX;
centerBall.y = centerY;
drawBalls(numBalls);
// Initialize UI components and header here
drawBG(0x393939);
drawCBGravity();
drawCBFriction();
drawNumBalls();
drawCBBounce();
drawCBSpring();
drawLabels();
styleComponents();
// Event Listeners created here
addEventListener(Event.ENTER_FRAME, onEnterFrame);
centerBall.addEventListener(MouseEvent.MOUSE_DOWN, onHold);
ns_numBalls.addEventListener(Event.CHANGE, nsChange);
cb_mass.addEventListener(Event.CHANGE, cbGravChange);
cb_friction.addEventListener(Event.CHANGE, cbFricChange);
cb_bounce.addEventListener(Event.CHANGE, cbBounceChange);
cb_spring.addEventListener(Event.CHANGE, cbSpringChange);
}
private function drawBalls(numBalls:Number):void {
// Creates variable-defined number of smaller balls
for (var i:uint = 0; i < numBalls; i++) {
var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xffffff, 0xF0FD79, [0, 100]);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.vx = Math.random() * hiSpeed + lowSpeed;
ball.vy = Math.random() * hiSpeed * gravity + lowSpeed;
addChild(ball);
balls.push(ball);
}
}
// END: INITIALIZATION //
// START: DRAW UI COMPONENTS //
private function drawBG(color:Number):void {
bg.graphics.clear();
bg.graphics.beginFill(color);
bg.graphics.drawRect(0, 0, right, 50);
bg.graphics.endFill();
addChild(bg);
}
private function drawCBGravity():void {
var dp:DataProvider = new DataProvider();
dp.addItem({label: "Low", data: 1});
dp.addItem({label: "Medium", data: 2});
dp.addItem({label: "High", data: 3});
cb_mass.dataProvider = dp;
cb_mass.move(10, yAlign);
cb_mass.width = cbWidth;
cb_mass.selectedIndex = 1;
bg.addChild(cb_mass);
}
private function drawCBFriction():void {
var dp:DataProvider = new DataProvider();
dp.addItem({label: "Low", data: 1});
dp.addItem({label: "Medium", data: 2});
dp.addItem({label: "High", data: 3});
cb_friction.dataProvider = dp;
cb_friction.move(cb_mass.x + xPad, yAlign);
cb_friction.width = cbWidth;
cb_friction.selectedIndex = 1;
bg.addChild(cb_friction);
}
private function drawNumBalls():void {
ns_numBalls.move(cb_friction.x + xPad, yAlign);
ns_numBalls.maximum = 50;
ns_numBalls.minimum = 1;
ns_numBalls.value = 10;
ns_numBalls.width = 50;
bg.addChild(ns_numBalls);
}
private function drawCBBounce():void {
var dp:DataProvider = new DataProvider();
dp.addItem({label: "Low", data: 1});
dp.addItem({label: "Medium", data: 2});
dp.addItem({label: "High", data: 3});
dp.addItem({label: "Insane", data: 4});
cb_bounce.dataProvider = dp;
cb_bounce.move(ns_numBalls.x + xPad, yAlign);
cb_bounce.width = cbWidth;
cb_bounce.selectedIndex = 1;
bg.addChild(cb_bounce);
}
private function drawCBSpring():void {
var dp:DataProvider = new DataProvider();
dp.addItem({label: "Low", data: 1});
dp.addItem({label: "Medium", data: 2});
dp.addItem({label: "High", data: 3});
dp.addItem({label: "Insane", data: 4});
cb_spring.dataProvider = dp;
cb_spring.move(cb_bounce.x + xPad, yAlign);
cb_spring.width = cbWidth;
cb_spring.selectedIndex = 1;
bg.addChild(cb_spring);
}
private function drawLabels():void {
// Gravity
lbl_mass.move(cb_mass.x + blockIndent, ylblAlign);
lbl_mass.text = "Mass: ";
lbl_mass.autoSize = TextFieldAutoSize.LEFT;
bg.addChild(lbl_mass);
// Friction
lbl_friction.move(cb_friction.x + blockIndent, ylblAlign);
lbl_friction.text = "Friction: ";
lbl_friction.autoSize = TextFieldAutoSize.LEFT;
bg.addChild(lbl_friction);
// Ball Count
lbl_ballCount.move(ns_numBalls.x + blockIndent, ylblAlign);
lbl_ballCount.text = "Balls: ";
lbl_ballCount.autoSize = TextFieldAutoSize.LEFT;
bg.addChild(lbl_ballCount);
// Bounce
lbl_bounce.move(cb_bounce.x + blockIndent, ylblAlign);
lbl_bounce.text = "Bounce: ";
lbl_bounce.autoSize = TextFieldAutoSize.LEFT;
bg.addChild(lbl_bounce);
// Spring
lbl_spring.move(cb_spring.x + blockIndent, ylblAlign);
lbl_spring.text = "Spring: ";
lbl_spring.autoSize = TextFieldAutoSize.LEFT;
bg.addChild(lbl_spring);
}
private function styleComponents():void {
var format:TextFormat = new TextFormat("Arial", 12, 0xFFFFFF, true);
format.letterSpacing = 1;
StyleManager.setComponentStyle(Label, "textFormat", format);
format = new TextFormat("Arial", 10, 0x000000);
StyleManager.setComponentStyle(TextInput, "textFormat", format);
}
// END: DRAW UI COMPONENTS //
// START: EVENT HANDLERS //
private function onEnterFrame(evt:Event):void {
// Creating ball objects
for (var i:uint = 0; i < numBalls - 1; i++) {
var ball0:Ball = balls*;
for (var j:uint = i + 1; j < numBalls; j++) {
var ball1:Ball = balls[j];
var dx:Number = ball1.x - ball0.x;
var dy:Number = ball1.y - ball0.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
var minDist:Number = ball0.radius + ball1.radius;
// Bounce code
if (dist < minDist) {
var angle:Number = Math.atan2(dy, dx);
var targetX:Number = ball0.x + Math.cos(angle) * minDist;
var targetY:Number = ball0.y + Math.sin(angle) * minDist;
var ax:Number = (targetX - ball1.x) * spring;
var ay:Number = (targetY - ball1.y) * spring;
ball0.vx -= ax * friction;
ball0.vy -= ay * friction;
ball1.vx += ax * friction;
ball1.vy += ay * friction;
}
}
}
for (i = 0; i < numBalls; i++) {
var ball:Ball = balls*;
// Movement code
move(ball);
// Blur checker
blurBalls(ball);
// Bounce or Spring code
dx = ball.x - centerBall.x;
dy = ball.y - centerBall.y;
dist = Math.sqrt(dx * dx + dy * dy);
minDist = ball.radius + centerBall.radius;
if (dist < minDist) {
angle = Math.atan2(dy, dx);
targetX = centerBall.x + Math.cos(angle) * minDist;
targetY = centerBall.y + Math.sin(angle) * minDist;
ball.vx += (targetX - ball.x) * spring * friction;
ball.vy += (targetY - ball.y) * spring * friction;
}
}
blurBalls(centerBall);
}
private function onHold(evt:MouseEvent):void {
centerBall.startDrag();
blurBalls(centerBall, false, true);
centerBall.addEventListener(MouseEvent.MOUSE_UP, onLetGo);
}
private function onLetGo(e:MouseEvent):void {
centerBall.stopDrag();
blurBalls(centerBall, true);
centerBall.removeEventListener(MouseEvent.MOUSE_UP, onLetGo);
}
private function nsChange(evt:Event):void {
var oldBalls:uint = balls.length;
numBalls = ns_numBalls.value as uint;
var newBalls:uint = numBalls;
var count:uint = Math.abs(oldBalls - newBalls);
if (newBalls > oldBalls) {
for (var i:uint = 0; i < count; i++) {
var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xffffff, 0xF0FD79, [0, 100]);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.vx = Math.random() * hiSpeed + lowSpeed;
ball.vy = Math.random() * hiSpeed * gravity + lowSpeed;
addChild(ball);
balls.push(ball);
}
} else if (newBalls < oldBalls) {
for (i = 0; i < count; i++) {
removeChild(balls.pop());
}
}
}
private function cbGravChange(evt:Event):void {
var e:String = cb_mass.value;
switch (e) {
case '1' :
mass = - 3;
break;
case '2' :
mass = 0;
break;
case '3' :
mass = 3;
break;
default :
mass = 0;
break;
}
}
private function cbFricChange(evt:Event):void {
var e:String = cb_friction.value;
switch (e) {
case '1' :
friction = 0.9;
break;
case '2' :
friction = 0.5;
break;
case '3' :
friction = 0.3;
break;
default :
friction = 0.5;
break;
}
}
private function cbBounceChange(evt:Event):void {
var e:String = cb_bounce.value;
switch (e) {
case '1' :
bounce = -0.2;
break;
case '2' :
bounce = -0.4;
break;
case '3' :
bounce = -0.7;
break;
case '4' :
bounce = -1;
break;
default :
bounce = -0.5;
break;
}
}
private function cbSpringChange(evt:Event):void {
var e:String = cb_spring.value;
switch (e) {
case '1':
spring = 0.10;
break;
case '2':
spring = 0.15;
break;
case '3':
spring = 0.3;
break;
case '4':
spring = 0.7;
break;
default:
spring = 0.15;
break;
}
}
// END: EVENT HANDLERS//
// START: MOTION AND PHYSICS GRAPHICS //
private function blurBalls(ball:Ball, stop:Boolean = false, keep:Boolean = false):void {
go = Math.random();
var range:Number = 10;
var frequency:Number = 0.4;
var flicker:Number = 5;
if (!stop && !keep && go < frequency) {
var blurX:Number = Math.cos(angle) * range + flicker;
var blurY:Number = Math.sin(angle * angle) * range + flicker;
} else if (!stop && keep) {
blurX = 50;
blurY = 50;
} else if (stop) {
blurX = 0;
blurY = 0;
}
var filterB:BlurFilter = new BlurFilter(blurX, blurY, BitmapFilterQuality.HIGH);
var filterArray:Array = new Array;
filterArray.push(filterB);
ball.filters = filterArray;
angle += angleSpeed;
}
private function move(ball:Ball):void {
// Create Boundaries
if (ball.x + ball.radius > right) {
ball.x = right - ball.radius;
ball.vx *= bounce;
} else if (ball.x - ball.radius < left) {
ball.x = left + ball.radius;
ball.vx *= bounce;
}
if (ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
ball.vy *= bounce;
} else if (ball.y - ball.radius < top) {
ball.y = top + ball.radius;
ball.vy *= bounce;
}
ball.x += (ball.vx * friction * gravity);
ball.y += (ball.vy * friction * gravity) + mass;
}
// END: MOTION AND PHYSICS GRAPHICS //
}
}
This is the code for the ball class that gets called in:
package com.chuayou {
import flash.display.Sprite;
import flash.display.GradientType;
public class Ball extends Sprite {
public var radius:Number;
public var color1:uint;
public var color2:uint;
public var ratios:Array;
public var focalPoint:Number;
public var vy:Number = 0;
public var vx:Number = 0;
public function Ball(radius:Number = 40, color2:uint = 0xCA2424, color1:uint = 0xF0FD79, ratio:Array = null, focalPoint:Number = 0.1) {
this.radius = radius;
this.color1 = color1;
this.color2 = color2;
ratios = ratio;
focalPoint = focalPoint;
init(focalPoint);
}
public function init(focalPoint):void {
var colors:Array = [color1, color2];
var alphas:Array = [1, 1];
var fp:Number = focalPoint
graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, null, null, null, fp);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
I know this is a TON of code to look over, but can anyone please help me! Its incredibly [COLOR=“DarkRed”]URGENT[/COLOR] that I resolve this ASAP!