Be a Supporter!

[As3] Help with something please?

  • 1,042 Views
  • 8 Replies
New Topic Respond to this Topic
SketchistGames
SketchistGames
  • Member since: Mar. 2, 2012
  • Offline.
Forum Stats
Member
Level 05
Game Developer
[As3] Help with something please? 2012-08-05 18:15:02 Reply

I am trying to make a game right now, and when you reach the end of one level it should unlock the next level. I am very unexperienced.. I wrote 51 lines of code just trying to do this, and it doesn't work :/ I need help please! I want it so when "level 2" is clicked in a level selection it does nothing unless you beat level 1... help me please! I really want to know how to do this :) any help will be VERY much apreciated, I am not trying to hire someone to make this game for me. I am trying to get help with this one thing. Please help me :) you will get credits in game and on the page on the website. Thanks for your time.


Current Trophies:
Weekly Fourth Place for Blast the Bunny 2
~SketchistGames :P

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 18:54:36 Reply

At 8/5/12 06:15 PM, SketchistGames wrote: I am trying to make a game right now, and when you reach the end of one level it should unlock the next level. I am very unexperienced.. I wrote 51 lines of code just trying to do this, and it doesn't work :/ I need help please! I want it so when "level 2" is clicked in a level selection it does nothing unless you beat level 1... help me please! I really want to know how to do this :) any help will be VERY much apreciated, I am not trying to hire someone to make this game for me. I am trying to get help with this one thing. Please help me :) you will get credits in game and on the page on the website. Thanks for your time.

AS3:

private var levels:Vector<Boolean> = new Vector<Boolean>;

public function checkLevel(level:uint):Boolean {
	return levels[level];
}

AS2 (I think):

var levels:Array = [];

function checkLevel(level):Boolean {
	return levels[level];
}

the way it works:
the vector is your level list. True is unlocked, false is locked.
just run checkLevel on the button's onClick event, and check for a return value of true.

you'll need to populate the array, of course. Remember that vectors and arrays start at 0. You can choose to either use this or just put a dummy entry in and start at 1.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 18:57:49 Reply

At 8/5/12 06:54 PM, egg82 wrote: AS3:

whoops :x

private var levels:Vector.<Boolean> = new Vector.<Boolean>;

I always forget that damn dot. It's so random .-.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
SketchistGames
SketchistGames
  • Member since: Mar. 2, 2012
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 19:12:21 Reply

I am sorry... I don't understand it very much :c I am going to send you a pm can you explain it to me more through a message?


Current Trophies:
Weekly Fourth Place for Blast the Bunny 2
~SketchistGames :P

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 19:29:26 Reply

it's fine, I can explain it here.

private var levels:Vector<Boolean> = new Vector<Boolean>;

public function checkLevel(level:uint):Boolean {
	return levels[level];
}

ste-by-step:
private - this keyword makes it so you can't access whatever it is outside of the class it's in
var - this declares a variable
levels - the variable name

- this means you're going to datatype the variable, which makes the variable "stick" to that type. If you tried to put a string into a variable with datatype int, it would throw you an error.

Vector - a vector acts pretty much exactly like an array, except you can datatype a vector
.<Boolean> - datatype the vector to a boolean. This means you can only put booleans in the vector.
Boolean - a type of variable that holds either true or false.
= new Vector.<Boolean> - we're making the variable levels hold a new vector datatyped to boolean. It seems redundant, and it may be, but that's the way it works.

public - this keyword makes it so you can access whatever it is outside of the class it's in
function - this declares a function
checkLevel - function name
(level:uint) - whatever's in the parenthesis gets passed into the function. So here you would use the function like this: checkLevel(10); or checkLevel(3); rather than just checkLevel();
level: - again, just a datatyped variable
uint - a uint is different from an int. On more basic terms, an int goes into the negatives, and a uint does not.

- this tells the function that we will be returning a value

Boolean - this tells the function that that value will be datatyped to a Boolean (true/false)
return - this ends the function and returns a value
levels - you can access the array (vector) levels here, because the function is within the same class as the variable
levels[level] - the level:uint we described above is being used here. In this case, it's being used to get an index out of levels[]

example:

the levels array holds these values:
index 0: false
index 1: false
index 2: true
index 3: false
index 4: true
index 5: false

running checkLevel(0); will return false.
running checkLevel(1); will return false.
running checkLevel(2); will return true.
running checkLevel(3); will return false.
running checkLevel(4); will return true.
running checkLevel(5); will return false.

so how do you deal with returned values? Easy. Put it into a variable.

private var chLevel:Boolean = checkLevel(0);

chLevel will now hold whatever checkLevel returns. In this case, it will be false.

of course, you can replace numbers with variables.

private var newLevel:uint = 0;
private var chLevel:Boolean = checkLevel(newLevel);

this is the same code as the above, but we replaced the number with a variable.

Hope that clears things up.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 19:31:09 Reply

those quotes are supposed to be colons (:) - but I forgot newgrounds turns a colon and a space after into a quote.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-05 19:58:13 Reply

triple-post! New record for me. Than again, by the time I post this, there may be someone who's beaten me to the punch.

anyway, I forgot that you may not know what classes are or how they work. Here's an example of a Main.as class structure:

package {
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Main extends Sprite {
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//Entry Point
		}
	}
	
}

and a sub-class structure (these are the bulk of the program):

package  {
	public class Predict {
		public function Predict() {
			//constructor
		}
	}

}

there may be a few questions revolving around these.

Q: Why are these two so different?
A: I work in FlashDevelop, which is an alternative (programmer-friendly) IDE to the standard Flash IDE. It does not have a GUI like flash, and thus you must do everything programmatically.

Q: FlashDevelop? Is it free?
A: Yes.

Q: So what do you mean "do everything programmatically"?
A: I mean exactly that. You even have to create the stage element (yes, the stage is an element) via text.

Q: Okay, but how does that relate to the differences between the two files?
A: The Main.as file (which is created automatically) creates the stage element. That is what you see in the functions main and init. The actual entry point (as you can see by the comments) is in the init, after the stage has been created. In AS3 you have the power to run code between the user opening the application and the stage actually showing on the screen.

A: Okay, so Predict.as is a sub-class. What's a sub-class?
Q: A sub-class isn't actually anything. It's all a class, I just gave it that name because it's less confusing than saying I have two different classes in one project.

Q: Alright, so why all the capital letters?
A: It's just good practice. Trust me, you'll be thankful that you have those good practices when you realize how easy it makes life. If you want to know the specifics:
Classes start with capital letters
When naming things, use capitals to denote the beginning of the next word. Example:
When naming a variable my variable, use myVariable
When using static functions and variables, use all uppercase
Functions are named like variables.

Q: So what's that function "Predict" doing there? It's kinda useless, isn't it?
A: It's called a constructor. If you want to run some code right when the class is added to the project (during runtime, of course), then that's where you stick it.

Q: So where do I put my functions and variables?
A:

package  {
	/**
	 * ...
	 * @author egg82
	 */
	public class Predict {
		public function Predict() {
			//constructor
		}
		
		private var levels:Vector<Boolean> = new Vector<Boolean>;

		public function checkLevel(level:uint):Boolean {
			return levels[level];
		}
	}

}

do it just like that.

Q: Do I need the comment at the top?
A: No, but FD will put it there automatically. I'm just too lazy to remove it.

Q: So i've got my class all set up. Now what? How do I get it to run?
A: That's pretty easy. Remember our Main.as? It comes in handy here.

package {
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author egg82
	 */
	public class Main extends Sprite {
		
		private var predict:Predict = new Predict();
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//Entry Point
			predict.checkLevel(3);
		}
	}
	
}

Q: Awesome! But what's with the naming?
A: You datatype the variable to your class name (Predict.as, which is Predict), and set it as a new class Predict (new Predict();)

Q: And the imports at the top?
A: FD does that automatically for you. It just makes it so you're able to actually use the external classes that you're using (such as Sprites, Events, etc)

Q: Sprite?
A: A movieclip with only 1 frame. Much faster than a movieclip.

Q: Wait, an event is a class?
A: Damn near everything is a class. Thankfully, most of those things (again, sprites, movieclips, events, etc) are done for you, so all you have to do is import them.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Dayl
Dayl
  • Member since: May. 15, 2005
  • Offline.
Forum Stats
Member
Level 18
Game Developer
Response to [As3] Help with something please? 2012-08-06 01:38:29 Reply

At 8/5/12 07:58 PM, egg82 wrote: triple-post! New record for me. Than again, by the time I post this, there may be someone who's beaten me to the punch.

LENGTHY EXPLANATION

You, sir, are a gentleman and a scholar.

Every post I come to, you are just there being helpful. :)

Just wanted to make sure you know. :D


BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to [As3] Help with something please? 2012-08-06 04:22:26 Reply

At 8/6/12 01:38 AM, Dayl wrote: Just wanted to make sure you know. :D

:D thanks

I got a question about the FD IDE, and i'd like to explain it here (more breathing room, and it's on-topic - though this topic got a bit de-railed, sorry)

using the IDE is a pain at first, but you'll grow to love it very quickly. It has the most helpful code hints in the world, runs and compiles at lightning-fast speeds, and does a lot of the tedious work for you (such as importing things that you use) - it's a huge time-saver for any flash programmer.

here's a step-by-step on creating your first AS3 project:
1. Open FD if you haven't already - it will take you through a little setup wizard (maybe. Something like that.)
2. Project -> Ne Project -> AS3 Project (under the AS3 category) - name it whatever you want
3. FD will create a new Main.as and open it for you

congrats! You've created a newproject. But what about adding classes?
1. Go to the right-hand side of the screen. You should see a few tabs there like "Outline", "bookmarks", etc. - hover your mouse over "Project"
2. A nice little side-menu appears with some folders in it
3. Right-click on the folder "src" - Add -> New Class - name it whatever you want, but please be ware of the "capitalization rule" I posted earlier (classes should start with capital letters. It's proper coding; stick with it.)
4. FD will create a barebones class for you.

great! Now we have a class. But what about importing things you've made in the flash IDE like animations?
Please note: Don't use a full flash document. Have one prepared with only (I mean ONLY) the graphics in it. You will be adding code to these elements in FD.
1. Look in your little "Project" sidebar again. You will see a folder called "lib"
2. Go into the flash IDE and save your .fla in the lib folder (will be located in the windows shell wherever you put your package)
3. Go to File -> Publish Settings in the flash IDE (while still having your .fla open)
4. Uncheck everything, and then check "SWC"
5. Make sure all of your base movieclips, etc. prepend "SWC_" - this means if you have a button in the main library folder called "myButton", you rename it to "SWC_myButton" (again, proper coding)
6. Make sure that you actually export everything you're going to use for actionscript - right-click the item in the library and select Properties. Then check "export for actionscript" and leave it at that.
7. Remove everything (EVERYTHING) from the stage. This is all useless as far as we're concerned.
8. Finally, hit the publish button.
9. Go back into the FD IDE and back into your lib folder. The swc, fla, all all that happy junk should be there. If not, you saved the .fla to the wrong path. Just move it to where it needs to be.
10. Right-click the .swc and make sure "add to library" is checked.

post-note: this does not cover a "worst-case" scenario. If you're having issues, you have some bigger problems, and it's best just to ask in the forums.

great, now we have our SWC (animations) imported and ready to use. So how do we use them?

remember that whole "naming thing" I was getting so worked up about? Here's where it comes into play:
let's use our "SWC_myButton" as an example.

private var coolVariableName:SWC_myButton = new SWC_myButton();

but wait, why doesn't that work? The answer is simple:
AS3 adds things to the stage in two parts.
The first part is adding the element to a variable, so all we have to do is reference the variable to change the element.
The second is actually adding that variable (not the element!) to the stage.
how do we do the second bit?

addChild(coolVariableName);

and about that little issue of changing the properties:

coolVariableName.x = 200;
coolVariableName.y = 200;

all-in-all, the code should look like this:

private var coolVariableName:SWC_myButton = new SWC_myButton();
coolVariableName.x = 200;
coolVariableName.y = 200;
addChild(coolVariableName);

now: I specifically added the x and y before I added it to the stage. Why is this?
because it's much faster when the plash player does this:
add to variable
set x
set y
add to stage

than it is when it does this:
add to variable
set x
set y
add to stage
set x
set y

one last thing: removing elements. Can you guess the function?

removeChild(coolVariableName);

P.S. everything works in events now. Research event dispatchers, they're quite handy.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature