PDA

View Full Version : Wierdness


jeremye
02-08-2009, 07:53 AM
I'm having a weird problem.

I just recently started messing with Flash
and i'm working on my hello type app
which is basically some balls that bounce around
the stage colliding with the bounds of the stage.

I have a class that takes care of the creation, moving,
colliding of the ball. Each ball I create spawns at x/y 10,10 on
the stage.

If I change the spawn point from 10,10 it's like the stage moves
the ball collides one time and then floats off into the void
and i'm not sure why.

I am using Flash CS4 and FlashDev so it could be something simple
like not having it setup right. (I read the docs though and everything looks good)

The layout of the code isn't perfect.



package
{
import fl.motion.Color;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;

public class BouncingBallsMain extends MovieClip
{
public function BouncingBallsMain():void
{
createBall();
createBall();
createBall();
createBall();
createBall();
}

public function createBall()
{
var ball:BouncingBallsMove = new BouncingBallsMove();
addChild(ball);
}
}
}



package
{
import flash.display.Sprite;
import flash.events.Event;

public class BouncingBallsMove extends Sprite
{
private var x_direction:Number = Math.random() * 10 + 1;
private var y_direction:Number = Math.random() * 10 + 1;
private var speed:Number = Math.floor(Math.random() * 5) + 1;
private var radius:Number = 10;

public function BouncingBallsMove()
{
this.graphics.beginFill(0x0000FF);
this.graphics.drawCircle(10, 10, radius);
this.addEventListener(Event.ENTER_FRAME, moveBalls)
}

private function moveBalls(event:Event):void
{
event.currentTarget.x += x_direction;
event.currentTarget.y += y_direction;

if (event.currentTarget.x <= 0)
{
x_direction = -x_direction;
}
else if (event.currentTarget.x >= stage.stageWidth - event.currentTarget.width)
{
x_direction = -x_direction;
}
if (event.currentTarget.y <= 0)
{
y_direction = -y_direction;
}
else if (event.currentTarget.y >= stage.stageHeight - event.currentTarget.height)
{
y_direction = -y_direction;
}
}
}

}




this.graphics.drawCircle(10, 10, radius);


If I change this like from 10,10 to something else, that
is when the problems kick in.