00:00
00:00
Newgrounds Background Image Theme

Zombiehit just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3: Code organization

504 Views | 9 Replies
New Topic Respond to this Topic

AS3: Code organization 2014-06-15 18:46:05


I am having difficulty organizing my code. I know a few various methods, but my main problem is separation of classes.
Here's a simple example to illustrate my point.

Currently I have a static class for each frame, such as below:

public class Frame01 {
  // methods/properties ...
  public var isMovieClipOnStage:Boolean;
  public static function loadFrame():void {}
}

Now let's say I have another class (non-static), such as a music player, that I activate on that frame (and perhaps others)

public class MusicPlayer {
  // methods/properties ...
  public var isMovieClipOnStage:Boolean;
  public function create():void { }
}

My problem arises when MusicPlayer needs to access properties in Frame01, such as "isThisFrameActive" or "isMovieClipOnStage." Do I just use the static-ness to my advantage (such as Frame01.isMovieClipOnStage)? Or do I try to separate these classes and allow a "callback" function/property, passed by Frame01 (such MusicPlayer.variablePassedByFrame01)?

Does this make sense? If so, am I on the right track, or are static frame classes a terrible idea in the first place?


BBS Signature

Response to AS3: Code organization 2014-06-15 18:51:24


At 6/15/14 06:46 PM, coln wrote: Does this make sense? If so, am I on the right track, or are static frame classes a terrible idea in the first place?

Frame01 sounds like you're going about the entire thing the wrong way. Why not have a Frame class, and it has functions that allow you to set the variables inside. Put the frames in an array, and get what you need by

currentFrame.isFrameActive

where currentFrame is the current Frame object. The music player should be independent of all frames I'd assume. If you give more info on what you're trying to accomplish overall (not the specific code thing you're trying to do), we can give a better explanation.

Response to AS3: Code organization 2014-06-15 19:10:07


At 6/15/14 06:51 PM, MSGhero wrote: If you give more info on what you're trying to accomplish overall (not the specific code thing you're trying to do), we can give a better explanation.

Hmm. If that's the case...
I'm creating a game with different frames (main menu, level select, levels, credits, etc.). Each frame has different needs. For example the main menu and level select needs a button handler, while the levels frame needs to be able to handle the level data/level artwork/etc.

I'm currently working on a level editor which has many plugins (such as an OS-style selection box, a pathway drawer, etc). I'm trying to link the different plugins to the level editor without dependencies on the LevelEditor.as static class, however I need to know things like "is the mouse click for plugin A or plugin B?"

If static classes for frames aren't the way to go (I'm not really sold on them, since they haven't been helpful), what would be the best way to organize stuff like that? Does the Main.as class handle the frames? If so, what about plugins?


BBS Signature

Response to AS3: Code organization 2014-06-15 19:26:57


At 6/15/14 07:10 PM, coln wrote: If static classes for frames aren't the way to go (I'm not really sold on them, since they haven't been helpful), what would be the best way to organize stuff like that? Does the Main.as class handle the frames? If so, what about plugins?

I thought your frames were like frames of animation or of a movieclip. They're usually called "States." Either way, in that case you should have a static sound manager (I like SoundAS) that each state has access to. When you enter the menu state, you can play the menu music, then when you start the game, you can stop the menu music (when you exit the menu) and play the game music (when you enter the game).

Your level editor class could have a currentPlugin var, and you could tell it currentPlugin.clicked(mouseX, mouseY) or something. Your plugins would have to all extend from a common class or implement an interface for that to work.

Response to AS3: Code organization 2014-06-15 19:36:13


At 6/15/14 07:10 PM, coln wrote: I'm creating a game with different frames (main menu, level select, levels, credits, etc.). Each frame has different needs. For example the main menu and level select needs a button handler, while the levels frame needs to be able to handle the level data/level artwork/etc.

You write a class for each one, in which you handle the specific functionality.
What is the problem?

I'm currently working on a level editor which has many plugins (such as an OS-style selection box, a pathway drawer, etc). I'm trying to link the different plugins to the level editor without dependencies on the LevelEditor.as static class,

That's what Events are for.

however I need to know things like "is the mouse click for plugin A or plugin B?"

Each plugin adds listeners for MouseEvents to its view.
Your question seems to boil down to "how do I add listeners to different things?"

If static classes for frames aren't the way to go (I'm not really sold on them, since they haven't been helpful), what would be the best way to organize stuff like that?

I'm not sure what you mean by "static class".
A class is never static.
Members of a class can be static.

I could imagine that you are referring to a class that only has static members.
But your Frame01 class does not full fill that requirement.
So what do you mean?

Does the Main.as class handle the frames? If so, what about plugins?

On one hand, you are asking about specific ways to implement the desired functionality.
On the other, you never explained what a plugin is. The term is very abstract.

This all seems very very vague.

Response to AS3: Code organization 2014-06-15 19:57:17


At 6/15/14 07:26 PM, MSGhero wrote: I thought your frames were like frames of animation or of a movieclip. They're usually called "States."

Okay, so using a static class (aka a class with all static methods/properties), is the best way to create states?

At 6/15/14 07:36 PM, milchreis wrote: You write a class for each one, in which you handle the specific functionality.
What is the problem?

My problem is more of a question. Is that the best way to do it?

I'm currently working on a level editor which has many plugins (such as an OS-style selection box, a pathway drawer, etc). I'm trying to link the different plugins to the level editor without dependencies on the LevelEditor.as static class,
That's what Events are for.

My question was more of, should i riddle the plugin with frame-specific options, or should the frame pass parameters to the plugin.

I'm not sure what you mean by "static class".
A class is never static.
Members of a class can be static.

When I said "static class" I meant a class whose methods and properties are all static.

But your Frame01 class does not full fill that requirement.
So what do you mean?

That was a mistake on my part. That should be a static member.

This all seems very very vague.

I apologize. I am just not very good at explaining what my problem is. I'll try again:

I have game states. Each state has a static class to represent it (as defined above). Each state requires "plugins" (sound manager, button handler, movieclip mover, etc). Each plugin needs to communicate with each other (i.e. "if button handler is clicked, play the sound manager"). What is the best way to get them to communicate with each other: passing parameters via the current state's static class (i.e. initPlugin(someMember)), or by accessing the current state through the plugin (i.e. StaticState.someMember).


BBS Signature

Response to AS3: Code organization 2014-06-15 20:33:31


At 6/15/14 07:57 PM, coln wrote:
At 6/15/14 07:26 PM, MSGhero wrote:
My problem is more of a question. Is that the best way to do it?

This question cannot be answered without looking at the specific implementation.
In general, I'd say yes, this is the way to go.
If they all share some functionality you could (and should) move that into a shared super class for example. (or use composition)

My question was more of, should i riddle the plugin with frame-specific options, or should the frame pass parameters to the plugin.

Consider each class to stand alone.
If you want it to do something, call a method on that object.
If it wants other things to do something, let it dispatch an Event.

But your Frame01 class does not full fill that requirement.
So what do you mean?
That was a mistake on my part. That should be a static member.

Making members static just to access them is a common bad practice.
It's not an access modifier, it's the question if you want to have this member "per object" or "per class".
This question should be asked to find out if a member should be static or not.

I have game states. Each state has a static class to represent it (as defined above). Each state requires "plugins" (sound manager, button handler, movieclip mover, etc). Each plugin needs to communicate with each other (i.e. "if button handler is clicked, play the sound manager"). What is the best way to get them to communicate with each other: passing parameters via the current state's static class (i.e. initPlugin(someMember)), or by accessing the current state through the plugin (i.e. StaticState.someMember).

Speaking of the general question "How do I wire up all the objects in my application to work with each other?".
The answer is: Events.
As part of what I described above.
Or in more sophisticated frameworks employing the mvc pattern, like robot legs for example.

Response to AS3: Code organization 2014-06-15 20:38:43


At 6/15/14 08:33 PM, milchreis wrote: The answer is: Events.
As part of what I described above.
Or in more sophisticated frameworks employing the mvc pattern, like robot legs for example.

Okay, so would you recommend I used events, or employ an MVC pattern? I know the answer is in part how complicated do I want this thing to get, but I am using this in part as a learning tool, so I am open to complexities.

If events, what event would I dispatch for a variable change? Say, "isDragging"? I don't want to recreate a drag mouse down, mouse up, and mouse move on every plugin to figure out whether a movieclip is being dragged. It would be easier to just check if the variable "isDragging" is true or not.


BBS Signature

Response to AS3: Code organization 2014-06-16 03:46:57


At 6/15/14 08:38 PM, coln wrote:
At 6/15/14 08:33 PM, milchreis wrote: The answer is: Events.
As part of what I described above.
Or in more sophisticated frameworks employing the mvc pattern, like robot legs for example.
Okay, so would you recommend I used events, or employ an MVC pattern? I know the answer is in part how complicated do I want this thing to get, but I am using this in part as a learning tool, so I am open to complexities.

mvc is more or less just a standardised way to build a relationship between objects.
Here's a video on how how to roll your own:
https://www.youtube.com/watch?v=tlgrYh9_Hpk

Here's robotlegs:
http://www.adobe.com/devnet/actionscript/articles/intro-robotlegs-pt1.html

If events, what event would I dispatch for a variable change? Say, "isDragging"?

Event.CHANGE or a custom one

I don't want to recreate a drag mouse down, mouse up, and mouse move on every plugin to figure out whether a movieclip is being dragged.

I'm not quite sure why you would want to recreate MouseEvents.
If they are required for the internal mechanics of your plugin, use them. But usually, that's not the information expected from your plugin to be returned.

Take a look at a slider component for example.
Internally, it has to listen to user input to do its thing.
But if you are using a slider, you don't care about that. You are interested in the value of the slider.
You register for the change event and when that happens, you read out the changed value.

It also redispatches events for the interaction with the thumb.
If you need to implement those depends on what you need.

It would be easier to just check if the variable "isDragging" is true or not.

You'd have to periodically check when the variable changes in that other class.

Response to AS3: Code organization 2014-06-17 22:24:47


At 6/16/14 03:46 AM, milchreis wrote: mvc is more or less just a standardised way to build a relationship between objects.
Here's a video on how how to roll your own:
https://www.youtube.com/watch?v=tlgrYh9_Hpk

Hmm. That video was very informative. Thank you for the links. I shall have to research more into this. Thanks again!


BBS Signature