• Home
tudwaythecore.com
blog
  • Recent Posts

    • Detecting focus in flash player
    • BitmapData PerlinNoise
    • Creating bridges in APE part 2
    • Creating bridges in APE
    • SteerWheels 2
    • Creating a simple APE example
    • Design me some levels!
    • Finished Migrating
  • Recent Comments

    • Ax on creating a platform game in as3 part 5
    • Ax on creating a platform game in as3 part 5
    • tudway on creating a platform game in as3 part 5
    • Bruno on creating a platform game in as3 part 5
    • tudway on creating a platform game in as3 part 4
  • Categories

    • APE
    • flash
    • game
    • platform
    • sound
    • tutorial
    • Uncategorized
  • Archives

    • May 2009
    • October 2008
    • September 2008
    • August 2008
    • May 2008
    • April 2008
    • March 2008
  •  

    July 2009
    M T W T F S S
    « May    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • games

    • Game Showcase
May 29

Detecting focus in flash player

flash, game, tutorial No Comments »

Have you ever wondered how games can detect if they are in focus? Well I'm going to show you anyway and it is a very useful function that i use in all my new projects.

I'm going to show you what i mean first.

Get Adobe Flash player

There should be a grey rectangle above. If you click it, you gain focus in the flash file until you click away, eg outside the swf, in the browser address bar or even out of the browser. when focus is gained the swf will go white.

This is because when the swf is not in focus i am creating a transparent block over the entire screen. Imagine it as pausing a game when the focus is lost.

I have created a universal function with a pause variable in the parameter so that you can easily pause a game or app when the focus is lost.

PLAIN TEXT
Actionscript:
  1. var p:Boolean=false;
  2. detectFocus(p);
  3. function detectFocus(pausevar:Boolean) {
  4. var focusm:MovieClip = new MovieClip();
  5. focusm.graphics.beginFill(0x000000, 0.6);
  6. focusm.graphics.drawRect(0,0,550,400);
  7. focusm.graphics.endFill();
  8. addChild(focusm)
  9. stage.addEventListener(Event.DEACTIVATE, notinfocus);
  10. stage.addEventListener(Event.ACTIVATE, infocus);
  11. function notinfocus(event:Event):void {
  12. pausevar=true;
  13. focusm.visible = true
  14. }
  15. function infocus(event:Event):void {
  16. pausevar=false;
  17. focusm.visible = false
  18. }
  19. }

line 1: Creating the pause variable that the detectfocus function changes, this is so the function can pause your game.

line 2: Calling the detectfocus function using the pause variable p.

line 3: setting up the constructor for the function with 1 variable, pausevar.

lines 4 - 8: creating the rectangle that goes over the top of the screen, for more help on the drawing api try this post note the alpha of the fill colour is 0.6 so it is transparent.

line 9: Adding an event listener for when the swf is not in focus which calls the function notinfocus.

line 10: Adding an event listener for when the swf is in focus which calls the function infocus surprisingly enough!

lines 11 - 14: Creating the notinfocus function which sets the pause variable to true and also makes the rectangle visible.

lines 15 - 18: Creating the in focus function that does exactly the opposite to the not in focus function, so it sets pause to false and makes the rectangle invisible.

Now lets see it in action for one of the new levels for steerwheels!

Get Adobe Flash player

Click inside to gain focus and then arrow keys to move. (you can't win yet)

Works well doesn't it!

May 26

BitmapData PerlinNoise

flash, game, tutorial No Comments »

I haven't posted in a while because i have been busy with other projects, which you will see soon.

I've decided to play with bitmapdata and see what it can be used for. I have used this effect in a game thats in production for the backgrounds because they look quite spacey. And its a quick way to get some awesome looking art without paying an artist.

First an example

Get Adobe Flash player

You should see a blue/green image that looks quite good, something you might find in a game.

PLAIN TEXT
Actionscript:
  1. import flash.display.Bitmap;
  2. import flash.display.BitmapData;
  3.  
  4. var seed:Number=Math.floor(Math.random()*100);
  5. var channels:uint=BitmapDataChannel.BLUE|BitmapDataChannel.GREEN;
  6. var myBitmapDataObject:BitmapData=new BitmapData(550,400,false, 0x00000000);
  7. myBitmapDataObject.perlinNoise(100, 80, 6, seed, false, true, channels, false, null);
  8. var myBitmap:Bitmap=new Bitmap(myBitmapDataObject);
  9. addChild(myBitmap);

Line 1 and 2: Importing the necessary classes needed in this example.

line 4: This is a random number between 1 and 100 that we use when creating the bitmap with perlin noise. This number can be any number but varying it changes the pattern produced.

line 5: Bitmapdata has 4 channels, red blue green and alpha, you can mix channels by using "|". This is what the perlin noise is made up of.

line 6: Creating a black bitmapdata that fills the whole stage. The parameters are : BitmapData(width:int, height:int, transparent:Boolean = true, fillColor:uint = 0xFFFFFFFF)

line 7: Creating a perlin noise over the top of the bitmapdata that we just created. This is what makes those cool effects, play around with the values and see what you get. the parameters are: perlinNoise(baseX:Number, baseY:Number, numOctaves:uint, randomSeed:int, stitch:Boolean, fractalNoise:Boolean, channelOptions:uint = 7, grayScale:Boolean = false, offsets:Array = null).

line 8: Creating the bitmap that will hold the bitmapdata that i created before so that i can display it on stage. You cant add bitmapdata directly to the stage.

line 9: Adding the bitmap to the stage so we can see it.

And thats all for now, i will be experimenting with this later.

Oct 26

Creating bridges in APE part 2

APE, flash, tutorial No Comments »

This is a continuation of the last tutorial concerning Bridges in APE and is the next step that i would take to make this function more useable. Thee is no difference in how i make the bridges, all i have done is to add the options to change the stiffness of the bridge and the colour of it.
This can be changed simply with a few lines of code.
I have also learnt that default values can be set to parameters in functions, this means that flash won't throw an error if not all of the parameters are set.

Here is an example of the new ape bridge function with random colours and lineĀ thicknesses. The top bridge has a stiffness of 0.4 and the 3rd has a stiffness of 1.2, the rest have a value of 1. You should be able to tell the difference.


Reload the page to see different bridges.

The colours and line thicknesses are random so don't expect a work of art, but you can see how easy it is to change what style the bridge is. You may notice that i have changed the falling balls to black.

The code:

PLAIN TEXT
Actionscript:
  1. stage.frameRate = 30;
  2. import org.cove.ape.*;
  3. import flash.events.Event;
  4. addEventListener(Event.ENTER_FRAME, run);
  5. var apeholder:MovieClip = new MovieClip();
  6. apeholder.graphics.drawRect(0, 0, 550, 400);
  7. addChild(apeholder);
  8. var ballArray:Array = new Array();
  9. APEngine.init(1/3);
  10. APEngine.container = apeholder;
  11. APEngine.addForce(new VectorForce(false,0,2));
  12. var examplelevel:Group = new Group();
  13. examplelevel.collideInternal = true;
  14. var leftwall:RectangleParticle = new RectangleParticle(5,200,10,600,0,true);
  15. leftwall.setStyle(3, 0x000000, 1, 0xFFFFFF,1);
  16. examplelevel.addParticle(leftwall);
  17. var rightwall:RectangleParticle = new RectangleParticle(545,200,10,600,0,true);
  18. rightwall.setStyle(3, 0x000000, 1, 0xFFFFFF,1);
  19. examplelevel.addParticle(rightwall);
  20. for (var ballnum:int = 0; ballnum<20; ballnum++) {
  21.     var ball:CircleParticle = new CircleParticle(Math.random()*550,-50-Math.random()*150,15,false,2);
  22.     ball.setStyle(1, 0x000000, 1, 0x000000,1);
  23.     examplelevel.addParticle(ball);
  24.     ballArray.push(ball);
  25. }
  26. createBridge(530, -50, 6, 60, 0.4, false, Math.random()*0xFFFFFF, Math.random()*10, Math.random()*0xFFFFFF);
  27. createBridge(20, 50, 6, 60, 1, true, Math.random()*0xFFFFFF, Math.random()*10, Math.random()*0xFFFFFF);
  28. createBridge(530, 150, 6, 60,1.2, false, Math.random()*0xFFFFFF, Math.random()*10, Math.random()*0xFFFFFF);
  29. createBridge(20, 250, 6, 60, 1, true, Math.random()*0xFFFFFF, Math.random()*10,  Math.random()*0xFFFFFF);
  30. APEngine.addGroup(examplelevel);
  31. function run(event:Event):void {
  32.     APEngine.step();
  33.     APEngine.paint();
  34.     for (var ballstocheck:int = 0; ballstocheck<ballArray.length; ballstocheck++) {
  35.         if (ballArray[ballstocheck].py>400) {
  36.             ballArray[ballstocheck].py = -50;
  37.         }
  38.     }
  39. }
  40. function createBridge(startx:Number, starty:Number, jointnum:int, ropedist:Number, elasticity:Number = 1, right:Boolean = true, linecolour:uint = 0x000000,linethickness:Number = 1, fillcolour:uint = 0x000000) {
  41.     var bridgeArray:Array = new Array();
  42.     var reverse:int = 1;
  43.     if (right == true) {
  44.         reverse = 1;
  45.     } else {
  46.         reverse = -1;
  47.     }
  48.     for (var bn:int = 0; bn<jointnum; bn++) {
  49.         var bparticle:CircleParticle = new CircleParticle(startx+(bn*ropedist*reverse),starty+(bn*ropedist/2),5,false,2);
  50.         bparticle.setStyle(linethickness, linecolour, 1, fillcolour,1);
  51.         examplelevel.addParticle(bparticle);
  52.         bridgeArray.push(bparticle);
  53.     }
  54.     for (var cn:int = 0; cn<bridgeArray.length-1; cn++) {
  55.         var bridgeConn:SpringConstraint = new SpringConstraint(bridgeArray[cn], bridgeArray[cn+1], elasticity, true, 10, 1);
  56.         bridgeConn.setStyle(linethickness, linecolour, 1, fillcolour);
  57.         examplelevel.addConstraint(bridgeConn);
  58.     }
  59.     bridgeArray[0].fixed = true;
  60.     bridgeArray[bridgeArray.length-1].fixed = true;
  61. }

The explanation of what has changed:

Lines 26-29: These, as you know if you have read this post are how we call the bridge functions. i will explain in more detail after i have explained the changes to the actual function.

Line 40: This is where i declare the the function that makes the bridges. You will notice 4 new parameters. Elasticity is how stiff the bridges are from 0-1, where 0 is floppy ad 1 is solid, but i have found that you can put the elasticity to 1.2 before it breaks the bridge, the default is 1. Linecolour is obviously the colour of the outlines. It is a hex code 0x000000 is black and the default. Line thickness is the thickness of the line in pixels, default is 1. Fillcolour is the fillcolour of the blocks and the default is black, 0x000000.

Line 50: This sets the style of the circle particle that acts as our joint. So instead of setting them to constants i have set them to the parameters that i set on line 40.

Line 55: This is the line where i set up the springconstraints for the bridge the only thing that i have changed in the stiffness, i have changed it from 1 to the elasticity parameter i set earlier.

Line 56: Where the style of the pringconstraints are set, just as before i have changed the colours and line thicknesses to those of the parameters.

Ok that is all the code we changed to get these results, now lets look at how i called the bridges on lines 26-29.
I will just explain line 26 as the others are the same.

Line 26: createBridge(530, -50, 6, 60, 0.4, false, Math.random()*0xFFFFFF, Math.random()*10, Math.random()*0xFFFFFF);

The bits in bold are the new parameters. The first one, the 0.4 is the stiffness of the bridge, so not very stiff in this case. The next one is the line colour and i have set a random colour for this. The next is the line thickness and i have set a random number between 1 and 10. The last is the fill colour which is again random.

I will probably continue to play with APE in the next tutorial but you can send requests if you want something more specific.

Previous Entries
Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml