Friday, June 13, 2008

Adding mc speed using Numeric Stepper component

In previous example, our hero has constant speed set to 2. Speed is defined inside doTween function, as 6th parameter for xT and yT tweens. Off course, this speed doesn’t have to be constant at all. We will use Numeric Stepper component just to show how to change speed parameter. Instance of Numeric Stepper is "step". We change mentioned parameter from "2" to "step.value".

download hero_v2 source file.

 


When making a game, we will not use Numeric Stepper, but we can change hero speed in this way in relation to health or game level or whatever.

Posted by flanture at 14:51:07 | Permanent Link | Comments (0) |

Wednesday, June 11, 2008

Character elastic movement


Flash game characters doesn’t have to move only in standard way, press left arrow key to move left, right arrow key to move right, space to jump etc. In this new flash game, main character (hero) will move differently. Hero will be dragable and on click, he will perform elastic move. Start position and destination position will be symmetrical in relation to Stage center. Enemy can be destroyed only if mouse is not pressed, otherwise, hero health will decrease.

Now, code. First we import related classes:

import mx.transitions.Tween;
import mx.transitions.easing.*;

We need to define simple onPress, onReleaseOutside and onRelease methods for movie clip of our hero, mcHero. Method onRelease has two functions, findSimPoint() and doTween(). It is obvious that findSimPoint() calculates symmetrical point of hero x and y in relation to Stage center. DoTween(), well, performs the tween! Code comes with comments.


Download hero.fla




Posted by flanture at 15:48:03 | Permanent Link | Comments (0) |

Tuesday, November 27, 2007

How to shuffle deck of cards


When making some Flash game there is sometimes need for Arrays manipulation. For example if you make game with cards, you want to find a way how to shuffle set of 52 cards. Let's say your array is:

a = Array("1h", "2h", ..., "14h", ..., "14d");

now, you can use next function to shuffle your cards array:

1.function shuffle( b:Array ) : Array{
2.     var temp:Array = new Array();
3.     var templen:Number;
4.     var take:Number;
5.     while (b.length > 0) {
6.         take = Math.floor(Math.random()*b.length);
7.         templen = temp.push(b[take]);
8.         b.splice(take,1);
9.     }
10.   return temp;
11.}


What this code means? I hope it is clear I only have to explain lines in while loop, because everything else is easy to understand. So, while loop will work until there are elements in start array, and do remember that we take one element from that array in every pass. With line 6 we choose randomly one element of start array. Then in 7, we push that element in first empty place in result array and than in line 8, we exclude same element from start array. In every while loop pass, start array is shorter for 1 element and result array is longer for 1 element. Since we choose element randomly in the end function returns shuffled array. Simply as that.

You can test this function with this code:

trace(a);
trace(a.length);

function onMouseUp() {
    var c:Array = new Array();
    c = shuffle(a);
    trace(c);
}


Download this function and few more here.
Enjoy!

Posted by flanture at 03:21:34 | Permanent Link | Comments (0) |