[web] Actionscript OOP problem.

Started by
2 comments, last by Wan 15 years, 9 months ago
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.
Advertisement
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) ?
Quote:Original post by ToohrVyk
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) ?


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 { }
Quote:Original post by ToohrVyk
[..] are you allowed to initialize

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

Nope.
Quote:Original post by DouglasThompson
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

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 DouglasThompson
You 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.

This topic is closed to new replies.

Advertisement