00:00
00:00
Newgrounds Background Image Theme

Care2mchBEAR 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!

calling classes from another class

1,065 Views | 8 Replies
New Topic Respond to this Topic

Im writing this game in as3, and using Flash Develop to create it, im kinda having problems calling a class from my PlayState class to add it onto the stage, im not sure if im doing someting wrong or not, cause ive looked up methods on how to do this, but it no change, so can any of you people give me an example on how to do this ?

Thanks in advance

Response to calling classes from another class 2013-07-22 15:08:49


At 7/22/13 02:51 PM, BigDeathRay wrote: Im writing this game in as3, and using Flash Develop to create it, im kinda having problems calling a class from my PlayState class to add it onto the stage,

The simplest way would be to let your PlayState class extend a DisplayObjectContainer - say Sprite for example - and add the DisplayObjects to itself.
This way, you can easily handle all the display assets within the state class.

All you have to do in your document class is to add/remove the different PlayState objects.

The question is very vague and the given solution may or may not be appropriate.

Response to calling classes from another class 2013-07-22 15:47:00


You'd have to show us your code for us to understand what you're trying to do.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to calling classes from another class 2013-07-23 06:16:33


At 7/22/13 03:08 PM, milchreis wrote:
At 7/22/13 02:51 PM, BigDeathRay wrote:
The question is very vague and the given solution may or may not be appropriate.

okay then, my playstate gathers all the classes to create the game, it does this by referencing the class, and then adding them onto the stage. Lets say im trying to add the Player, which is a sprite, how would i be able to do this ?
could it be done this way? :

Playstate:

package {
public class Playstate {
protected var _player:Player;
public function player():void{
_player = new Player;
addChild(_player
}

Response to calling classes from another class 2013-07-23 06:19:18


At 7/22/13 03:47 PM, MintPaw wrote: You'd have to show us your code for us to understand what you're trying to do.

/\
|
|

lol

Response to calling classes from another class 2013-07-23 06:44:08


At 7/23/13 06:16 AM, BigDeathRay wrote: okay then, my playstate gathers all the classes to create the game,

How does your code even compile?
Don't you get an error?

What you are trying to do is create the document class I mentioned.

package {
public class Playstate {

I would not call it "PlayState", the name is kind of misleading.
While the name doesn't really matter, yours seems to be chosen on purpose and having your main represent a "state" is probably not a good one.
Go for "Main", "Game" or something like this. Maybe the title of the game.
Depending on your workign environment, you have to specify that this class is your document class.

protected var _player:Player;

Make it private.

public function player():void{

Be aware that you have to somehow, somewhere have to call this method.
Merely defining it will not do that.
You possibly want to do that in the constructor, which you haven't defined at all.
If this is supposed to be the constructor, change the name to the name of the class.

_player = new Player;
addChild(_player

That method is not defined.
You'd have to extend a DispplayObjectContainer in order to be able to do this.
You are missing a bracket.

Your code indention is horrible.
You should indent your code with tabs to make it more readable.

It looks like you threw together some lines you found on the internet without really knowing what they do.
A basic understanding is vital.

Response to calling classes from another class 2013-07-23 14:16:23


At 7/23/13 06:44 AM, milchreis wrote:
At 7/23/13 06:16 AM, BigDeathRay wrote:
How does your code even compile?
Don't you get an error?

this isnt all the code, its just the PlayState class, there are other classes, which include the main class, player class, enemy, etc. I just put up the Playstate class cause that is where the problem is, the Player class just creates a rectangle, nothing special

_player = new Player;
addChild(_player
That method is not defined.
You'd have to extend a DispplayObjectContainer in order to be able to do this.

ah that mustve been the problem *facepalm*

You are missing a bracket.

the bracket ,meh, this was written on the reply form, and not on Flashdevelop

Your code indention is horrible.
You should indent your code with tabs to make it more readable.

th reason for bad code indention is because i was confused as how the code HTML worked, do i just copy and paste, or whatever, so i just typed it in not knowing what to expect, and i apologise for that

It looks like you threw together some lines you found on the internet without really knowing what they do.
A basic understanding is vital.

i actually tried to understand some code which was put up on the internet, although i understood it most of it, i mustve done something wrong (dont count that bracket) and thanks for the help, but could you give me an example of how you would do it ?

Ps: love that collection of websites on your blog, really nice <3

Response to calling classes from another class 2013-07-23 15:09:27


At 7/23/13 02:16 PM, BigDeathRay wrote: this isnt all the code, its just the PlayState class, there are other classes, which include the main class, player class, enemy, etc. I just put up the Playstate class cause that is where the problem is, the Player class just creates a rectangle, nothing special
but could you give me an example of how you would do it ?

Follow the advice in my first post and you should be good to go.

You'd have to extend a DispplayObjectContainer in order to be able to do this.
ah that mustve been the problem *facepalm*

again, your code should produce errors

the bracket ,meh, this was written on the reply form, and not on Flashdevelop

That's nonsense, if you have a problem with a certain code, post the exact same code, not some random other stuff

th reason for bad code indention is because i was confused as how the code HTML worked, do i just copy and paste, or whatever, so i just typed it in not knowing what to expect, and i apologise for that

just copy your code in the code tags

Response to calling classes from another class 2013-07-24 12:28:46


At 7/23/13 03:09 PM, milchreis wrote:
Follow the advice in my first post and you should be good to go.

okay man, will try, thanks