SpotLights to RTS games and pseudo inverse masks
Well… Today a friend of mine asked me to help him with a night render for his RTS game. He needed an inverse mask to his MovieClip (witch was his entire game). I thought a little and I decided to help him. I started writing an inverse mask, but I realized that this would cost a lot of the cpu. So I thought that to him what really matter is to have a fog in a layer above his game, so I wrote (in some minutes) 2 classes that manage theses “Spots Lights”.
The result as you can see is here (you can drag the red curcles):
As I’m writing codes that are not related to my engine (that I’m writing to my final project), I decided to create a Google Code Repository to my extra experiments. So the classes will be always there. This firsts experiments can be improved a lot, so if you have some comments or know a better way of do it, please let me know
The class of this example is here:
package { import com.filipesilvestrim.game.render.light.SpotLight; import com.filipesilvestrim.game.render.light.SpotRenderer; import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; [SWF(width=350, height=468)] public class SpotExperiment extends Sprite { private var spotRender : SpotRenderer; private var spotSelected : SpotLight; [Embed(source="aoe.jpg")] private var map : Class; public function SpotExperiment() { stage.scaleMode = StageScaleMode.NO_SCALE; addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); addEventListener(MouseEvent.MOUSE_UP, onMouseUp); addEventListener(Event.MOUSE_LEAVE, onMouseUp); addEventListener(Event.ENTER_FRAME, onEnterFrame); //Adding the background (in some cases the game as itself) addChild(new map()); //creating the render. The first param is where we'll add the representation, the second is the width and the third is the height spotRender = new SpotRenderer(this, 350, 468); //here we can change the fog color to other one and the transparency spotRender.changeColorMap(0x000000, 1); //Now we're adding 3 spots of diferent sizes and position //As params we have: name, pos X, pos Y, width, height var spot1 : SpotLight = new SpotLight("spot1", 200,200,200,200); //we can adjust the intensity from 0 to 1 spot1.intensity = 1; //the focus can be adjusted too spot1.focus = .5; //adds the red ball for debug spot1.debug(this); //add the spot to be rendered spotRender.addSpot(spot1); var spot2 : SpotLight = new SpotLight("spot2", 250,300,100,100); spot2.debug(this); spotRender.addSpot(spot2); var spot3 : SpotLight = new SpotLight("spot3", 100, 100, 300, 300); spot3.debug(this); spotRender.addSpot(spot3); //render as first time to the scenario don't appear without no one fog spotRender.render(); } private function onMouseDown ( event : MouseEvent ) : void { //if some spot is target if (spotRender.getSpot(event.target.name) != null) { //hold in the spotSelected variable the sport that represents that Sprite ball spotSelected = spotRender.getSpot(event.target.name); } } private function onMouseUp ( event : Event ) : void { //if mouse released spotSelected = null; } private function onEnterFrame ( event : Event ) : void { //hold the spots changes (if some spot selected) if(spotSelected != null) { //reposition the spot and the debug reference accourding the mouse position spotSelected.x = spotSelected.spriteDebug.x = mouseX; spotSelected.y = spotSelected.spriteDebug.y = mouseY; //when change the position the sportRender must be rendered again spotRender.render(); } } } }
Clone a object with type cast
Wheel, I’m not writing another way of do it…
Is the same way that ObjectUtils of Flex Framework do, but… Imagine that we want to duplicate one object of the class Example, we can just do the clone with a code like this:
public function deepClone ( obj : * ) : * { var bytes : ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject(); }
Let’s first imagine our Example class like this:
package com.filipesilvestrim { public class Example { public var name : String; public function Example () {} } }
Ok, but now if we try get the object with the cast like trace((deepClone(obj) as Example)). Man… shame on you, you’ll get “null”. but why? It occurs because when we are cloning the class we do it with the ByteArray and when you return that’s “new” bytes as an Object, the Flash Player don’t know that those bytes are affiliate with the class Example. So how to solve it?
Let’s register in the Flash Player the class Example, to that when it saw the bytes being cast, it understand that those bytes mean that class. To register we need to this:
registerClassAlias("com.filipesilvestrim.Example", Example);
And the code working will be like this:
package com.filipesilvestrim { import flash.net.registerClassAlias; import com.filipesilvestrim.Example; public class MainClone { public function MainClone () { var obj : Example = new Example(); registerClassAlias("com.filipesilvestrim.Example", Example); trace("Cloned Object with cast = " + (deepClone(obj) as Example)); // must trace "Cloned Object with cast = [object Example]" } private function deepClone ( obj : * ) : * { var bytes : ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject(); } } }
So, lets code and have fun
What a shame… Back to blog!
Wow today I saw my blog and I felt a pain on my heart. Can you guys imagine, it’s almost one year that I didn’t blog.
Last year was so busy, building a looot of games, conciliating work and college and my User Group. I just feel said about have being had no more time to blog. So, let’s change this scene.
To those that don’t know what I do. I’m an Game Developer, mainly developing in AS3, at W3Haus. I have an Adobe User Group in Rio Grande do Sul, Brazil; the user group is called AUGRS. I’m finishing my college (Digital Games at UNISINOS) this year and my thesis project will target game engines and AS3 (soon I’ll have more news about it).
And here we go starts new year with an picture of me in an meeting at LFPUG (London Flash Platform User Group) last year when I traveled to London to attend Flash On The Beach 2008. I’m the 4º from right to left. And by request of my friend (lol), I’ll introduce Pedro Taranto, another Brazilian that traveled with me and he was there to - the 1º from left to right.

Flash Player loses focus with button + removeChild
Well, this week, I was doing a little game that to the player start to play needs click in one “start game” button and after uses the keyboard to control the character moves. So until this point all OK, but what i didn’t told you is that when click’s in the “start game”, it disappears with one removeChild() (because it appears above all the stage).
After this, my character was no more moving… I fought “wow could be my InputManager class not working so fine…”, but I tried find some bug or something wrong and nothing. After some tries, accidentally I removed the buttonMode = true - propertie that i was defining to that my “start game” Movie Clip behaviors like Button - and tcharam… All working…..
At first we need remind that Keyboard events will just works when Flash Player get focus. And accidentally I found a way of Flash Player loses focus with on removeChild().
So what is happening here?!
First, open the swf file (focus it); after, click at the button (button with focus); later on, the button is removed from the stage (the focus is removed together with the button - so here are the problem). Now if I click on the stage, or other local in the swf file (Flash Player will get focus again) and it will works…
Here is a source code as example :
var spBtn = new Sprite(); spBtn.buttonMode = true; spBtn.graphics.beginFill( 0xff0000, .4 ); spBtn.graphics.drawRect( 0, 0, this.stage.stageWidth, this.stage.stageHeight ); spBtn.graphics.endFill(); addChild( spBtn ); addEventListener ( KeyboardEvent.KEY_DOWN, managekeyDown ); spBtn.addEventListener( MouseEvent.MOUSE_DOWN, mouseClick ); function mouseClick ( e : Event ) : void { spBtn.removeEventListener( MouseEvent.MOUSE_DOWN, mouseClick ); removeChild( spBtn ); } function managekeyDown ( e : KeyboardEvent ) : void { if ( e.keyCode == Keyboard.LEFT ) { trace ( "JUST TRACE IF FLASH PLAYER HAVE FOCUS ON IT" ); } }
======= Updated at August 1º (expect update more the blog…) ======
To fix this issue you must reset the focus to the stage. So the code will change just in this part:
function mouseClick ( e : Event ) : void { spBtn.removeEventListener( MouseEvent.MOUSE_DOWN, mouseClick ); removeChild( spBtn ); //After the remove child we’ll reset the focus to stage this.stage.focus = this; }
From now on just English…
Well guys, unhappily - to whom just want to know in Portuguese - and happily to the rest of the World, I’ll be writing just in English on my blog. I’m taking this action looking forward an better transparency to change informations on line, once the majority of the ActionScript Gurus communicate each others in English and too because I’ve a lot of abroad ActionScript friends.
So, to those that want continues seeing ActionScript related subjects in Portuguese, I strongly advice join to the AUGRS ( Adobe User Group Rio Grande do Sul) discussion list.
To the others, we’ll be in touch.
== Versão em Português / Portuguese Version ==
Bom pessoal, infelizmente - para aqueles que só saber Português - e felizmente para o resto do mundo, estarei blogando de agora em diante somente em Inglês. Estou tomando esta medida visando uma melhor transparência para troca de informações online, uma vez que a maioria dos gurus da área se comunicam em inglês e também levando em conta de que tenho muitos amigos da área no exterior.
Então, para quem quiser continuar vendo assuntos relacionados a ActionScript em Português, aconselho estes a participarem da lista de discussão do AUGRS ( Adobe User Group Rio Grande do Sul).
Aos demais, estaremos em contato, porém agora Inglês.
No comments








