03-18-2009, 09:41 PM
|
#1
|
|
Registered User
Join Date: Feb 2009
Location: Sydney, Australia
Posts: 33
|
[AS3] Platform Game Advice
Hey.
I'm trying to make a platform game but I need some advice about how it should be structured.
For example, should the document class of the game be the youngest child so to speak and or should all the other classes be children of the document class?
At the moment, I have three classes, a Document Class, a Player Class which handles things like player movement, jumping, scrolling and a platform class which at the moment handles collison detection and collison reaction (making the player stand ontop of the platform, making the player stop when hitting the side of the platform).
It seems that no matter what way I arrange the classes, I get problems.
Could somebody give me a general overview of how a non tile based platform game should be arranged.
If anybody has any more questions about the way I've structured my game, please let me know.
Thanks in advanced
|
|
|
03-19-2009, 09:35 AM
|
#2
|
|
Senior Member
Join Date: Feb 2008
Posts: 387
|
the document class is in reality neither an exact parent or a child, but by its use, it can act as both! A parent child relationship occurs when one class "inherits" properties from another class, but it is hardly ever needed to inherit from the document class!
Anyway, the structure of the game can be made in any way, where your goal is to keep the classes independent of each other as much as possible! So you can do something like,
Document class - handles preloader, menu, and every element that is outside the core gameplay!
Game class - this class will handle the actual gameplay, for example, "player" and "bullet" are two separate classes inside game class, so its the job of game class to check for collision between them. Game class can look inside itself, but a class, like player class here, shouldn't look outside itself!
--Sub classes-
player - player movements, key press, AI . (I generally use player for both player and NPC)
object - defines properties of objects like solid platform, spring, traps,....
weapons - self explanatory
enemy - ""
interactions between the objects of subclass should be handled by their parent, the game class!
|
|
|
03-19-2009, 10:31 AM
|
#3
|
|
Registered User
Join Date: Feb 2009
Location: Sydney, Australia
Posts: 33
|
Yay, a reply, thanks Bluemagica.
That's an interesting approach that I haven't considerd before with this Game Class idea. I would have to make all movieclips on the stage accesible from Game Class, how would I do this? Maybe add them to the stage from Game Class?
|
|
|
03-19-2009, 11:05 AM
|
#4
|
|
Senior Member
Join Date: Feb 2008
Posts: 387
|
yep exactly, you add all the things in the actual gameplay, from within the game class, and you initiate the game class from the document class! there can be another class (which isn't exactly a OOP way), a global class, which makes communicating between classes easier...like telling the "score" from the game class to document or scene_manager class so that it can be shown at the end of game, or player preferences, etc!
|
|
|
03-19-2009, 10:41 PM
|
#5
|
|
Registered User
Join Date: Feb 2009
Location: Sydney, Australia
Posts: 33
|
Alrghty, thanks again buddy.
So, if this GameEngine class isn't linked to the DocumentClass which is as the name suggests the document class of this game, then how would I do I call movieclip instances on the stage via their instance name in GameEngine or its children?
I uploaded an image of my current class structure.
Edit:
Alrighty, here is one for ya. I have a movieclip with the instance name player on the stage. That movieclip is linked to the PlayerCode class by using the following linkage properties:
Class: PlayerCode
BaseClass: flash.display.MovieClip
I can use the keyword "player" to refer to that movieclip instance in the Class PlayerCode but I can't use it elsewhere, not even in DocumentClass which is the projects document class! Not even in DocumentClass! Why is that?Alrghty, thanks again buddy.
So, if this GameEngine class isn't linked to the DocumentClass which is as the name suggests the document class of this game, then how would I do I call movieclip instances on the stage via their instance name in GameEngine or its children?
I uploaded an image of my current class structure.
|
|
|
03-20-2009, 03:54 AM
|
#6
|
|
Senior Member
Join Date: Feb 2008
Posts: 387
|
lol, i don't think you have completely understood yet! In the structure, everything dosent have a need to have a parent or "extend" a class, an "include" will be better!
Take my advice, and use StarUML (freeware), its a great software to plan your class structure, and with an extra plugin, it can generate as3 code for your class structure....
back to your question, class name and instance name are not the same and every object instance, exits within its parent scope, so if you create a player from gameEngine, DocumentClass on the outside, and bulletClass on the inside can't see it directly...they will both need to use a reference!for documentClass, say you did something like
var game:gameEngine = new gameEngine("level1","player1",5); //lvl,name,lives
game.init();
and in gameEngine you did something like
public var player:Player = new Player();
player.x = player.y = stage.stageWidth/2;
From documentClass you can access player by:- game.player
similarly, for bullet:- MovieClip(parent).player
theres another tiny trick:
var player:Player = new Player();
player.name = P1;
then you can use
var player = game.getChildByName(P1);
|
|
|
03-20-2009, 10:57 PM
|
#7
|
|
Registered User
Join Date: Feb 2009
Location: Sydney, Australia
Posts: 33
|
Thanks again Bluemagica.
Just two questions and then I'm going to see if I can fix this problem.
Quote:
Originally Posted by bluemagica
everything dosent have a need to have a parent or "extend" a class, an "include" will be better!
|
Right, so just like I say "import flash.display.object", I can say "import DocumentClass" and that means that in whatever class I import that to, I can access all of DocumentClass' functions/methods, variables and movieclips that belong too it say?
Question 2, I'm a little bit confused about what you mean classes on the inside and classes on the outside, could you explain that a bit for me when/if you have a free moment.
|
|
|
03-21-2009, 04:30 AM
|
#8
|
|
Senior Member
Join Date: Feb 2008
Posts: 387
|
1) with include, you can access only "public" stuff, say class A looks like:
Code:
package script
{
import flash.display.*;
class A extends Sprite //A is inheriting from sprite class
{
public var age:Number = 9; //public variable
public function A() //public function + constructor
{
show_name();
}
private function show_name() //private function
{
trace("PLAYER1");
}
}
}
So in call B you can do:
Code:
package script //simply means the as file is in "script" folder
{
import script.A;
public class B
{
public function B()
{
trace(A.age); //9
A.age = 10; // modifying the public variable temporarily
trace(A.age); //10
A.show_name() //error, can't access private
var abc = new A(); //show_name() called
}
}
}
This code is just to show you the use of public and private, but there an be few errors since i wrote this without testing!
2) inside and outside simply means their visibility, you are creating the player class, within the gameEngine class, so its inside the gameEngine, means for any instance of player, gameEngine will be its parent! Similarly, documentClass lies outside gameEngine!
|
|
|
03-21-2009, 01:30 PM
|
#9
|
|
Registered User
Join Date: Feb 2009
Location: Sydney, Australia
Posts: 33
|
Argh!
Alrighty, about your answer to question one.
I am trying to access a variable exactly as you accessed the variable age from class A except I get this error:
1119: Access of possibly undefined property gravity through a reference with static type Class.
Now, what I am doing is exactly what you have done in that example except for three things:
1) My fla and *.as files are placed in the same location so my package doesn't have a name. Meaning the equivalent of my import statement on your example looks like this: import A; (I doubt its this because I get no errors saying could not find specified class)
2) Class B in the example doesn't extend anything, however the equivalent of Class B in my set up extends movieclip
3) Class B belongs to a movieclip in the library with an instance placed on the stage, hence the need for #2.
So which one of these things is causing that error? Other than those 3 things, both your example and my setup are the exact same!
|
|
|
03-21-2009, 04:21 PM
|
#10
|
|
Senior Member
Join Date: Feb 2008
Posts: 387
|
try
var a = new A();
trace(a.gravity);
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 05:59 AM.
///
|
|