Striker9099
10-01-2009, 06:18 PM
Hello there. I am trying to do something like the rolling ball on this site: Eye Digits (http://www.eyedigits.com) but I just can't get it right. So far I was able to move the ball with the mouse... I first got it to rotate but not exactly the way I wanted so I tried easing the rotation but didn't get any luck either... so now I'm stuck, I want the speed of rotation to increase as the speed of movement increases and when the mouse isn't moving (and thus the ball isn't) I want the rotation to stop... (I tried to create a boolean to check for the ball's movement but couldn't incorporate it into the code properly) anyway, here's my ball class and my document class:
Ball Class:
package {
import flash.display.Graphics;
import flash.display.Sprite;
public class Ball extends Sprite {
private var radius:Number;
private var color:uint;
public function Ball(radius:Number=50, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
private function init():void {
var gr:Graphics = this.graphics;
gr.beginFill(color);
gr.drawCircle(0, 0, radius);
gr.endFill();
gr.beginFill(0xffffff);
gr.drawEllipse(-7, -radius + 4, 14, 6);
gr.endFill();
}
}
}
Document Class:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class RollingBall extends Sprite
{
private var ball:Ball;
private var radius:Number = 50;
private var rot:Number = 0;
private var easing:Number = 0.1;
private var moving:Boolean = false;
private var rotV:Number = 0;
// boundaries
private var left:Number;
private var right:Number;
// velocity
private var vx:Number = 5;
public function RollingBall()
{
init();
}
private function init():void {
ball = new Ball(radius, 0x000000);
ball.x = radius + 2;
ball.y = stage.stageHeight - radius;
addChild(ball);
rot = 360 * vx / (Math.PI * radius * 2);
left = radius;
right = stage.stageWidth - radius;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
if (ball.x >= right || ball.x <= left) {
vx *= -1;
rot *= -1;
}
rotV = (ball.rotation - rot) * easing;
vx = ((mouseX - 20) - ball.x) * easing;
ball.x += vx;
if (mouseX > ball.x) {
ball.rotation += rotV;
} else if(mouseX < ball.x) {
ball.rotation -=rotV;
}
}
}
}
Any help would be appreciated :]
Ball Class:
package {
import flash.display.Graphics;
import flash.display.Sprite;
public class Ball extends Sprite {
private var radius:Number;
private var color:uint;
public function Ball(radius:Number=50, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
private function init():void {
var gr:Graphics = this.graphics;
gr.beginFill(color);
gr.drawCircle(0, 0, radius);
gr.endFill();
gr.beginFill(0xffffff);
gr.drawEllipse(-7, -radius + 4, 14, 6);
gr.endFill();
}
}
}
Document Class:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class RollingBall extends Sprite
{
private var ball:Ball;
private var radius:Number = 50;
private var rot:Number = 0;
private var easing:Number = 0.1;
private var moving:Boolean = false;
private var rotV:Number = 0;
// boundaries
private var left:Number;
private var right:Number;
// velocity
private var vx:Number = 5;
public function RollingBall()
{
init();
}
private function init():void {
ball = new Ball(radius, 0x000000);
ball.x = radius + 2;
ball.y = stage.stageHeight - radius;
addChild(ball);
rot = 360 * vx / (Math.PI * radius * 2);
left = radius;
right = stage.stageWidth - radius;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
if (ball.x >= right || ball.x <= left) {
vx *= -1;
rot *= -1;
}
rotV = (ball.rotation - rot) * easing;
vx = ((mouseX - 20) - ball.x) * easing;
ball.x += vx;
if (mouseX > ball.x) {
ball.rotation += rotV;
} else if(mouseX < ball.x) {
ball.rotation -=rotV;
}
}
}
}
Any help would be appreciated :]