The web site for Flash ActionScript 3.0 game developers

 
         
   

Ask Gary

Multiple Windows with Flash

How can I create a Projector that opens multiple windows, say I want to simulate a windows application with a main window that opens additional windows on top for configuration purposes.
Thanks, Sharon

If you want a real OS window, you'll need to look into using a Flash Wrapper application -- something that add functionality to Flash projectors. Search for "flash wrapper" and start looking it different ones.
On the other hand, you can use Movie clips and Loaders to simulate windows inside of a Flash movie. Just make a movie clip that looks like a window, and have that appear on top of everything else. When the user is done with it (OK or Cancel buttons?) then remove it from the stage. You could also add a close button if you like, and even the ability to move the window around around by dragging it from the top. These kinds of things can be quite simple, or very complex, depending on what you want.

Not Repeating Items in a List

Markus Wrote:

I am making a rather simple word game which reads in words from an XML-file (randomized). What I want to do is having a randomized order but sometime keeping track of which words has already been used in the game, so that I dont risk that a word will appear twice in the game. Do you have any solution for this?

This is a pretty common task. If your list is shuffled into a random order, though, then you don't have to worry about it. For instance, if you have 100 words, and then put them in a random order, and then take one word from the list at a time, then you should never end up with the same word twice as you are removing words from the list as you go. The key is to shuffle the array or words, and then remove them as you use them. For an example, see the section "Randomizing the Questions" in chapter 10 of the book.

hitTestObject Not As Expected

John Writes:

I have a long rectangle shaped movie clip and a ball on my stage. I want to detect when the ball hits the rectangle. The rectangle is angled at 45 degrees using transform. hitTestObject appears to true based on a rectangular are that encompasses the entire rotated movieclip (Imagine drawing a rectangle around the object so that the object goes from the upper left to lower right corners of the rectangle).
I want to test when the ball is touching the actual movie clip - not the area around it - how do I do that?

You are right. The hitTestObject function just looks at the bounding boxes, not the actual shape of the objects. If you think about it, this makes sense. Can you imagine the complexities of looking at the shape of two Flash display objects and determining if they overlap? Such a function would be very slow, which is why I imagine it doesn't exist. I talk a bit about how hitTestObject and hitTestPoint work in chapter 2, in the section on Collision Detection.

So, to your problem. If the ball is small enough, then you can simply use hitTestPoint with the point being the center of the ball. But if the ball is too large, then you'll need to dig into deeper math to determine when the ball actually touches the side of the rectangle. You can do a Google search on "collision detection circle line" to see some articles on the subject. It is pretty complex if you don't have a background in math.

Random Direction and Background

Hello there, I am a beginner at flash and have been trying to make a game.. Two things, the first is, that I want a Math.random that has the interval from -1 to 1, so that I can create a random direction for a ball. The other question is that I want to create a button that gives the background a random color. These are my problems, can you help me ?
Yours sincerly
Brian

If you are looking to create a random number from -1 to 1, which includes all stops in between, then you would need a range of 2, but lowered by 1 so it is -1 to 1 instead of 0 to 2. So, the statement would be var myRandomNumber:Number = Math.random()*2-1;
However, if your intention is to create a number that is -1 half the time and 1 half the time, then you can't simply round the number, as you would get -1, 0 and 1 at various times. So you would probably want: var myRandomDirection:int = (Math.random() < .5) ? -1 : 1;
The ? and : statement has three parts. The first checks to see if Math.random() is less than .5, which will happen 50% of the time. If so, then -1 is returned. Otherwise 1 is returned.
One way to create a random background color is to have a sprite that is a solid fill behind everything else. Then use the colorTransform to set its color. Like this:

var myColorTransform:ColorTransform = new ColorTransform();
myColorTransform.color = 0xFF0000;
myBackgroundMC.transform.colorTransform = myColorTransform;

To make it random, have the color value chosen at random from an array of possible colors.

Class or Object By Name in AS3

Hi Gary, just wondering if I can convert string to class/object in AS3?
I know Java can convert string name to class or object...
regards
Ed

Hello Ed. Yes, you can do this. The secret is the getDefinitionByNamefunction. Here is an example:

var classRef:Class = getDefinitionByName("MyClassName") as Class;
var myObject:Object = new classRef();

So myObject can be a Sound, a MovieClip, whatever. It is amazing how little attention this function gets when it is absolutely necessary for programming anything that uses a large library of named objects like sounds, graphics, etc.

Ask Gary: Variable Names

Hi Gary,
I teach AS and have for some time been telling my students to append
suffixes such as _mc to movieClips, _btn to buttons, etc. This seems to
really make life easier when troubleshooting. I have to wonder if I'm
really out of the loop on this one because I don't see anyone else doing
that.

In AS3 if you type your variables well, then you really don't need to use suffixes to help identify variables when troubleshooting. Furthermore, if you use descriptive names, then the problem goes away. For instance, if I was putting playing cards on the stage, I would place them all in a sprite and call the sprite "cardSprite".
While I'm not a huge fan of using fixed suffixes like "_mc" in variable names, I'm not against it either. Whatever makes it easier for you or your programming team to get the job done.

Ask Gary: AS3 Capitalization

Gary,

Is there a rule for knowing when a built in function, property, etc
will start with a capital, be all caps, or start lower case? For example,
addEventListener starts lower case, MouseEvent start upper case, and
ROLL_OVER is all caps. I am coming out of ASP programming and just
keeping my variables the right case and remembering all the new commands
is hard enough - but having to remember which commands are which case is a
bit much. Let me know if there is a good rule to remember these by.

Thanks.
Brandon

Good question. This is especially important when you consider that AS3 is case sensitive. So addEventListener is a function, but AddEventListener is nothing.
It seems the convention is that functions start with lowercase, then capitalize each word after that. Like gotoAndStop. Classes start with capital letters, like the Math in Math.random(). Constants use all caps and underlines between words, like ENTER_FRAME.
But I wouldn't take that for granted. And it doesn't really matter too much because the auto-completion in the AS3 programming windows will list the command for you as you type.
What I would avoid though is using capitalization to differentiate between functions or functions and variables. For instance, I wouldn't use moveObject as a function and then moveobject as a variable. It works, but I wouldn't do it.
I do kinda break that rule sometimes when I have a movie clip that by its nature will only be on the stage once. For instance, the space ship in an asteroids game. I might call that SpaceShip in the library and as a class, and then spaceShip will be the instance name. It makes it easier for me to find the movie clip in the Library based on the instance name I'm using in the code.

Ask Gary: Repositioning Word Search

hi Gary,

enjoying the book, and probably ahead of myself BUT, how can I change
where the wordsearch grid and wordlist appear on the stage if I change the
size of the stage?

I've made the stage larger, and I would like to put the grid and wordlist
in the center - how do I change the X/Y?

thanks...
skillspider

There are many ways to do this. One would be to change some constants in the code. If you look at the start of the script, you will see an offset for the letter grid.

static const offset:Point = new Point(15,15);

You can simply change this to 115,115 to move the grid to the right 100 pixels and down 100 pixels.
Similarly, you can move the word list over by changing this line:

newWord.x = 400;

What I would do first, is to make it dependent on the same offset constant. So change it to this:

newWord.x = offset.x + 385;

Now it will always be to the right of the letter grid. All you need to do is change the offset constant to move both elements.
Another way to do this would be to simply move the gameDprite. Every game element is in the gameSprite, which is at position 0,0 by default. So moving it to 100,100 will have the same effect as the other changes. Here is that code inserted into the startWordSearch function.

// set up the sprites
gameSprite = new Sprite();
gameSprite.x = 100;
gameSprite.y = 100;
addChild(gameSprite);

Ask Gary: How to Play Multiple Sounds

Hi Gary,
Thanks for the podcasts. I've just watched your video podcast on using sound in AS3 and it was very good, but I would like to be able to play more than one sound at a time. I've been able to do this in previous versions of ActionScript, but I can't get it to work in AS3. The new classes for producing sounds has changed quite a bit and I find it a little confusing.
Thank you for your time,
Chris Schnuck

Thanks for the question Chris. In the past, playing multiple sounds at the same time has been tough in Flash. Each movie clip, including the root level, could only handle one at a time. But with AS3, this doesn't seem to be an issue.

Using similar code from the Podcast, I was able to have a loop playing and also have sounds playing from a button at the same time. I just basically created two different sound objects to do it. The music loop uses one called myLoopSound and the button sounds uses one called myExternalSound.

myButton.addEventListener(MouseEvent.CLICK, playSound);

var myExternalSound:Sound = new Sound();
myExternalSound.load(new URLRequest("SoundExample.mp3"));

var myLoopSound:Sound = new Sound();
myLoopSound.load(new URLRequest("MusicLoop.mp3"));
myLoopSound.play();

function playSound(event:Event) {
myExternalSound.play();
}

You can download the example movie here.

Playable Game Demos Now Available

You can now play all the games from the book on this page. This is to give potential readers an idea of what they will be getting. All source code is in the book, and each chapter section describes how each game was built.

Syndicate content

Copyright Gary Rosenzweig