Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 11-23-2009, 05:46 PM   #1
adamredwoods
Registered User
Help with OOP Classes in Flash-- creating data types?

Hi,

I'm relatively new to AS3, but have a little programming background and some AS2 experience-- but sometimes examples and concepts go over my head.

What I'm trying to do is to create data types for multiple re-use. Say that I'm creating a game, so I want to create a player data type (multiple players), but also things like ship data (multiple ships). I have learned a little C, and in C I would use a struct. In AS3, I though I could use a class, but it's turning out to be more complicated than a simple struct. For one, I cannot put the class into the Flash timeline (confusing). So I created a separate file...

moz.as:
Code:
package moz {

import flash.display.MovieClip;


// Tech list for levels
 class ShipList {
    var weapon:Number;
    var travel:Number;
    var shields:Number;
}

// the player class
public class PlayerClass {
    var race:Number;
    var pname:String;
    var money:Number;
    var ships:ShipList;

    public function PlayerClass() {
        money=100;
        //
    }
}
and then in the timeline for Flash:
Code:
import moz.*;

var player:PlayerClass = new PlayerClass();
var player2:PlayerClass = new PlayerClass();

player.ships.weapon = 1;
player2.ships.weapon = 2;
I could have put the ShipList into the PlayerClass, but I am planning to have many ships, accessed through an array.

There errors I get are:
Quote:
1046: Type was not found or was not a compile-time constant: PlayerClass.
1046: Type was not found or was not a compile-time constant: PlayerClass.
1180: Call to a possibly undefined method PlayerClass.
1180: Call to a possibly undefined method PlayerClass.
I could also be setting this up wrong in Flash. I have tried going through the Document Class, but that did not seem to make any difference.

I've search over the web for help, but I always end up with the same examples and I am not sure how to create something I actually want to use.

Any ideas? Is there a better way to create a struct? Help is greatly appreciated.
adamredwoods is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-23-2009, 06:52 PM   #2
adamredwoods
Registered User
Related, but can this be done with a class instead? I think a class would help against any misspelled variables when debugging (?).

http://www.kirupa.com/forum/showthread.php?t=335280
adamredwoods is offline   Reply With Quote
Old 11-23-2009, 06:58 PM   #3
micken
Registered User
For starters it is good practice to give each class it's own Actionscript file. From my experience trying to put more than one class in a file makes problems like this one that don't make much sense.

You can make a data type using a Class but that kind of defeats the purpose of Actionscript 3.0. Flash lets you link any library object with a class of it's own. This is your gateway from flash to code. You need a ship? make a movieclip called ship and link it to a class called Ship. Then make a file called Ship.as and inside declare a class called Ship that extends MovieClip.
Voila you now have a ship object which you can display on the screen whenever you want and store any data relevant to the ship inside.

Actionscript 3 makes putting actionscript inside frames less desirable. I like to think of each frame in FLASH as a little "box" of code and use it for small tasks like firing an event at a certain part of an animation or letting the rest of my game know an animation has finished.
micken is offline   Reply With Quote
Old 11-23-2009, 09:16 PM   #4
TheCanadian
Noo doot aboot it, eh?
 
TheCanadian's Avatar
Location Take a guess . . .

Posts 6,808
You can only have one public class per AS file. Any classes that are not defined withing the package block are only accessible by the class that is in the package block. And the AS file should be named after the main class name, eg: PlayerClass.as

__________________
Proud Montanadian
We tolerate living and breathing.

Name Brand Watches
TheCanadian is offline   Reply With Quote
Old 11-23-2009, 09:16 PM   #5
adamredwoods
Registered User
Quote:
Originally Posted by micken View Post
For starters it is good practice to give each class it's own Actionscript file. From my experience trying to put more than one class in a file makes problems like this one that don't make much sense.
Quote:
And the AS file should be named after the main class name, eg: PlayerClass.as

Thank you both for these tips, this is what finally got my code to work. I don't understand why someone can't put multiple classes under one package, but rather has to separate them into one file. Seems unnecessary, but I'll accept it.

...also for other people who are interested: I needed to put PUBLIC in front of all my VARs (that I wanted public).

Last edited by adamredwoods; 11-23-2009 at 09:19 PM..
adamredwoods is offline   Reply With Quote
Old 11-23-2009, 09:26 PM   #6
TheCanadian
Noo doot aboot it, eh?
 
TheCanadian's Avatar
Location Take a guess . . .

Posts 6,808
Quote:
Originally Posted by adamredwoods View Post

Thank you both for these tips, this is what finally got my code to work. I don't understand why someone can't put multiple classes under one package, but rather has to separate them into one file. Seems unnecessary, but I'll accept it.
You can have multiple classes in the same package, just not in the same AS file.

Quote:
Originally Posted by adamredwoods View Post
...also for other people who are interested: I needed to put PUBLIC in front of all my VARs (that I wanted public).
That's because, by default, variables are in the internal namespace which means that they can only be accessed by classes and functions in the same package.

__________________
Proud Montanadian
We tolerate living and breathing.

Name Brand Watches
TheCanadian is offline   Reply With Quote
Reply

Tags
class, flash, oop


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:33 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com