Playing with getPixel
Today I played a bit with getPixel function and I want to share with you some considerations about it.
First, you have to know getPixel(x,y) returns an integer that represents an RGB pixel value from a BitmapData object at a specific point (x, y).
Then, I wanted to know the percentage of a color in an image, maybe to manage level filling in a game like ColorFill (when using complex shapes to fill the level) or maybe to analyze the percentage of a color in a photo.
So I made this picture

and I linked it as bg.
Then I wrote this little actionscript
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 | import flash.display.BitmapData;
var bitmap_bg:BitmapData = BitmapData.loadBitmap("bg");
_root.createEmptyMovieClip("background_image",_root.getNextHighestDepth());
background_image.attachBitmap(bitmap_bg,_root.getNextHighestDepth());
precision = 1;
r = 0;
g = 0;
b = 0;
other = 0;
total = 0;
for (x=0; x<500; x += precision) {
for (y=0; y<400; y += precision) {
switch (bitmap_bg.getPixel(x, y).toString(16)) {
case "ff" :
b++;
break;
case "ff00" :
g++;
break;
case "ff0000" :
r++;
break;
default :
other++;
}
total++;
}
}
r = r/total*100;
g = g/total*100;
b = b/total*100;
other = other/total*100;
trace("Red: "+r+"%\nGreen:"+g+"%\nBlue: "+b+"%\nOhter: "+other+"%"); |
It’s a very easy one:
Line 1: importing BitmapData library
Line 2: declaring a BitmapData variable called bitmap_bg containing the image linked as bg
Line 3: creating an empty movie clip where I will attach the bitmap image loaded at line 2
Line 4: attaching the bitmap, as said at line 3
Line 5: Setting a variable called precision. This is the core of the script. precision indicates the gap in pixels from the last examined pixel and the next pixel I am going to examine. The smaller precision value, the more accurate the percentage, the slower the script.
Lines 6-10: Initializing variables counting the amount of red, green and blue pixels found, and the total of pixels examined. I also count the pixels of other colors. Even if you only see a blue background, a red circle and a green heart, there are other colors due to antialiasing.
Lines 11-28: Scanning all pixels with the precision gap between one pixel and another, and incrementing the color variable according to the color found at line 13.
Lines 29-33: Formatting and showing the results
Look how results change when I play with precision
Precision: 1
Red: 8.91%
Green:7.9145%
Blue: 82.5735%
Ohter: 0.602%
Precision: 2
Red: 8.91%
Green:7.916%
Blue: 82.578%
Ohter: 0.596%
Precision: 5
Red: 8.9125%
Green:7.8625%
Blue: 82.55%
Ohter: 0.675%
Precision: 10
Red: 8.8%
Green:7.95%
Blue: 82.7%
Ohter: 0.55%
Precision: 20
Red: 8.8%
Green:8%
Blue: 82.6%
Ohter: 0.6%
As you can see, you don’t need a low (and cpu expensive) precision value to obtain realistic results.
Download the source, maybe changing the image, and tell me what do you think about. Any idea for a game?
Win a TemplateMonster theme with TemplateMonster Lottery
If you are looking for a fresh and professional template for your personal site, Flash game portal or a site for a web design job, then you should take a look at TemplateMoster.
Now you have a chance to win one of these templates thanks to the TemplateMonster lottery.
Let’s see what does it mean from the mouth (well, the keyboard) of Helen:
TemplateMonster.com is going to hold a lottery with give-away to any participant.
The lottery is going to be about the upcoming 4th of July.
And here are the details of TemplateMonster Lottery which reminds elections in its structure.
We’ll expose two of TemplateMonster’s website designs. In order to take part in the lottery one should vote for one design or the other.
The design which gets the most votes becomes the winner. Note the best design is chosen during two weeks period.
And here are the benefits for users to take part in our lottery:
- everyone who participates in our lottery gets the free Icon Set. All icons included into this free Icon Set also relate to patriotic and 4th of July topic;
- also everyone who participates in our lottery has a chance to get one of our special prizes.
The special prizes are:
- 1 Gift Certificate giving the opportunity to get any product from Template Monster database for free;
- two 80% discount coupons for purchasing any product from Template Monster database;
- three 50% discount coupons for purchasing any product from Template Monster database.
Note everyone who purchases any product from Template Monster during this promo will get the Icon Set that I’ve mentioned for free.
And now, the link: 4th July Lottery 2008.
That’s it… and if you don’t understand what’s the point of giving away patriotic icons on July 4th, you should watch Independence Day movie.
Looking for your experience for a case history
I need some of your experiences in order to build a case history section in this blog and for an article do be published on a paper magazine… and maybe for something else.
If you think this blog changed your way of developing and monetizing Flash games, or just made you love Flash game developing, then please leave a comment with your first name, the first letter of your last name and the Country you are from (example: Emanuele F. – Italy)
And, of course, your experience.
Thank you very much.
Create a Flash game like ColorFill – part 1
If you play all successful Flash games (and you should, if you want to be a Flash game developer), surely you played ColorFill.
In this game, you have to fill 80% of the stage with colors while avoiding collisions with enemies.
Play the game a bit then follow this prototype
The main idea to create this game is based upon the “vertical state” of arrows. I designed the arrows as horizontal ones, but if the player presses SPACE, I rotate them by 90 degrees so the left one becomes the up one and the right one becomes the down one.
There’s not any more difficulty in this first part, when I still don’t fill the area once I successfully draw a line.
Some previous concepts from Create a flash draw game like Line Rider or others – part 2 are involved in order to determine collisions between the ball and the line. Read more
Step by step AS3 translation of Creation of a platform game with Flash – step 1.5
According to Bart de Boer, this is the step 1.5 of the Step by step AS3 translation of Creation of a platform game with Flash.
He fixed the jump issue and improved the collision engine.
As in the previous example, the file Script.as is the main class:
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | /*____________________________________________________
|______________ register of functions _______________|
|____________________________________________________|
- main() only calling to update_hero() (every frame)
- create_hero() creates hero as the var "Hero"
- update_hero() check collision an move
- BuildMap() load and create the level
extern
Data.as
- Setup() create levels
collision_manager.as
- Setup(size,map,hero) setup the map
- Solve_all(forecastx,forecasty) solves the collsions and checks for a jump
*/
package{ //The begin of an .as file
import flash.display.MovieClip; //import some libraries
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Script extends MovieClip{ // start the script
private const gravity:int = 1;
private const max_speed:int = 8;
private const walkspeed:int = 4;
private const jumpspeed:int = 10;
private var forecast_x:int;
private var forecast_y:int;
private const start_x:int = 50;
private const start_y:int = 50;
private var left:Boolean;
private var up:Boolean;
private var right:Boolean;
private var space:Boolean;
private var level:Array = new Array();
private var tiles:Array = new Array();
private var Map_data:Data = new Data; // create a version of the Data.as
private var Hero_col:collision_manager = new collision_manager;
private var Hero:hero = new hero;
public function Script(){ // the init (will only be runned once)
BuildMap();
create_hero();
addEventListener(Event.ENTER_FRAME, main);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
Hero_col.Setup(25,level,Hero);
}
private function main(event:Event){
update_hero();
}
private function key_down(event:KeyboardEvent){
if(event.keyCode == 37){
left = true;
}
if(event.keyCode == 38){
up = true;
}
if(event.keyCode == 39){
right = true;
}
}
private function key_up(event:KeyboardEvent){
if(event.keyCode == 37){
left = false;
}
if(event.keyCode == 38){
up = false;
}
if(event.keyCode == 39){
right = false;
}
}
/*
/// /// ///////// /////////// ///////////
/// /// /// //// /// /// ///
/// /// /// //// /// /// ///
////////// ///////// ////////// /// ///
////////// ///////// //// /// /// ///
/// /// /// //// /// /// ///
/// /// /// //// /// /// ///
/// /// ///////// //// /// ///////////
*/
private function create_hero(){
addChild(Hero);
Hero.x = start_x;
Hero.y = start_y;
Hero.x_speed = 0;
Hero.y_speed = 0;
}
private function update_hero(){
Hero.y_speed += gravity;
if(left){
Hero.x_speed = -walkspeed;
}
if(right){
Hero.x_speed = walkspeed;
}
if(up && Hero_col.can_jump){
Hero.y_speed = -jumpspeed;
}
if(Hero.y_speed > max_speed){
Hero.y_speed = max_speed;
}
forecast_y = Hero.y + Hero.y_speed;
forecast_x = Hero.x + Hero.x_speed;
Hero_col.solve_all(forecast_x, forecast_y);
Hero.x_speed = 0;
}
/*
||||||||||| ||||||||||| |||||||||| ||||||||||
|||||||||||| |||||||||||| |||| |||| |||| |||
|||| ||||| ||||| |||| |||||||||||| ||||||||||
|||| ||||||| |||| |||| |||| ||||
|||| ||| |||| |||| |||| ||||
|||| |||| |||| |||| ||||
*/
private function BuildMap(){
Map_data.Setup(); // setup data from extern file
level = Map_data.level1; // get data from extern file
for(var t = 0; t < level.length; t++){
for(var u = 0; u < level[t].length; u++){
if(level[t][u] != 0){ //if the data is not null
var new_tile:platform_tile = new platform_tile; //than build a tile
addChild(new_tile); //put it on the screen
new_tile.gotoAndStop(1);
new_tile.x = u * 25;
new_tile.y = t * 25;
tiles.push(new_tile); //put it in an array
}
}
}
}
}
}
// You may not post this on any other site than: http://www.emanueleferonato.com or http://www.frozenhaddock.com. You may not claim that you wrote this code. I'm not responsible for any nuclear activity that may be caused by this script. |
The level is stored in the Data.as
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 | package{
public class Data{
public var level1:Array = new Array();
public function Setup(){
level1 = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
}
}
}
// You may not post this on any other site than: http://www.emanueleferonato.com or http://www.frozenhaddock.com. You may not claim that you wrote this code. I'm not responsible for any nuclear activity that may be caused by this script. |
While the solve_all function contained in the collision_manager.as solves all collisions in the forecast position of the player
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | package{
public class collision_manager{
private var Tile_size:int; //containing the size of the tiles
private var level; //level data container
private var forecast_x:int; //where the player will be at the end of the frame
private var forecast_y:int; //" "
public var can_jump:Boolean; //same as in Emanuele's tutorial
private var hero; //to store the Hero object in
public function Setup(size,map,heroj){ //my standard setup function(init)
Tile_size = size; //initializing al vars
level = map;
hero = heroj;
}
public function get_corners(point_x,point_y){
//get the position of the four corners of the hero
hero.downy = Math.round((point_y + 10 - Tile_size/2) / Tile_size);
hero.upy = Math.round((point_y - 10 - Tile_size/2) / Tile_size);
hero.rightx = Math.round((point_x + 5 - Tile_size/2) / Tile_size)
hero.leftx = Math.round((point_x - 5 - Tile_size/2) / Tile_size);
/*
Looks in wich tiles these four point are.
*Attention* TILES and not pixel!
Will be used to get the position of the corners
and to get the end position of the hero.
*/
hero.downleft = level[hero.downy][hero.leftx];
hero.downright = level[hero.downy][hero.rightx];
hero.upright = level[hero.upy][hero.rightx];
hero.upleft = level[hero.upy][hero.leftx];
/*
Gets the sort tile the position of the corners has.
One means there can be collision, zero is air.
*/
}
public function check_ground(){
hero.downy = Math.round((hero.y + 11 - Tile_size/2) / Tile_size);
hero.rightx = Math.round((hero.x + 5 - Tile_size/2) / Tile_size)
hero.leftx = Math.round((hero.x - 5 - Tile_size/2) / Tile_size);
// Makes three points in tile-coordinates
hero.downleft = level[hero.downy][hero.leftx];
hero.downright = level[hero.downy][hero.rightx];
//Checks the sort
if(hero.downleft == 1 || hero.downright ==1){ //if there is any collision by the hero's feet
can_jump = true; //than can jump
}
else{
can_jump = false; //else not
}
}
public function solve_all(forecastx, forecasty){
/*
This function looks hard... an it is...
I've been working on it for loads of hours, and this is the result.
This is the best collision-test I've ever made, for sqaures.
It's still readable, because I made it simple.
It's four times almost the same. It checks for collision between the spots,
the four spots if there only were x_speeds, the four spots if there only was y_speed,
the four spots if there were both y- and x_speed.
That are 3*4 = 12 spots.
//////////////////////////////////
Why's:
question:
Why did I used forecast_x and forecast_y and not just hero.x and hero.y?
answer:
Changing the hero over the screen will cost loads of CPU because an hero
exist out of loads of pixels. The forecast's are just simple variables
question:
Is such a huge function not requiring a lot of CPU?
answer:
It doesn't uses any while- or for loops, Math. functions and the most important of
all: no hitTestPoint()/hitTestObject(). So it's a lot faster.
question:
Is there a better or faster collision test?
answer:
Probaly, yes.
//////////////////////////////////
var-explanation:
downC -> is collision under the spot
upC -> is collision above the spot
rightC -> is collision right from the spot
leftC -> You probaly wouldn't expect this, but this is collision left from the spot
*/
forecast_x = forecastx;
forecast_y = forecasty;
get_corners(forecast_x,forecast_y);
if(hero.downleft == 1){
get_corners(hero.x,forecast_y);
if(hero.downleft == 1){
hero.downC = true;
}
else{
hero.downC = false;
}
get_corners(forecast_x,hero.y);
if(hero.downleft == 1){
hero.leftC = true;
}
else{
hero.leftC = false;
}
if(hero.leftC && hero.downC){
forecast_x = (hero.leftx+1) * 25 + 5;
forecast_y = (hero.downy+1) * 25 - 11;
hero.y_speed = 0;
}
else if(hero.leftC){
forecast_x = (hero.leftx+1) * 25 + 5;
}
else if(hero.downC){
forecast_y = (hero.downy+1) * 25 - 11;
hero.y_speed = 0;
}
}
get_corners(forecast_x,forecast_y);
if(hero.downright == 1){
get_corners(hero.x,forecast_y);
if(hero.downright == 1){
hero.downC = true;
}
else{
hero.downC = false;
}
get_corners(forecast_x,hero.y);
if(hero.downright == 1){
hero.rightC = true;
}
else{
hero.rightC = false;
}
if(hero.rightC && hero.downC){
forecast_x = hero.rightx * 25 - 6;
forecast_y = (hero.downy+1) * 25 - 11;
hero.y_speed = 0;
}
else if(hero.rightC){
forecast_x = hero.rightx * 25 - 6;
}
else if(hero.downC){
forecast_y = (hero.downy+1) * 25 - 11;
hero.y_speed = 0;
}
}
get_corners(forecast_x,forecast_y);
if(hero.upleft == 1){
get_corners(hero.x,forecast_y);
if(hero.upleft == 1){
hero.upC = true;
}
else{
hero.upC = false;
}
get_corners(forecast_x,hero.y);
if(hero.upleft == 1){
hero.leftC = true;
}
else{
hero.leftC = false;
}
if(hero.leftC && hero.upC){
forecast_x = (hero.leftx+1) * 25 + 5;
forecast_y = (hero.upy) * 25 + 10;
hero.y_speed = 0;
}
else if(hero.leftC){
forecast_x = (hero.leftx+1) * 25 + 5;
}
else if(hero.upC){
forecast_y = (hero.upy) * 25 + 10;
hero.y_speed = 0;
}
}
get_corners(forecast_x,forecast_y);
if(hero.upright == 1){
get_corners(hero.x,forecast_y);
if(hero.upright == 1){
hero.upC = true;
}
else{
hero.upC = false;
}
get_corners(forecast_x,hero.y);
if(hero.upright == 1){
hero.leftC = true;
}
else{
hero.leftC = false;
}
if(hero.leftC && hero.upC){
forecast_x = hero.rightx * 25 - 6;
forecast_y = (hero.upy) * 25 + 10;
hero.y_speed = 0;
}
else if(hero.leftC){
forecast_x = hero.rightx * 25 - 6;
}
else if(hero.upC){
forecast_y = (hero.upy) * 25 + 10;
hero.y_speed = 0;
}
}
hero.x = forecast_x; //place the hero
hero.y = forecast_y;
check_ground() //runs the function to check for ground
}
}
}
// You may not post this on any other site than: http://www.emanueleferonato.com or http://www.frozenhaddock.com. You may not claim that you wrote this code. I'm not responsible for any nuclear activity that may be caused by this script. |
Now we have a working AS3 platform engine
Thanks to Bart de Boer for providing the source code of this prototype. Download it and start making your own AS3 platform game.
Step by step AS3 translation of Flash game creation tutorial – part 2
This is the second part of Tim Edelaar‘s step by step AS3 translation of Flash game creation tutorial.
Now it’s time to translate to AS3 the content of Flash game creation tutorial – part 2 which I suggest you to read before this one.
The style of the coding is the same of the previous part, with actionscript written directly in the timeline with the critical parts commented.
The bounds
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | //import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
//create the function main.
function Main(event:Event){
CheckKeys();
MoveHero();
CheckCollision();
}
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is pressed.
key_left = true;
}
if(event.keyCode == 39){ //checks if right arrowkey is pressed.
key_right = true;
}
if(event.keyCode == 38){ //checks if up arrowkey is pressed.
key_up = true;
}
if(event.keyCode == 40){ //checks if down arrowkey is pressed.
key_down = true;
}
}
function KeyUp(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is released.
key_left = false;
}
if(event.keyCode == 39){ //checks if right arrowkey is released.
key_right = false;
}
if(event.keyCode == 38){ //checks if up arrowkey is released.
key_up = false;
}
if(event.keyCode == 40){ //checks if down arrowkey is released.
key_down = false;
}
}
function CheckKeys(){
if(key_left){
xspeed -= speed;
}
if(key_right){
xspeed += speed;
}
if(key_up){
yspeed -= speed*thrust;
}
if(key_down){
yspeed += speed*thrust;
}
}
function MoveHero(){
hero.x += xspeed;
hero.y += yspeed;
xspeed *= friction;
yspeed += gravity;
yspeed *= friction;
hero.rotation += xspeed;
}
function CheckCollision(){
//Checks if left border hits the wall.
if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if right border hits the wall.
if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if upper border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if lower border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
} |
The coin – 1st attempt
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | //import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
//create the function main.
function Main(event:Event){
CheckKeys();
MoveHero();
CheckCollision();
}
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is pressed.
key_left = true;
}
if(event.keyCode == 39){ //checks if right arrowkey is pressed.
key_right = true;
}
if(event.keyCode == 38){ //checks if up arrowkey is pressed.
key_up = true;
}
if(event.keyCode == 40){ //checks if down arrowkey is pressed.
key_down = true;
}
}
function KeyUp(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is released.
key_left = false;
}
if(event.keyCode == 39){ //checks if right arrowkey is released.
key_right = false;
}
if(event.keyCode == 38){ //checks if up arrowkey is released.
key_up = false;
}
if(event.keyCode == 40){ //checks if down arrowkey is released.
key_down = false;
}
}
function CheckKeys(){
if(key_left){
xspeed -= speed;
}
if(key_right){
xspeed += speed;
}
if(key_up){
yspeed -= speed*thrust;
}
if(key_down){
yspeed += speed*thrust;
}
}
function MoveHero(){
hero.x += xspeed;
hero.y += yspeed;
xspeed *= friction;
yspeed += gravity;
yspeed *= friction;
hero.rotation += xspeed;
}
function CheckCollision(){
//Checks if left border hits the wall.
if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if right border hits the wall.
if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if upper border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if lower border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Now do the same for our coin.
if(coin.hitTestPoint(hero.x,hero.y,true)){
coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
}
} |
The coin – 2nd attempt
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | //import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
//create the function main.
function Main(event:Event){
CheckKeys();
MoveHero();
CheckCollision();
}
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is pressed.
key_left = true;
}
if(event.keyCode == 39){ //checks if right arrowkey is pressed.
key_right = true;
}
if(event.keyCode == 38){ //checks if up arrowkey is pressed.
key_up = true;
}
if(event.keyCode == 40){ //checks if down arrowkey is pressed.
key_down = true;
}
}
function KeyUp(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is released.
key_left = false;
}
if(event.keyCode == 39){ //checks if right arrowkey is released.
key_right = false;
}
if(event.keyCode == 38){ //checks if up arrowkey is released.
key_up = false;
}
if(event.keyCode == 40){ //checks if down arrowkey is released.
key_down = false;
}
}
function CheckKeys(){
if(key_left){
xspeed -= speed;
}
if(key_right){
xspeed += speed;
}
if(key_up){
yspeed -= speed*thrust;
}
if(key_down){
yspeed += speed*thrust;
}
}
function MoveHero(){
hero.x += xspeed;
hero.y += yspeed;
xspeed *= friction;
yspeed += gravity;
yspeed *= friction;
hero.rotation += xspeed;
}
function CheckCollision(){
//Checks if left border hits the wall.
if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if right border hits the wall.
if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if upper border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if lower border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Now do the same for our coin.
if(hero.hitTestObject(coin)){
coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
}
} |
The coin – 3rd attempt
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | //import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
//create the function main.
function Main(event:Event){
CheckKeys();
MoveHero();
CheckCollision();
}
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is pressed.
key_left = true;
}
if(event.keyCode == 39){ //checks if right arrowkey is pressed.
key_right = true;
}
if(event.keyCode == 38){ //checks if up arrowkey is pressed.
key_up = true;
}
if(event.keyCode == 40){ //checks if down arrowkey is pressed.
key_down = true;
}
}
function KeyUp(event:KeyboardEvent){
if(event.keyCode == 37){ //checks if left arrowkey is released.
key_left = false;
}
if(event.keyCode == 39){ //checks if right arrowkey is released.
key_right = false;
}
if(event.keyCode == 38){ //checks if up arrowkey is released.
key_up = false;
}
if(event.keyCode == 40){ //checks if down arrowkey is released.
key_down = false;
}
}
function CheckKeys(){
if(key_left){
xspeed -= speed;
}
if(key_right){
xspeed += speed;
}
if(key_up){
yspeed -= speed*thrust;
}
if(key_down){
yspeed += speed*thrust;
}
}
function MoveHero(){
hero.x += xspeed;
hero.y += yspeed;
xspeed *= friction;
yspeed += gravity;
yspeed *= friction;
hero.rotation += xspeed;
}
function CheckCollision(){
//Checks if left border hits the wall.
if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if right border hits the wall.
if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if upper border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Checks if lower border hits the wall.
if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
hero.x = 275;
hero.y = 200;
xspeed = 0;
yspeed = 0;
}
//Now do the same for our coin.
if(hero.hitTestPoint(coin.x,coin.y,true)){
coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
}
} |
And that’s all for today.
Download source codes and thank Tim Edelaar for the contribution
First Triqui’s milestone: 250,000 plays – considerations
This is the first milestone for Triqui: 250,000 plays.
I know there are portals that reach that number in a day (or less), anyway for a portal made in a few hours, I think it’s an important step.
If you want to know more about Triqui, I suggest you to read these posts:
Ramblings about Flash game portals
Play Flash games on Triqui.com
More ramblings about Flash game portals
And now it’s time to show you something about Triqui:
Ads placements
At the moment I am trying to monetize Triqui only with Adsense, so I placed one ad in the homepage and one ad in the page where you can play games.
The ads are placed in two very different areas, I made this in order to collect some data about interaction between the user and the ads.
As you can see from the picture, in the first page the ad is at the very top of the screen, while in the second one it’s in the middle of the page.

The theory is: very few people will look at the first ad because they are looking at the games, and a lot of people will look at the second one because it’s just under the game they are playing (and watching)
Let’s see some numbers:
Home page CTR: 0.12
Game page CTR: 1.31
Home page eCPM: 0.14
Game page eCPM: 2.22
Obviously the game page ad is a lot more profitable than the home one.
Some of you may argue some clicks in the game page may be caused accidentally because the ad is too close to the game.
Yes… maybe… but I don’t think this represents more than 10% of the total clicks.
Driving traffic
The only way I promoted Triqui was through this blog, and with two self-sponsored games: Jamag and Bees n’ Flowers.
According to Google analytics, this is the chart of the visits to Triqui Triqui when the games got the frontpage of an important portal (no matter which portal).

As you can see, the “portal effect” is quite strong, and if you consider an average 5 plays per user, in its best day the portal scored 7,259 visitors and more than 35,000 plays with a 2.22 eCPM.
Not that bad, if you consider there is still no reason to return to Triqui once you visited it.
Actually my plan is breaking the average 1,000 daily visitors, with some new features to be added soon.
Again, I know 1,000 visitors is nothing, but the entire portal is nothing, don’t forget there are no more than 100 php lines.
Search Engines
I did not that much to rank in search engines, but in some cases I had some nice surprises… I am the first relevant result for umbrella trick and the second one for operation darkness strike.
I am not talking about Bloons, but they drive some traffic to the site, and that’s enough at the moment.
Final ramblings
Remember… if you can make some money developing Flash games, you can make even more money hosting a Flash gaming portal. You just need to drive some traffic on it and have something that will make visitors return.
Now I am planning a rating/reviewing system to the portal, just to see if I manage to fidelize some players.
Meanwhile, I would like to hear the voice of some small portal owners.
What to do when you are looking for pixel graphics for your game and run out of ideas
You are trying to design some decent pixel graphics for your tile based game and all you have is a Photoshop document with some crap on it.
How many times did it happen in your life?
Here they are a couple of clues to help you in this task.
Once upon a time…
Look what I found on Wikipedia:
“RPG Maker (1st release: December 17, 1992) is a program that allows users to create their own role-playing games. Most versions include a tile set based map editor, (tilesets are called chipsets in pre-XP versions), a simple scripting language for scripting events, and a battle editor. All versions include initial premade tilesets, characters, and events which can be used in creating new games. An interesting feature of the PC Versions of RPG Maker programs is that a user can create new tilesets and characters, and add any new graphics he/she wants. Several sites have developed, dedicated to helping users share their creations.”
Now the question is: so what?
Well, read once again this: “An interesting feature of the PC Versions of RPG Maker programs is that a user can create new tilesets and characters, and add any new graphics he/she wants”.
So if you google for rpg maker chipset images or rpg maker tileset images, you will find a lot of sites with free tile based pixel art for your use or inspiration.
And it’s not over… there is a site called Charas containing an online character generator.
As you can see from the picture, it’s very easy to have your character with a 4 way walking animation in a matter of seconds, with thousands of options to custom it.

I hope this hint will make you save some time when you are looking for pixel graphics and run out of creativity. It saved mine for sure.
Adding a new twist to the prototype of a Flash game like Poux
Do you remember the Poux tutorial series?
If not, then read parts 1, 2 and 3 and take a look at the finished project known as Christmas Couples.
I always say using prototypes to create games is a good idea, and the almost 7,000,000 plays Christmas Couples had since its release confirm my theory.
Anyway, the “twist” is you must remove adjacent tiles by drawing on them.
You must connect two or more adjacent tiles with the same color with the “pen”, and if you have the precision of a surgeon, you can also connect tiles diagonally!
Oh, well, take the commented actionscript Read more
How to generate friendly URLs with .htaccess – part 2
Once you read part 1, you should know how to have a friendly url to play a game in your Flash game portal.
In the previous example, I showed you how to transform this link:
http://www.triqui.com/play.php?id=1713
into this one:
http://www.triqui.com/id/1713/
playing with .htaccess file
This is called “friendly” url, and now I’ll show you how to improve it. Starting from…
Your database game table
There are thousands of ways you can store your games data into your database, but all should follow a rule that in the table that stores your games information you have a column where you saved the game name.
No matter if you called the table “games” or “entries” or “bananas” and you called the column “game_name” or “title” or “apples”… you should have a table with all games with a column for the game name. Read more
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



