Category Archives: AS3

Handle the Right Click in AS3

It can not be denied that handling the Right Click in Flash is a problem.
At first, it was made to display a Contextual Menu with Flash Player’s options, this is great for a website. However Flash is also used to make browser’s games and it is reductive if we can’t use this. For example in a Point & Click game, like the mythical Diablo, the Right Click is very important and simply to use. If you want to develop that kind of game with Flash (one of my dream ;-)) it will be difficult to manage this. In this direction, old Apple’s mouses with their only click were preposterous !! I think that was a commercial argument like “our Operating Systems is so ergonomic that you don’t need an alternative button”. But if you used software like Photoshop, you will always push the ctrl button on the keyboard, but also shift for other action or alt, cmd…

So today I try to look over that. And what I notice is :
– the right click and the middle click are functional in AS3 (those MouseEvent exist) but work only for Air !!
– you can easily change the context menu options (good example) and add functions but About and Settings can not be removed from the menu !!
– use JavaScript to handle the Right Click thanks to this class. Here we go :

I created a .swf in which with a right click you create a circle, and with a left click on a circle delete it (the application). Note it seems it is no more updated. But what happens ? It doesn’t work with Safari and Opera… failed. And you should have notice that I created a circle by pressing the right button, logically it should be the left… ?? Because of the JavaScript, you can’t give an occurence through it to Flash, so how can I obtain the target ?
This is the code :

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
var circle:Circle;
 
stage.addEventListener(MouseEvent.MOUSE_DOWN, deleteCircle);
 
var i:int = 0;
 
var methodName:String = "rightClick";
var method:Function = rightClick;
ExternalInterface.addCallback(methodName, method);
 
function rightClick():void {
 
	circle = new Circle();
	i++;
	circle.name = String(i);
	addChild(circle);
	circle.x = mouseX;
	circle.y = mouseY;
}
 
function deleteCircle(m:MouseEvent):void {
 
	if (m.target.name != null) {
		removeChild(getChildByName(m.target.name));
	}
}

So to conclude, at the moment you can’t use cleanly the right click in Flash like the left. You will always have the contextual menu with About and Settings’ buttons. But in fact who use them ? Aside on a website using Webcam or Microphone. So I think that developers should have the choice if they want a contextual menu (by default if we don’t need the right click) or not to have a more ergonomical application.

Starfield for games

On monday’s afternoon waiting a C++ lesson, I surf on the web. But there aren’t a lot of news, so in a fully bother I go on a flash game’s website and I play a game with Starship, asteroid… I’m sure you know that kind of game 😉 And I ask myself about it mechanism.

After some research I discover the term “Starfield” but no explanation nowhere about how it works, therefore I decide to programme one !
So in fact a Starfield is a perpetual movement of Stars in a restrected field. I know this a little short 😀
The idea is to have a movement in your field and only there, so if a Star go away it should be deleted or puted back. And in fact that the field itself which is moving on the stage.

There is all the code below, but look at the application before !
Click on the animation and use arrow’s keyboard for moving the Starship. In this example the Starship never moves, it’s the Starfield !!

Continue reading Starfield for games

Simple BevelFilter

Some months ago, I was at Turbulent Média Inc. in Montréal, Canada as a flash trainee. During this internship I discovered the BevelFilter class.
At this time, I’m creating my Portfolio (it will be online soon), and I reuse an effect discovered there for buttons.
Here is my button.

And now the simple code without any Tween !

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
package {
 
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.filters.BevelFilter;
   import flash.filters.BitmapFilter;
 
   public class Bevel extends Sprite {
 
       private var _effect:BevelFilter;
       private var _filter:BitmapFilter;
       private var _myFilters:Array;
       private var _angle:uint;
 
       public function Bevel():void {
 
	    btn.addEventListener(Event.ENTER_FRAME, _animation);
	}
 
	private function _animation(evt:Event):void {
 
	   _effect = new BevelFilter(5, _angle, 0xFFFFFF, 1, 0x000000, 10, 10, 1);
	   _filter = _effect;
	   _myFilters = [];
	   _myFilters.push(_filter);
	   btn.filters = _myFilters;
 
	    _angle +=2;
 
	    if (_angle > 360) {
		_angle = 0;
	    }
         }
    }
}

Mathematicals and AS3 = Biomorphs

A biomorph is a shape resembling that of a living organism (such as bacteria), though not necessarily of biotic origin. It is created with Mathematicals functions, such as fractal.
Based on the work of C. Pickover, I have made a computer program with AS3 that you can try below. I used the as3mathlib because of Complex Numbers.

Biomorphs – the computing time could be long, wait a moment.

Here is a part of the code. All comments are welcome 🙂

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
private function calculate():void {
 
  var r:Number = 0;
  var n:uint;
 
  for (var abs:Number = -3; abs < 3; abs += 0.01) {
 
    for (var ord:Number = -2.5; ord < 2.5; ord += 0.01) {
 
    n = 0;
    r = 0;
 
    z  = new Complex(abs, ord);
 
      while ((n < 11) && (r < 10)) {
 
      z = f(z, A, B, C, D, E, F, G);
      r = Complex.modulo(z);
 
      if ((n == 10) || (r > 10)) {
 
        putPixels(Complex.abs(z), abs, ord);
      }
      n++;
      }
    }
  }
  showBitmap();
}
 
private function f(z:Complex, A:Complex, B:Complex, C:Complex, D:Complex, E:Complex, F:Complex, G:Complex):Complex {
 
  var i:Complex = new Complex(0, 1);
 
  var inter1:Complex = Complex.adds(Complex.power(z, A), B);
  var inter2:Complex = Complex.adds(Complex.mult(C, i), Complex.mult(D, Complex.cos(z)));
  var inter3:Complex = Complex.adds(Complex.mult(E, Complex.sin(z)), Complex.mult(F, Complex.exp(z)));
  var inter4:Complex = Complex.mult(G, Complex.exp(z));
 
  return Complex.adds(Complex.adds(inter1, inter2), Complex.adds(inter3, inter4));
}
 
private function putPixels(h:Complex, abs:Number, ord:Number):void {
 
  abs = Math.round((abs + 3) * 100);
  ord = Math.round((ord + 2.5) * 100);
 
  var biomorphe:Number = Complex.norm(h) / 2;
 
  if (biomorphe > 1500000) {
    monBitmap.setPixel(abs, ord, 0xFFFFFF);
  } else if (biomorphe > 15000) {
  monBitmap.setPixel(abs, ord, 0x0099CC);
  } else if (biomorphe > 150) {
  monBitmap.setPixel(abs, ord, 0x993333);
  } else {
  monBitmap.setPixel(abs, ord, 0xFFFF00);
  }
}