Create a Flash game like Snowflakes – AS3 version
August 30, 2008 by Emanuele Feronato
Filed Under Actionscript 3, Flash, Game design • Leave a Comment
Filed Under Actionscript 3, Flash, Game design • Leave a Comment
As announced in Create a Flash game like Snowflakes, here it is the AS3 version.
I used the same comments to help you understanding the conversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | package {
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.*;
public class snowflakesas3 extends Sprite {
// max stars on stage
var max_stars = 20;
// current stars on stage
var stars_on_stage = 0;
// gravity
var gravity = 0.1;
// this is the influence distance
// using influence and real_influence I perform the square root only once
// when the distance from the mouse and the star is less than real_influence
// then the star is affected by the mouse
var influence = 625;
var real_influence = Math.sqrt(influence);
// friction
var friction = 0.9;
// divider, to make stars move slowly
var divider = 50;
// mouse speed
var mouse_speed = 0;
var playersprite:pointer = new pointer();
public function snowflakesas3() {
// mouse cursor replacement
Mouse.hide();
playersprite.addEventListener(Event.ENTER_FRAME,playersprite_enterframe);
addChild(playersprite);
addEventListener(Event.ENTER_FRAME,main_enterframe);
}
// function to be executed by the mouse pointer
public function playersprite_enterframe(event:Event) {
// calculating the distance from the last point the mouse was spotted
// and the current mouse position
var dist_x = playersprite.x-mouseX;
var dist_y = playersprite.y-mouseY;
mouse_speed = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
// minimum speed = minimum force applied
if (mouse_speed<0.2) {
mouse_speed = 0.2;
}// updating pointer position
playersprite.x = mouseX;
playersprite.y = mouseY;
}
// main function
public function main_enterframe(event:Event) {
// should I add a star?
if (stars_on_stage<max_stars) {
//adding a star
stars_on_stage++;
var starsprite: star = new star();
starsprite.xspeed = 0;
starsprite.yspeed = 0;
starsprite.x = Math.random()*450+25;
starsprite.y = Math.random()*50-100;
starsprite.addEventListener(Event.ENTER_FRAME,starsprite_enterframe);
addChild(starsprite);
}
}
// function the star will execute at every frame
public function starsprite_enterframe(event:Event) {
var current_star:star = (event.currentTarget as star);
// gravity
current_star.yspeed += gravity;
// calculating the distance from the star and the mouse
// without square roots
var dist_x = current_star.x-mouseX;
var dist_y = current_star.y-mouseY;
var distance = dist_x*dist_x+dist_y*dist_y;
// if we are in the radius of influence...
if (distance<influence) {
// ...apply a force to the star
// force is determined by mouse distance and speed
var xforce = mouse_speed*dist_x/divider;
var yforce = mouse_speed*dist_y/divider;
current_star.xspeed += xforce;
current_star.yspeed += yforce;
}
// adding friction
current_star.xspeed *= friction;
current_star.yspeed *= friction;
// updating position
current_star.y += current_star.yspeed;
current_star.x += current_star.xspeed;
// make the star rotate
current_star.rotation += (current_star.xspeed+current_star.yspeed);
// if the star reaches the bottom of the stage, remove it
if (current_star.y>300) {
stars_on_stage--;
current_star.removeEventListener(Event.ENTER_FRAME,starsprite_enterframe);
removeChild(current_star);
}
}
}
} |
There is no better way in learning a new language than porting your old projects into the new language.
I’ll write a post about it… meanwhile download the source code.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
They can be easily customized to meet the unique requirements of your project.
Leave a Reply
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(2 votes, average: 4.50 out of 5)




