Create a Flash game like Snowflakes – AS3 version
- August 30, 2008 by Emanuele Feronato
- 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
TUTORIAL SERIES:
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online

(2 votes, average: 4.50 out of 5)
