Forum Topic: As3 Oop

(165 views • 17 replies)

This topic is 1 page long.

<< < > >>
None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 09:35 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

Yes, its me with another one of my idiotic questions :]

i recently made a function that works perfectly for very easily checking key downs, for most keys on a standard keyboard by using their name instead of having to figure out the key code (eg. KEY("DELETE") ) which returns either true or false. As i said no problems there.

Next i wanted to convert it into a class, but me only being using AS3 for a week and a half, and not ever getting far enough in AS2 to bother learning OOP its not looking to good. I did read up something back when using AS2 and with some help from google was able to convert most of it into OOP friendly code. That i think is fine as well. Now im stuck at the last part, how to import that into my game. I tried Putting it in a folder AS/key/key.as and importing it into my fla like this:

import AS.key.key;
var key:Key = new Key()

Key being a public function inside the package

but that didint seem to do it:

1046: Type was not found or was not a compile-time constant: Key.

So what im looking for basically is just: how the hell do i get a package/class to work?

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

knugen

Reply To Post Reply & Quote

Posted at: 11/8/09 09:54 AM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,654

Class names are case-sensitive :)


None

Johnny

Reply To Post Reply & Quote

Posted at: 11/8/09 09:56 AM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,429

You can put it in the same folder as the .fla you're using it in, and it's imported automatically.

The other option is to change the classpath of the package.

Whether it's good practice or not, I generally keep a copy of all the classes I'm using in the same folder as the .fla I'm working on.

All sites currently down. Deal with it. <3

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 10:18 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

like i said, importing aint a issue. Its doing that just fine and it looks like i figured out my origonal question, but im getting a bunch of errors on all my variables, claiming for example:

1120: Access of undefined property p.

while the source is:

for (var p:uint; p<l; p++) {

clearly showing that p is defined. wtf? is it because i need to put private/public in front of it? or define it on its own?

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 11/8/09 10:21 AM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,429

At 11/8/09 10:18 AM, Yambanshee wrote: for (var p:uint; p<l; p++) {

clearly showing that p is defined. wtf? is it because i need to put private/public in front of it? or define it on its own?

Not when used like that, you don't.

Is that the exact line that's causing you error? If so, I don't know what's going on. If it's another line, it could be because it's not in the same function as where it was defined.

All sites currently down. Deal with it. <3

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 10:37 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

i have no idea how but it seems to have sorted itself out when i turned it into a comment, then back into code. but im stll getting god damned errors! what the hell, heres my code, see whats wrong and how it should work

KEY EXAMPLE.fla file path is in the root folder
key.as file path is rootFolder/AS/key

.as code:

package AS.key{
	import flash.events.KeyboardEvent;
	public class Key {
		public function keyInt(sta) {
			sta.addEventListener(KeyboardEvent.KEY_DOWN, kd);
			sta.addEventListener(KeyboardEvent.KEY_UP, ku);
		}
		private var keysCode:Array = [a lot of stuff];
		private var keysString:Array = ["a lot of stuff'];
		private var keyValues:Array = new Array();
		private var l:uint = keysCode.length;
		for (var p:uint = 0; p<l; p++) {
			keyValues.push();
		}
		// INT. DONE

		private function kd(e:KeyboardEvent):void {
			for (var i:uint = 0; i<l; i++) {
				if (e.keyCode == keysCode[i]) {
					keyValues[i] = "true";
					return;
				}
			}
		}
		private function ku(e:KeyboardEvent):void {
			for (var i:uint = 0; i<l; i++) {
				if (e.keyCode == keysCode[i]) {
					keyValues[i] = 'false';
					return;
				}
			}
		}
		public function KEY(e:String) {
			var pl:uint = keysString.length;
			for (var i:uint = 0; i<pl; i++) {
				if (e == keysString[i]) {
					if (keyValues[i] == "true") {
						return true;
					} else {
						return false;
					}
				}
			}
		}
	}
}

fla code:

import AS.key.key;

var key:Key = new Key(stage)
keyInt(stage)

errors:

name:
1046: Type was not found or was not a compile-time constant: Key.
source:
var key:Key = new Key(stage)

name:
1180: Call to a possibly undefined method Key.
source:
var key:Key = new Key(stage)

name:
1180: Call to a possibly undefined method keyInt.
source:
keyInt(stage)

all errors on my fla

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

Questioning

zrb

Reply To Post Reply & Quote

Posted at: 11/8/09 10:41 AM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,548

At 11/8/09 10:37 AM, Yambanshee wrote: package AS.key{

Can you actually do that ? I thought a package's name is only one word :o

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

knugen

Reply To Post Reply & Quote

Posted at: 11/8/09 10:45 AM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,654

At 11/8/09 10:37 AM, Yambanshee wrote: import AS.key.key;

var key:Key = new Key(stage)
keyInt(stage)

You have defined your class in key.as, but you're trying to instantiate a class with the name Key.

You would also need to do key.keyInt(stage).


Elated

wwwaaasssiiimmm

Reply To Post Reply & Quote

Posted at: 11/8/09 10:47 AM

wwwaaasssiiimmm NEUTRAL LEVEL 02

Sign-Up: 04/15/09

Posts: 14

i have nothing informative to say in here hI!


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 11:14 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

At 11/8/09 10:45 AM, knugen wrote: You have defined your class in key.as, but you're trying to instantiate a class with the name Key.

You would also need to do key.keyInt(stage).

i see. i thought i had to refer to the class and not the package.

But now it says that both p and l is undefined on line 12 (the for loop) and that keyValues is undefined on line 13 (inside the for loop)

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

knugen

Reply To Post Reply & Quote

Posted at: 11/8/09 11:27 AM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,654

At 11/8/09 11:14 AM, Yambanshee wrote: But now it says that both p and l is undefined on line 12 (the for loop) and that keyValues is undefined on line 13 (inside the for loop)

That loop is in the class body, it should be in one of the functions. Also, I don't think push() with no arguments does anything at all, but I'm not sure.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 11:35 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

At 11/8/09 11:27 AM, knugen wrote:

Also, I don't think push() with no arguments does anything at all, but I'm not sure.

copy/paste error. it does push a value "false"

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 11/8/09 11:50 AM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,429

Try these things.

Change:

import AS.key.key;

to

import AS.key.Key; //capital K or the class.

If that doesn't work..

The 2nd thing, just to avoid any classpath problem.

Try placing it in the same folder as your .fla.
Remove the import command completely
Remove everything after 'package' (no AS.key)

That should remove any classpath problems completely so you can test your class and narrow down the problem.

All sites currently down. Deal with it. <3

BBS Signature

None

knugen

Reply To Post Reply & Quote

Posted at: 11/8/09 11:53 AM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,654

Does it work?

If not perhaps you should try with a simpler type of class instead then keep building on that. If you need key functionality you can use this class.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 11:54 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

sorry for double post

Got it working!
download the opensource .as here!

help file, here

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 11/8/09 12:04 PM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 936

Hm, I made one where you predefine game actions and then can map any key to any gameaction (multiple keys for an action ie.). Also you can remap keys ect.

Usage would be:

import source.ui.keys.KeyAction;
import source.ui.keys.KeyManager;

// init keymanager
KeyManager.init(stage);

// bind some keys to the predefined gameactions
KeyManager.bind(KeyAction.UP_VALUE, "w");
KeyManager.bind(KeyAction.UP_VALUE, "UP_ARROW");

// in your enterframe / game loop
if (KeyAction.UP) {
	trace("UP");
}

Downside is that you have to maintain a KeyAction file for your projects, but you will benefit from autocompletion and ease of use within your code.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 11/8/09 12:12 PM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,582

At 11/8/09 12:04 PM, LeechmasterB wrote: Hm, I made one where you predefine game actions and then can map any key to any gameaction (multiple keys for an action ie.). Also you can remap keys ect.

that looks quite useful. Probably a lot more efficient then my method

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 11/8/09 04:33 PM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 936

I made it a little more userfriendly and added comments, including example project for cs3 and flashdevelop 3.
It should be pretty easy to understand how it works, all you have to edit is KeyActions.as and bind keys to your games needs.

KeyController.rar

You can see that there are no loops necessary during runtime, only on initialization, to create the label/value lists.

Enjoy... ^^


All times are Eastern Standard Time (GMT -5) | Current Time: 07:08 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!