Create a Flash game like Snowflakes – AS3 version
Talking about Snowflakes game, Actionscript 3, Flash and Game development.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
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.
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_stage300) {
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.