Forums

ForumsProgramming Forum

classes and stuff

Posted Jan 28, '12 at 10:54am

alex57834

alex57834

89 posts

So far I have finished learning all the basics of action script loops, arrays event handlers,functions. ext and I am now trying to make my first game.

I am now stuck on how classes work, how to set them out, what you do with them, and what they are for and what do you put inside of them.

This has become a problem for me because I have looked on various tutorials and sites and they all were quite confusing.

Please if you could I'm only twelve so if you could put in the most simple language you could it would be a great help.

 

Posted Jan 28, '12 at 7:18pm

Carlytoon

Carlytoon

329 posts

A class is a fundamental part of the OOP programming, it can be anything you want, here is a example:

package
{
public class mathClass
{
static public function Pow(number:Number,exponent:int=2):Number
{
var Int:int=exponent;
for(var i:int=0;i<exponent;i++)
{
Int*=exponent;
}
return Int;
}
}
}

In this case my class is a Math class with a Pow funtion.


But actually your class can be a custom MovieClip or Sprite. You can base your objects from other objects here is a exmple:

customSprite:
package
{
import flash.display.MovieClip;
public class customSprite extends Sprite
{
public function function1:void()
{
//custom stuff.
}
public function function2:void()
{
//custom stuff.
}
public function function3:void()
{
//custom stuff.
}
}
}


basedObject:
package
{
public class basedObject extends customSprite
{

}
}

In this case I define a customSprite class the will extend Sprite class, and then i define a basedObject class that will extend customSprite. You need to know that basedObject will have customSprite functions.

Anyway if want to know more just ask, hope this helps you

 

Posted Jan 29, '12 at 3:48am

alex57834

alex57834

89 posts

So is it just to make everything easier to handle like different folders for different objects or sprites.Is it so you can call for different things like functions within different classes.

Also could you explain packages
and the class customSprite extends sprite to me sorry for being so stupid.

 

Posted Jan 29, '12 at 1:09pm

BlueJayy

BlueJayy

27 posts

AS3 is an object oriented language, which means you can make objects of different classes to complete different tasks. For a real-life example, think of every individual student in a school as objects of the "Student" class. All of the students have similarities, such as they all have names, ages, and personalities, but each of those "values" are different for each individual student.

So, classes can be thought of as a way to organize your code to make writing it a lot easier.

package {

public class <class name> extends <something> {

public function <class name> (&ltarameters&gt {

<constructor stuff here>

}

}
}

So above, I've typed out the basic way to start a class.

&quotackage"
For now, don't worry about what package does. You don't really need to know it's function, and I feel that it's a little confusing to people that are still learning the basics of classes.

&quotublic class <class name> extends <something>"
Here, you're naming your class and telling what it extends. Extending a class gives you access to methods, variables, and constants defined in other classes. So for example, if you extend the "MovieClip" class (This is a pre-made class that AS3 knows is there, but you can make your own classes with their own functions if you want) , you're gaining access to functions such as "gotoAndStop" and "addChild", as well as variables, such as x and y positions and the current frame of the object.

&quotublic function <class name> (&ltarameters&gt"
Here's the beginning of your constructor function. Whenever you create a new object of a class, the constructor function will run, just like any other function you may create. An example for you parameters may be to set the starting position of an object on the stage.

So I'm just going to write a quick example of how you might use a class.

Here's a simple class that I called "Ball".

package {

public class Ball extends MovieClip {

public function Ball (xPos, yPos) {

x = xPos
y = yPos

}
}
}

So to use this in your main AS3 file (Your main class), whenever you want to add a "Ball" object, you would put "addChild(Ball(<number>,<number&gt)" The two numbers would tell the new object where to be once it is added.

And don't worry about not getting everything at once. You're not stupid. Classes are a strange thing to get a handle on when you're first starting out. But once you figure out the basics on how to structure classes, just keep using them and they'll eventually be a breeze and you'll be using them all over the place. ^^

If things are still confusing you, feel free to ask.

 

Posted Jan 29, '12 at 2:01pm

alex57834

alex57834

89 posts

Thanks for going to that much effort writing it out and making it simple at the same time, it was a great help.

The only things bugging me now is how you have said the main AS3 file(Your main class)Where is this class located is it on the timeline or is it the document class.

If I have got this completely wrong, tell me.

Also, could you tell me a example structure to a very simple application so you could give me a small insight into the structure of perhaps, a game.

 

Posted Jan 29, '12 at 10:13pm

BlueJayy

BlueJayy

27 posts

I would suggest moving away from programming in the timeline, (if that's what you're doing now) as it's not the most organized or optimized form of programming. Everything should be done as separate ActionScript files.

Everything I'm about to type is a lot of setup. It may seem like a confusing process at first, but once you start cranking out games, it becomes a very quick process.

So, to start out, make a new folder and call it whatever you want.Then, open up Flash and create a new ActionScript 3.0. (Just like you normally would.) Save that file into the folder you created. (From now on, I'll be calling this file the Main Fla) Then, inside the folder you made, make a new folder called "Classes". In Flash, go to File > New and make a new ActionScript 3.0 Class. Immediately save the file into the Classes folder that you created. (From now on, I'll be calling this file the "Main Class&quot Do not use any spaces or non-alphanumerical things in the name.

Now go back to your Main Fla and go to File > Publish Settings. Then click on the little image that looks like a wrench near the top of the window that pops up. Now, in the new window that pops up, type in the name of your Main Class. Then, if you look below that, you should see a few tabs, such as "Source Path" and "Library Path" etc. Make sure you have Source Path selected. This tab window lists all of the files that your Main Fla will reference while coding. It's basically the central hub that tells the Main Fla where everything is. If there is anything in the tab window, (There's probably one thing that has a folder with a . next to it) click on it and press the "-" button. Then click on the folder icon next to the "-" button and browse to the Classes folder that you created. Select the Classes folder and hit ok.

All you were doing in all of that was telling the Main Fla which file it should start up first and where to find all of the files that you'll be making.

Now go to the Main Class file that you created and we'll actually start coding.

To start out, type (And I mean type. Don't copy and paste. You won't remember anything that way) the following:

package {

import flash.display.MovieClip

public class <Main Class Name> extends MovieClip {

public function <Main Class Name> () {

}
}
}

That's the base code for pretty much every class you'll be making as you start to learn about classes. Obvisouly, replace <Main Class Name> with whatever you named the Main Class file. From there, you're coding pretty similar to how you would on the timeline. If you happen to find the need to make more classes than just the Main Class, (You may not for every project, but most of them call for more than one) make a new ActionScript 3.0 Class file and save it into the Classes folder.

I'll just write a quicky program to get you started.

Main Class: (Called Main)

package {

import flash.display.MovieClip

public class Main extends MovieClip {

var theball:Ball = new Ball(100,100)

public function Main () {

addChild(theball)

addEventListener(Event.ENTER_FRAME, enterframe)

}

public function enterframe (event:Event) {

theball.y++

}

}
}

Ball Class: (Called Ball)

package {

import flash.display.MovieClip

public class Ball extends MovieClip {

public function Ball(xPos, yPos) {

x = xPos
y = yPos

}

}
}

So all I have here are two classes. One is the main class, and the other is the ball class. The main class is creating a new object of the ball class and adding it to the stage. Then every frame, it's moving that object down 1. The Ball class has parameters that lets me set its original x and y positions. When I created the object "theball" I set it's starting coordinates to 100,100. Try testing something out like that yourself and seeing if it works.

Oh, quick side note. You won't actually see anything going on when you run the game, since we didn't make any images. I don't know if you know how to do that or not, so I'll just go ahead and tell you anyway. Since we have a ball class, draw and circle and then select it. Go to Modify > Convert to Symbol. Name the symbol "Ball", make its type a MovieClip, and then go down to advanced and check Export for ActionSctipt. Then make sure next to Class, it say "Ball" and then click ok. Now Flash knows to put the image of the ball wherever the ball object is on the stage.

If you run into any problems or errors, feel free to post back.

 

Posted Jan 29, '12 at 11:21pm

Carlytoon

Carlytoon

329 posts

excelent explanation blueJayy

But we forgive to tell to alex that a class function can be overrided to complement that class. And remember that override is pretty usefull when programming.

Well alex, a class function can be overrided to complement it, here is a example:

baseClass:
package
{
public class baseClass
{
public function setPosition(xPos:int,yPos:int):void
{
x=xPos;
y=yPos;
}
}
}

myObject:
package
{
public class myObject extends baseClass
{
override public function someFunction(xPos:int,yPos:int):void
{
super.setPosition(xPos,yPos);
trace(xPos,yPos);
}
}
}

Well, in this example a made "baseClass" with a setPosition function, then I make another class called "myObject" that will extend "baseClass". I override the setPosition function using the "override" keyword, then I use "super" to paste the setPosition content, and after that i complement that function.

Now, when you call setPosition function in a object based on "myObject", the function gonna do the "baseClass" setPosition function and will trace the x and y position bacause I complement the function with that

Hope this helps you and remeber, if you got any questions just ask.

 

Posted Jan 30, '12 at 2:23pm

alex57834

alex57834

89 posts

I did everything bluejay said and it was coming up with loads of errors
so I tinkered with the code for half an hour and i was getting nowhere
so i copy and pasted bluejays code changed the names for the classes and then it came up error 1046 line 17 type was not found or was not compile time something. please could you help.

 

Posted Jan 30, '12 at 2:25pm

alex57834

alex57834

89 posts

by the way it was the main class

 

Posted Jan 30, '12 at 2:39pm

BlueJayy

BlueJayy

27 posts

Oh, sorry alex.
Forgot to import the events.

Here, put this line underneath "import flash.display.MovieClip"
import flash.events.*

That should work.

 
Reply to classes and stuff

You must be logged in to post a reply!