Creating games in actionscript 3




















There are three types of numbers: uint , int , and Number. The uint type is for whole numbers 0 or higher. The int type is for whole numbers that can be positive or negative integers, in other words. The Number type can be positive or negative numbers, whole or floating point, such as 3. In for loops, we usually use either uint or int types because we only move in whole steps.

So, this is basically a quick way to loop and get the chance to create 36 different Card movie clips. Creating the movie clips is just a matter of using new , plus addChild. We also want to make sure that as each new movie clip is created, it is stopped on its first frame and is positioned on the screen correctly:.

Adding a symbol in ActionScript 3. In between these two commands, you want to do things such as set the x and y position of the new symbol. The positioning is based on the width and height of the cards we created. In the example movie MatchingGame1.

So, by multiplying the x and y values by 52, we space the cards with a little extra space between each one. We also add horizontally and 45 vertically, which happens to place the card about in the center of a x standard Flash movie. Before we can test this code, we need to link the Flash movie to the ActionScript file. The ActionScript file should be saved as MatchingGame1. However, that is not all you need to do to link the two.

You also need to set the Flash movie's Document class property in the Property Inspector. You can test a movie when either the Flash movie itself is the current document or an ActionScript file is the current document. When an ActionScript file is the current document, look in the upper-right part of the document window for a Target indicator. This tells you what Flash movie is compiled and run when you test.

If the wrong file is shown as the Target, you can use the drop-down menu to change it. The easiest way to test is to go to the menu and choose Control, Test Movie.

Before we go any further with developing this game, let's look at how we can make what we have better. We copy the existing movie to MatchingGame2. Remember to change the document class of MatchingGame2. Suppose you don't want a 6x6 grid of cards; maybe you want a simpler 4x4 grid or even a rectangular 6x5 grid.

To do that, you need to find the for loops in the previous code and change the loops so they loop with different amounts. A better way to do it is to remove the specific numbers from the code all together. Instead, have them at the top of your code and clearly labeled, so you can easily find and change them later. Putting specific numbers in your code, such as the 6s for the row and column lengths, is called hard coding. It is considered to be bad practice for programmers because it makes it harder to adjust your program later, especially for others who want to inherit your code and find where they can adjust it.

Instead of placing these values in the code, let's put them in some constant variables up in our class to make them easy to find and modify:. Notice that I chose private static const when defining each constant. The private means these variables can only be accessed inside this class. The static means they have the same values in all instances of the class.

And, the const means that the values can never change. If you were to use public var instead, it would give you the opposite declaration: can be accessed outside of the class and holds different values for each instance. Because this is the one and only instance of the class and there are no outside scripts, it really makes no difference, except for neatness. Now that we have constants, we can replace the code in the constructor function to use them rather than the hard-coded numbers:.

I also changed the name of the class and function to MatchingGame2. You can find these in the sample files MatchingGame2. As we move through this chapter, we change the filenames of both the ActionScript file and the movie.

If you are following along by creating your own movies from scratch, remember to change the document class in the Property Inspector so each movie points to the right ActionScript file. For instance, the MatchingGame2. In fact, open those two files.

Test them one time. Then, test them again after you change some of the constants. Make the boardHeight only five cards, for instance. Scoot the cards down by 20 pixels by changing boardOffsetY. The fact that you can make these changes quickly and painlessly drives home the point of using constants.

Now that we can add cards to the screen, we want to assign the pictures randomly to each card. If there are 36 cards in the screen, there should be 18 pairs of pictures in random positions.

Chapter 2, "ActionScript Game Elements," discussed how to use random numbers. However, we can't just pick a random picture for each card.

We need to make sure there are exactly two of each type of card on the screen. No more, no less; otherwise, there are no matching pairs. This process is kind of the opposite from shuffling a deck of cards. Instead of mixing the cards and then picking new cards from the top of the deck, we are using an ordered list of cards and picking new cards from random spots in the deck.

To do this, we need to create an array that lists each card, and then pick a random card from this array. Arrays are variables that hold a series of values.

The array is 36 items in length, containing 2 of each of the 18 cards. Then, as we create the 6x6 board, we are removing cards from the array and placing them on the board. When we have finished, the array is empty, and all 18 pairs of cards are accounted for on the game board.

Here is the code to do this. A variable i is declared in the for statement. It goes from zero to the number of cards needed. This is the board width times the board height, divided by two because there are two of each card. So, for a 6x6 board, there will be 36 cards. We must loop 18 times to add 18 pairs of cards. This new code goes at the start of the constructor function:.

The push command is used to place a number in the array twice. Here is what the array looks like:. Now as we loop to create the 36 movie clips, we pull a random number from this list to determine which picture displays on each card:.

The new lines are in the middle of the code. First, we use this line to get a random number between zero and the number of items remaining in the list:. The Math. Multiply this by cardlist. Then, use Math. Then, the number at the location in cardlist is assigned to a property of u named cardface.

Then, we use the splice command to remove that number from the array so it isn't used again. Although we usually need to declare and define variables, we can also add dynamic properties such as cardface to an object. This can only be done if the object is dynamic, which the Card object is by default because we did not define it otherwise.

The cardface property assumes the type of the value it is assigned such as a Number , in this case. This is not the best programming practice. A better practice is to define a class for the Card , complete with an ActionScript file declaring a package, class, properties, and constructor function. However, this is a lot of extra work when only one little property is needed, so the benefits of convenience outweigh the benefits of sticking to strict programming practices.

In addition, the MatchingGame3. This syntax makes the Card movie clip show its picture. So, all 36 cards are face up rather than face down. It takes the value of the property cardface , which is a number from 0 to 17, and then adds 2 to get a number from 2 to This corresponds to the frames in the Card movie clip, where frame 1 is the back of the card, and frame 2 and so on are the picture faces of the cards.

Obviously, we don't want to have this line of code in our final game, but it is useful at this point to illustrate what we have accomplished. This is useful to get visual confirmation that your code is working so far. I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time. Pearson Education, Inc. This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site.

Please note that other Pearson websites and online products and services have their own separate privacy policies. To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:. For inquiries and questions, we collect the inquiry or question, together with name, contact details email address, phone number and mailing address and any other additional information voluntarily submitted to us through a Contact Us form or an email.

We use this information to address the inquiry and respond to the question. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes. Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information informit. On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information.

However, these communications are not promotional in nature. We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information.

The information gathered may enable Pearson but not the third party web trend services to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising.

Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site. Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time. If a user's personally identifiable information changes such as your postal address or email address , we provide a way to correct or update that user's personal data provided to us.

This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service informit. Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT.

If you choose to remove yourself from our mailing list s simply visit the following page and uncheck any communication you no longer want to receive: www.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest pearson. California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice.

TIGSource has an official Discord server now! Welcome, Guest. Please login or register. Did you miss your activation email? Check out Digital Ocean more details in this thread. Level 1. Making games in actionscript 3. I hope it's okay to pimp my own tutorials here, if not I'm sorry. I've had a ton of requests for tutorials on how to make games in flash, and the most I've seen promote completely horrible coding practices. So I wrote some of my own.

They're all based on using free tools some of which are windows only so it's easy to follow along. I've got parts six and seven all typed up and ready to go, but suggestions for coming parts are very much appreciated. Also, if you find any inconsistencies I'd be happy to have them pointed out to me. Level Re: Making games in actionscript 3. Thanks for posting this.

I started learning Flash a few months ago but haven't progressed very much so this was worth reading through. It didn't have too much that I didn't know already, except that I did not know you could embed symbols from other. One problem I noticed in your tutorial was that you use it's for its a lot. I know this is a common problem but it still makes it less readable to me. For future parts, I don't have any preferences, I'd recommend just to focus on things that are generally useful and not covered in other tutorials I guess.



0コメント

  • 1000 / 1000