Jump to content
  • Advertisement
Sign in to follow this  
franklyn

[web] Actionscript OOP problem.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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) ?

Share this post


Link to post
Share on other sites
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 { }

Share this post


Link to post
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 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!