Skip to main content
Known Participant
December 14, 2009
Answered

Game graphics/animation questions

  • December 14, 2009
  • 2 replies
  • 1392 views

I'm new to Flash, so I am *not* aware of some seemingly obvious things, such as the existence of certain methods or the way that .fla interacts with code.  Please keep this in mind when giving explanations

I was reading a tutorial and it said that you can reference the symbols directly just with the symbol name.  Is it possible to do this without applying actions but rather by using an external .as file?  I tried, but I got lots of undefined errors even though I import the stage class and so on.   This is the site that I was looking at: http://www.tutcity.com/view/keyboard-controls-in-as3.19742.html.  However, it seems to require adding actions in flash.  I got it working when using the Flash actions as suggested, but I'd like to have the code in the .as files only, rather than relying on Flash actions, for more control.  I don't know how to get the .as file to recognize stage, so I get the following errors:

1120: Access of undefined property stage.

1120: Access of undefined property keyHit.

Do I have to instantiate the stage or something?  Shouldn't the .fla file know what the stage is?  I've tried import flash.display.* too and it didn't work.  This is my code.

package code
{
    import flash.events.*;
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.ui.Keyboard;

    public class Paradox extends MovieClip
    {


        function keyHit(event:KeyboardEvent):void
        {
           var enemySpeed:uint=5;

            switch (event.keyCode)
            {
                case Keyboard.RIGHT :
                MechDragon.x+=enemySpeed;
                break;

                case Keyboard.LEFT :
                MechDragon.x-=enemySpeed;
                break;
               
                case Keyboard.UP :
                MechDragon.y-=enemySpeed;
                break;
               
                case Keyboard.DOWN :
                MechDragon.y+=enemySpeed;
                break;
            }
        }
       
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);    
    }
}

I don't understand the relationship between the .fla file and the .as file as far as animation goes.  Is there a redraw command, or a next frame command or something? Also, is there a command to get the frame?  I want to be saving a bunch of frames to a list or something (gonna do some time manipulation stuff).  I also want to be able to change the graphics depending upon what happens.  For example, if the character moves left, switch to the moving left sprite.  If the character moves up, switch to the moving up sprite, etc.  But I want it to be linked to the same object, just have the image change.  Can I do this or must I make lots of objects and mess around with some graphics switches?

This topic has been closed for replies.
Correct answer

Aww, I was hoping it wasn't just changing the alpha   The walk cycle is one thing (thanks for that suggestion, I'll try it), but I've got another problem.  Let's say a character is walking right.  Then he jumps.  Then he turns around and goes left.  If you do the animation in the timeline and play the clip, won't you be unable to predict every single combination of a player's actions?  Is there a way to tell the character at specific points to change the images to the appropriate ones?

If I were going to do some 3D animation in OpenGL, I'd have an object of some class. It would have properties like the vertices of its triangles.  So if I wanted to animate that object, first I'd change all the vertices to their desired positions.  Then I would advance the frame and redraw the image according to the new vertices.  And I'd repeat the process in some loop.  The object still would be the same object, but its properties would be updated, and its changes would be reflected in the rendering.

I'd like to know if there's some sort of equivalent in Flash.  I realize these are two very different things.  But is there a way to, say, assign several different images to a symbol and then let the code tell Flash when to use which image (or image sequence)?  I want to assign a specific image sequence if the character changes direction and starts facing the left.  The moving left animation would be different from the jump animation, which would be different from the climbing up a ladder animation.  So is there a way to tell Flash to change the character's walk cycle animation to a different one?


You can do this in various ways... just like in DirectX even - you can animate vertices all you want in Flash - see the graphics class for that.

You could have all your different animations in your player clip, on its timeline, and just goto various frames as you needed.

Or you could have different clips or event bitmaps and swap them as you needed.

>>But is there a way to, say, assign several different images to a symbol and then let the code tell Flash when to use which image (or image sequence)?

The easiest way to that would be to use the timeline and go to frames as you needed...

2 replies

December 14, 2009

First of all, do not listen to Venian he is obviously clueless about oop and using external classes. As you mgiht know, using classes is not all about reusability - it's about organization just as much. When you have a game that totals thousands of lines of code, you'll be happy you broke up your code into classes - that is WAY easier to debug than thousands of lines of code on the timeline.And also if you code in something like FlashDevelop you get intellisense, autoComplete and more... much nicer to code in than in Flash.

Anyway, your stage error is because you cannot have code outside of a function in a class. Put the stage code inside your class constructor... I assume this is a document class you're using... a constructor is automatically initialized in any class - and is a function with the same name as the class itself.

So:

public function Paradox(){

     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);

}

Also: >> I was reading a tutorial and it said that you can reference the symbols directly just with the symbol name.

Yes, you give your library symbol a class name and you can then reference it in your code. For example, if you have a MovieClip library symbol with a class name of 'myShip' you could bring it on stage with:

var player:myShip = new myShip();

addChild(player);

player.x = 100;

player.y = 100;

etc...

SylvandyrAuthor
Known Participant
December 14, 2009

Thank you!  It works now.

I'm having issues with linking files and folders and such, though.  I had no problem when I put the code in Paradox.af.  But now I want to organize the classes properly (putting it in Paradox.af was just the first testing step to get the code working).  I do not know how to properly reference the code if I had a folder hierarchy, say, like the one below.  * denotes a folder, tab denotes the contents of that folder.

* Paradox-1-0

     Paradox.fla

     * code

          Paradox.as

          * Enemies

               MechDragon.as

I want the Paradox.as file to be the control for the game where all the higher level stuff happens.  I want to use the MechDragon class as a class definition for one of the enemies.  Currently the code is still in the testing phase, so I just have an image and pressing the arrow keys moves it around accordingly.  The functions will change later.

Now, if I place the code in the Paradox.as file, everything works.  I have the Paradox.fla file linked to code.Paradox.  Obviously if I move the code to the MechDragon file there aren't going to be any references.  I was looking at a sample of game code and it had exported the symbols to its classes.  So I tried right clicking on my symbol, clicking Properties, then selecting Export for ActionScript and Export in Frame 1.  I put the class as MechDragon and the Base class as code.Enemies.MechDragon.  It says "A code for this class could not be found in the class path, so one will be automatically generated in the SWF file upon export."  Then I pressed OK.  Then I tried to Publish the .fla and got this: "1046: Type was not found or was not a compile-time constant: MechDragon."  But the source was "Paradox.as", line 2.  But line 2 is just a { in Paradox.as.  I don't understand what my Paradox.as file has to do with anything, cause it doesn't reference MechDragon at all; why would the error be there?  This is my empty Paradox.as file:

package code
{
    import flash.events.*;
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.ui.Keyboard;
    import flash.text.TextField;

    public class Paradox extends MovieClip
    {

    }
}

What's going on?  Do I have to create an instance of the MechDragon class in the Paradox class or something?  How am I supposed to link all the classes to the .fla file?  There's only one place you can link to in the Properties tab in the .fla file and I figured it should link to the controller class =/

Venian
Inspiring
December 14, 2009

I can't say for certain what's your problem. Once you import the as file you should have no problem.

But yes you need to inialize practically almost every class you work with in flash. If it says something is missing like a method you will have to import the class for that method in your as file. That's because you work in an external file. Flash has all those methods built in while external files don't.

But why would you want to use an external file to make your game? External files and classes are useful when building reusable code with limited functionality. Yes you can build an entire website into an external file but it makes no sense. And I've seen several people doing this. It's just time consuming, a pain in the back to debug problems and hard to modify later. And these intensify if you use multiple external files.

The idea behind costum classes is that of OOP (object-oriented programming). This is a concept that should enable you to build reusable code for future usability. That's what you do when you build a costum Math class for instance which you call everytime you want it to calculate something more complex than the basic math class can do. But when you build a site or a game with graphics and sounds and personalized content, the only way to use that code for another project is to modify all that content which will be hell for you using external classes.

Take as an example any costum tween class you find on the net. They are a compilation of a ton of code that has only one purpose to tween and they can be called anytime in your project to only do that, with minimal stress and coding. That's OOP.

SylvandyrAuthor
Known Participant
December 14, 2009

[EDIT]: Accidental post, hold on.