The web site for Flash ActionScript 3.0 game developers

 
         
   

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.

FlashGameU.com Video Tutorial: Countdown Clock

Gary Rosenzweig from FlashGameU.com shows you how to build a simple clock to count down time until the end of a game.

View video in your browser.

Download QuickTime Movie.

Download the source file.

Subscribe via iTunes.

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.

FlashGameU.com Video Tutorial: Mouse Fade and Base Classes

Gary Rosenzweig from FlashGameU.com shows you how to make a movie clip fade away according to the distance to the cursor location. He uses a Base Class to assign this same script to two different movie clips.

View video in your browser.

Download QuickTime Movie.

Download the source file.

Subscribe via iTunes.

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.

FlashGameU.com Video Tutorial: Creating a Snake Game, Part 2

In this mini-chapter two-part episode, Gary Rosenzweig completes a snake game in ActionScript 3.

View video in your browser.

Download QuickTime Movie.

Download the source file.

Subscribe via iTunes.

FlashGameU.com Video Tutorial: Creating a Snake Game, Part 1

In this mini-chapter two-part episode, Gary Rosenzweig starts building a snake game in ActionScript 3.

View video in your browser.

Download QuickTime Movie.

Download the source file.

Subscribe via iTunes.

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.

FlashGameU.com Video Tutorial: Playing Multiple Flash Movies Inside a Single Flash Application

Gary Rosenzweig takes a look at how to put two Flash games together, inside of a single Flash movie. The trick is to use the Loader object.

View video in your browser.

Download QuickTime Movie.

Download the source file.

Subscribe via iTunes.

Syndicate content

Copyright Gary Rosenzweig