Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

17 January 2010

getURL in ActionScript 3 (navigateToURL)

One of the things that has become something of a pain in AS3 is getURL. If you’ve used ActionScript 2 before, you know how simple it is to open a URL in a web browser.

getURL("http://www.google.com");

In AS3, however, it gets a bit more complicated.
You have to declare your URLRequest. This is pretty much a fancy way of holding the URL. You may wonder, why you need a URLRequest to hold the URL when you could just use the string itself? Well the answer is because you can use the same URLRequest with many different functions. And there are special properties to a URLRequest.
This is how you declare a URLRequest:

var url:URLRequest = new URLRequest("http://kornesh92.blogspot.com");
The URLRequest by itself is useless, but coupled with navigateToURL, it’ll act like getURL.

This is the full code:
var url:URLRequest = new URLRequest("http://kornesh92.blogspot.com");
navigateToURL(url);
Put that in the first frame of your movie and when you compile it, this blog should open up in your browser.

16 January 2010

AS3 FPS Display Class

You can display your current FPS(Frame Per Second) during games.
Save as FPSTimer.as.

package{
import flash.events.Event
import flash.events.TimerEvent
import flash.text.TextField
import flash.utils.Timer

public class FPSTimer extends TextField{
private var fps:int = new int();
private var t:Timer = new Timer(1000);

public function FPSTimer():void{
t.start();
addEventListener(Event.ENTER_FRAME, onEnter);
t.addEventListener(TimerEvent.TIMER, onTimer);
}
private function onEnter(e:Event):void{
fps++;
}
private function onTimer(e:TimerEvent):void{
text = String(fps);
fps = 0;
}
}
}
Using the FPSTimer.as is easy. Just add it to your Sprite,MovieClip or stage like this:

addChild(new FPSTimer()) 
and it’ll display the FPS. Simple as that.

How to Monetizing a Flash Game

1) Sign up to MochiAds. This company allows you to ake money from every game play and offers some more interesting services such as leaderboards, link tracking, and so on..



2) Sign up to MochiBot, a free service to determine Flash games popularity


3) Sign up to FlashGameLicense, the place to buy and sell Flash games. Read a review about this service here
4) Think about a game design idea. The more original, the better. If you have no ideas, copy successful games.
5) Make your game…The most part of the whole process
6) Once you think it’s finished encrypt it. You can use the MochiAds free encrypter.
7) Add MochiAds ads and MochiBot stats
8) Submit your game to a developers forum or take a look at First Impression service by FlashGameLicense, to get some qualified reviews about your game. Remember to sitelock your game to prevent it to spread without your control. If you don’t know how to sitelock a game, read how to sitelock a flash movie
9) Now that your game should be really finished, submit it to FlashGameLicense
10) Wait at least two weeks
11) At this time if you found a sponsor, distribute the game according to the partnership you signed. If you did not find any sponsor, return to step 10 or proceed to 12
12) Sumit your game to as much portals as you can. For a faster distribution, try FlashGameDistribution. Read a review about this service here
13) Improve and fix game bugs, interface, etc...

05 January 2010

Sitelocking an Actionscript 3 Flash Movie (SWF)

Sometimes you need to make your Flash movie (or game) to work only on selected domains. Sometimes you may want to blacklist some domains so they can’t display your Flash content.
This is called sitelocking, and means you lock a movie to a specific site.

Site locking is important if you want to keep your game locked to your site, or you’re posting it on a site but don’t want it stolen from that site. Remember to remove the site locking code when you feel like distributing the game around.

It’s also best to encrypt your swf, since if people can decompile it, using any number of swf decompilers around, then they can easily remove your site locking.

Let’s see how can you sitelock a Flash file.

 
var lockURL:String = "kornesh92.blogspot.com";
var lock:Boolean = true;

var urlString:String = stage.loaderInfo.url;
var urlParts:Array = urlString.split("://");
var domain:Array = urlParts[1].split("/");

if(lockURL!= domain[0]){
 trace("Access Denied. You dont have permission to host this swf on your site.");
}

site lock to a set of possible urls instead of only one? Well, you’d create an array of url strings and then check against that. Like this:

 
var lockURL:Array=["localhost","www.site2.com","www.site3.com"];
var lock:Boolean=true;
var urlString:String=stage.loaderInfo.url;
var urlParts:Array=urlString.split("://");
var domain:Array=urlParts[1].split("/");

for (var i:uint = 0; i < lockURL.length; i++) {
 if (lockURL[i]==domain[0]) {
  lock=false;
 }
}
if (lock) {
 trace("Access Denied. You dont have permission to host this swf on your site.");
}

addEventListener looping methods in AS3

Instead of using

btn0.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn1.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn2.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn3.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn4.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn5.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn6.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn7.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn8.addEventListener(MouseEvent.MOUSE_OVER, doSomething);
btn9.addEventListener(MouseEvent.MOUSE_OVER, doSomething);

function doSomething(e:MouseEvent):void {
  txt.text = e.target.name;
}

You could use this
Method 1


for (var i:uint = 0; i < 9; i++) {
this["btn"+i].addEventListener(MouseEvent.MOUSE_OVER, doSomething);
}

function doSomething(e:MouseEvent):void {
txt.text = e.target.name;
}

Download source code:


Method 2


for (var i:uint = 0; i < 10; i++) {
 this["btn"+i].addEventListener(MouseEvent.MOUSE_OVER, doSomething);
}

function doSomething(event : MouseEvent):void {
 switch ( event.target) {
  case btn1 :
   // dosomething
   txt.text="Button 1";
   break;
  case btn3 :
   // dosomethingelse
   txt.text="Button 3";
   break;
  case btn6 :
   // dosomethingelse
   txt.text="Button 6";
   break;
  case btn8 :
   // doSomethingDifferent
   txt.text="Button 8";
   break;
  default :
   // do nothing or something else
   txt.text="Other buttons";
   break;
 }
}

Download source code:

04 January 2010

Sites To Submit Your Flash Games

So where to submit your Flash Game when it's finished?!

Flash game distribution can be a challenge, and I encourage you to spread the fun and give even more people the opportunity to enjoy your games all over the internet. So I’m posting a long list of sites where you can submit your games.

You can find a lot of game portals lists around the web, but this one includes only portals able to give at least 100,000 views, according to MochiBot stats.

Having your game on half of the portals listed here should grant you some millions visits. Don’t even bother submitting your game to portals able to give you only a few thousands visits.

Just follow this list and let the game virally spread through minor portals.

More than 1,000,000 views
www.addictinggames.com
www.agame.com
www.mindjolt.com
www.oyunlar1.com

More than 100,000 views
www.7k7k.com
www.andkon.com
www.ejocurigratis.ro
www.fastgames.com
www.flasharcadegamessite.com
www.flashgames.it
www.freeonlinegames.com
www.freeworldgroup.com
www.funnygames.nl
www.gamegarage.co.uk
www.games2girls.com
www.gamesfreak.net
www2.jeux.com
www.jogosjogos.com
www.juegos10.com
www.juegosdiarios.com
www.killsometime.com
www.kongregate.com
www.micoia.net
www.minijuegos.com
www.net-games.biz
www.net-games.co.il
www.newgrounds.com
www.onemorelevel.com
www.oyunskor.com
www.physicsgames.net
www.playedonline.com
www.puffgames.com
www.spele.nl
www.thegamehomepage.com
www.wyspagier.pl
www.xiaoyouxi.cn
www.yougame.com

19 July 2009

AS3 addChild looping methods

Monster For this article, simply import a image and convert it into movie clip. In the Library, give it the class name Monster.



The Base class field will automatically be populated for you, but if it hasn't, make sure to enter flash.display. MovieClip as shown in the above image.

You would have to repeat a statement multiple times if you want to have an action executed more than once. For example, lets say, you want to create five MovieClips and add them to the display list. The long way for doing this would have been to type the command five times:


var enemy1 = new Monster(); 
enemy1.x = 0 
addChild(enemy1); 
var enemy2 = new Monster(); 
enemy2.x = 50 
addChild(enemy2); 
var enemy3 = new Monster(); 
enemy3.x = 100 
addChild(enemy3); 
var enemy4 = new Monster(); 
enemy4.x = 150 
addChild(enemy4); 
var enemy5 = new Monster(); 
enemy5.x = 200
addChild(enemy5);

However, you can do the same exact thing using a loop without having to type the same code multiple times, which is smarter way of doing it.

You can use either of the following methods.

Method 1

var enemy:Array = new Array();
for(var i:Number = 0; i < 5; i++) 
{
enemy[i] = new Monster();
addChild(enemy[i]); 
enemy[i].x = i*50;
}


Method 2

for(var i:Number = 0; i < 5; i++) 
{
var enemy:Monster = new Monster();
addChild(enemy); 
enemy.x = i*50;
}

Output







This second code generates the same exact result, but in a much more compact way. Using a loop also makes it less likely for you to have typing errors because you type the code once and then it is automatically repeated(looped).



You can always find out more at AS3 language reference Livedocs