-
Create fluent motion (CS3 & AS3)
First of all, this is my first time working with flash, so I'm a beginner at flash. Today I created my first flash file, located here http://flash.knobbout.com/brick/
As you can see, the motion of the ball and the slider is a bit jaggy, this is because I am increasing the x and y values by 8. If I lower this value the ball and slider will move too slow. How can I create a fluent motion (which is very common in flash movies) in my movie? Here is the source code (fps of movie is set to 30):
Code:
// Variables
var slider_speed:int;
var ball_speed:int;
var ball_direction:Number;
// Events
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressHandler);
stage.addEventListener(Event.ENTER_FRAME, updateBall);
// Reset the game once
resetGame();
function resetGame():void {
// Default ball properties
ball_speed = 8;
ball_direction = Math.PI * -7/9;
ball_mc.x = 40;
ball_mc.y = 80;
ball_mc.width = 20;
ball_mc.height = 20;
// Default slider properties
slider_speed = 8;
slider_mc.x = 80;
slider_mc.y = 380;
slider_mc.width = 120;
slider_mc.height = 10;
}
function updateBall(handler:Event):void {
var newX = ball_mc.x + ball_speed * Math.sin(ball_direction);
var newY = ball_mc.y + ball_speed * Math.cos(ball_direction);
// Prevent ball from exiting stage
if ((newX < 0) || (newX + ball_mc.width > stage.stageWidth)) {
ball_direction *= -1;
}
if (newY < 0) {
ball_direction = (ball_direction < 0 ? -1 : 1) * (Math.PI - Math.abs(ball_direction));
}
if (newY + ball_mc.height > slider_mc.y) {
// Check if user has bounced the ball
if ((newX > slider_mc.x) && (newX < slider_mc.x + slider_mc.width)) {
ball_direction = (ball_direction < 0 ? -1 : 1) * (Math.PI - Math.abs(ball_direction));
}
else {
// Game Over
resetGame();
return;
}
}
// Set new X and Y
ball_mc.x = newX;
ball_mc.y = newY;
}
function keypressHandler(handler:KeyboardEvent):void {
// Move slider either left or right
if (handler.keyCode == 37) {// LEFT
slider_mc.x -= slider_speed;
}
if (handler.keyCode == 39) {// RIGHT
slider_mc.x += slider_speed;
}
// Prevent slider from going off-screen
if (slider_mc.x < 0) {
slider_mc.x = 0;
}
if (slider_mc.x > stage.stageWidth - slider_mc.width) {
slider_mc.x = stage.stageWidth - slider_mc.width;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|