# [web] Actionscript OOP problem.

This topic is 3482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently took up ActionScript to try and develop some flash based games but i cant seem to figure out how to do oop in flash. i've tried some basic class definitions but the ends result just doesnt seem to work. heres a simple bit of code i wrote up to get a feel for objects and classes
package Game
{

public class rod
{
public function rod(){}
public function tie(){}
}

public class game
{
var rodObj:rod = new rod();
rodObj. //the function doesnt come up and i dont see "rod" highlighted.

}
}

as you can see i dont receive any intelisense suggestions on the new object nor does it recognize the class defintion and format "rod" as a keyword.

##### Share on other sites
I'm not very familiar with actionscript, but are you allowed to initialize and use variables at class scope (instead of inside a class method) ?

##### Share on other sites
Quote:
 Original post by ToohrVykI'm not very familiar with actionscript, but are you allowed to initialize and use variables at class scope (instead of inside a class method) ?

You're allowed to declared and initialize at the class level, but I guess I'm not "language-diverse" enough to understand how the concept of using variables at the class level would work? At any rate, you can't in Actionscript.

Anyway, @OP:

What ToohrVyk is really getting at is that in your "game" class, you're trying to execute code at the class level. You'll need to stick the code you currently have at the class level into a function. For instance, in game's constructor:

public class game{	public function game()	{		var rodObj:rod = new rod();		rodObj. // etc...	}}

Beyond that, you can only have one public class per "package block", and only one "package block" per file. So in other words, you'll probably want to have a rod.as and a game.as file, one for each respective class (and files have to be named after the public class they contain). Both can be in the Game directory and use the Game package, though.

You can have additional classes within a single file, but they'll have to be included outside of the package, and will only be available to the file defined in that class (and POSSIBLY the entire package if you declare them internal, but I haven't messed with that). You probably won't want to worry about this stuff for a little while, though.

Finally, the IDE I used has difficulty with code completion when you don't specify the return types of a function, not including constructors which actually cannot have one. In particular, your tie function would need to be declared like this:

public function tie():void { }

##### Share on other sites
Quote:
 Original post by ToohrVyk[..] are you allowed to initialize

Yep.
Quote:
 and use variables at class scope [..]

Nope.
Quote:
 Original post by DouglasThompsonWhat ToohrVyk is really getting at is that in your "game" class, you're trying to execute code at the class level. You'll need to stick the code you currently have at the class level into a function. For instance, in game's constructor

Exactly, although of course in this example rodObj is now local to the constructor. Defining and constructing the variable can be done in class scope as a member variable.
Quote:
 Original post by DouglasThompsonYou probably won't want to worry about this stuff for a little while, though.

It's vital information though, even at this stage. It's not something that's obvious at first, and it may lead to confusion later on.