Be a Supporter!

Event Functions in AS3 classes???

  • 581 Views
  • 24 Replies
New Topic Respond to this Topic
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Event Functions in AS3 classes??? Feb. 14th, 2008 @ 01:33 PM Reply

Been messing around with AS3 alot. Got almost all of it down but one thing. How do i go about having an ENTER_FRAME listener in an AS3 class file? Im not entirely sure where to place the function or listener. I've looked everywhere for an answer and just cant seem to find it. This is the code im trying to use:

package {
	import flash.display.MovieClip;
	public class TheAwesomeSprite extends MovieClip {
		function TheAwesomeSprite(wid:Number, hei:Number) {
			function movement(event:Event):void {
				x+=200;
			}
		}
		addEventListener(Event.ENTER_FRAME, movement);
	}
}

And that gives me this error when i test the swf:
1046: Type was not found or was not a compile-time constant: Event.
function movement(event:Event):void {

So if someone could shed some light on this little problem of mine it would be greatly appreciated. Then i can finally get going on a decent engine.


BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 01:51 PM Reply

The error is about you not importing flash.events.Event. ...Duh.
When you define a function inside another it is local to only that function. Class methods are defined alongside the constructors.
Also you want to call the addEventListener within the constructor, right?

Maybe you want to try OOP in AS2 first, you're like totally clueless. -_-


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 02:04 PM Reply

At 2/14/08 01:51 PM, GustTheASGuy wrote: The error is about you not importing flash.events.Event. ...Duh.
When you define a function inside another it is local to only that function. Class methods are defined alongside the constructors.
Also you want to call the addEventListener within the constructor, right?

Maybe you want to try OOP in AS2 first, you're like totally clueless. -_-

lol. funny guy. Wait, no, i meant ignorant fuck.

Anyone who has been around back in the day knows im at least competent with most AS. If not more then competent. So get over yourself and learn your place.

This is where you stop being a big shot and explain importing events a little more. I have yet to deal with them. Even after my extensive use with AS2 classes.


BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 02:10 PM Reply

I know you, won't help you much. :P

You see the 'import flash.display.Sprite;' you have? All the API classes are organised into packages like this and you need an import statement to get each one, like you needed this one to extend the Sprite class.

I don't see any grounds for you to believe you're competent with AS, you've got no idea what function goes where.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 02:24 PM Reply

At 2/14/08 02:10 PM, GustTheASGuy wrote: You see the 'import flash.display.Sprite;' you have? All the API classes are organised into packages like this and you need an import statement to get each one, like you needed this one to extend the Sprite class.

So i have to import the event im assuming. If thats the only issue here, i dont see why i cant figure the rest out. If i have anymore issues, i'll post.

I don't see any grounds for you to believe you're competent with AS, you've got no idea what function goes where.

Which is why your an ignorant fuck. I ask for one question, post a topic in the BBS for the first in over a year, and out of fucking nowhere you have my AS skill pinned down like needlepoint??? That makes no fucking sense man. I've built some impressive engines entirely on OOP, whether you like to believe it or not.

lol. And here i thought Glaiel was bad. Now theres an entirely new generation of know-it-all's out to shit on the little guy. Well done.


BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 02:33 PM Reply

Well I'm most commonly on the forum so I'm just about the only one you'll encounter to be able to answer the questions. >:D
I don't like people who ask about stuff they'd be better off figuring out themselves even if they're as slow as you.

Neither do I believe you've done anything with OOP, you might've at best crammed your Flash code into a class. Seriously, look at the code you posted.

Glaiel wasn't bad at all, oh no.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 02:44 PM Reply

At 2/14/08 02:33 PM, GustTheASGuy wrote: I don't like people who ask about stuff they'd be better off figuring out themselves even if they're as slow as you.

Really laying it on arn't ya? Anyway, i think making the transition from AS2 to AS3 in the matter of a week with having to ask a single question is far from unimpressive. After this i will have completely made the jump. I have spent an access of 5 hours surfing the net trying to figure out the answer to this question. Thats more then enough time to spend before coming to the forums.

Neither do I believe you've done anything with OOP, you might've at best crammed your Flash code into a class. Seriously, look at the code you posted.

What do you consider OOP? My FLA has nothing on the stage, everything is brung out dynamically from the library. All the AI code, weapon code, and controlable character code are incased in classes. What exactly constitutes OOP?

And what exactly is wrong with the code i posted??? The only issue was that one line of code where i import the event. Other then that, its all example. A simple example. What is your issue with it?

Glaiel wasn't bad at all, oh no.

In comparison? No. Your much worse.


BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 03:06 PM Reply

List:
1. AS3 is syntatically the same as AS2 therefore there's so nothing to jump around about. For a week. Normally experianced programmers can get a language down in under an hour.
2. Importing shit is obvious. Basic OOP concept. At best you can't have much experience if you have a problem wit this. (Note how positive-sounding this sentence is.)
3. OOP is about organising operations which you're apparently not familiar so I guessed you just shoved all your code in a class structure and figured you got it covered.
4. In your example the function definition and constructor body have switched places. Nothing wrong with that at all no way no how absolutely not even.

You've been around NG for as long as I know and you're not familiar with me. You're, uh, slow.
The only way you can retain your sanity is to completely ignore the way I speak.
You can't compare me to Glaiel, that guy's a huge geek. And yes I was saying the same thing. Ha!

Now you've FORCED me to throw away ten minutes of my time detailing how stupid you are I could've spent doing some awesome programming BETTER THAN YOU in AS3 ALSO so THAT'S IT.
Noob.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 03:44 PM Reply

lol. Seriously. Your the bottem of the barrel.

Im an art major man. Programming is very much a second thought at the moment, and always was. No, i dont claim to be advanced, nor do i claim to be better then you. I know im not advanced, and i am fine with that. The problem here is, why do you have a problem with that? Why in gods name would you give a shit if i was advanced or not? Did it not occur to you that i simply dont care for getting better, and would prefer to know just enough to code my own games, whether there done your way or not?

Im not mad at all. Just perplexed. Im sitting here laughing because i cant for the life of me figure you out. Does me not knowing a code really piss you off that much? Your mad because im supposed to know it? What does it matter. Honestly now. What the fuck does it matter whether i understand code as well as you?

Your pathetic. If you look back on this conversation without realizing your fault, then you truly are fucked.


BBS Signature
c0wz
c0wz
  • Member since: Oct. 14, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 04:01 PM Reply

Winner in this thread is OP. Flash is for assistance in Flash, not for fuckholes to revel in their own Actionscripting glory and announce to the world "YOU SUX LOL!!!!!!!!11111111111111"

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 04:06 PM Reply

At 2/14/08 04:01 PM, c0wz wrote: Winner in this thread is OP. Flash is for assistance in Flash, not for fuckholes to revel in their own Actionscripting glory and announce to the world "YOU SUX LOL!!!!!!!!11111111111111"

Funny thing is, he doesn't realize it yet.


BBS Signature
KaynSlamdyke
KaynSlamdyke
  • Member since: Jun. 25, 2004
  • Offline.
Forum Stats
Member
Level 16
Melancholy
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 04:29 PM Reply

At 2/14/08 04:06 PM, 23450 wrote: Funny thing is, he doesn't realize it yet.

And he won't for a few years to come. Let him have his powertrip - it's what makes Gust the psychotic little expert we all know and love.


...

BBS Signature
AAF
AAF
  • Member since: Jun. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 07:03 PM Reply

in an off topic point,

do you think AS3 is easier then AS2?

I mean ,for someone that doesnt know anything about both.


.

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 07:31 PM Reply

At 2/14/08 04:29 PM, KaynSlamdyke wrote:
At 2/14/08 04:06 PM, 23450 wrote: Funny thing is, he doesn't realize it yet.
And he won't for a few years to come. Let him have his powertrip - it's what makes Gust the psychotic little expert we all know and love.

Yah, he's pretty young, i should expect it. Still bothers me though. I dont think being good at something absolves him from being an asshole though.

At 2/14/08 07:03 PM, AAF wrote: in an off topic point,

do you think AS3 is easier then AS2?

I mean ,for someone that doesnt know anything about both.

AS2 i would assume. Its alot less strict.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 09:36 PM Reply

Now i came across another issue. How do i go about refrencing objects on the stage from inside a class? Every time i try, it says something about using an unidentified variable or something....

If Gust wants to bitch for another 5 posts, its welcome, because i at least got an answer last time. lol.


BBS Signature
Tree-SkyLark-BCE
Tree-SkyLark-BCE
  • Member since: Aug. 6, 2005
  • Offline.
Forum Stats
Member
Level 35
Programmer
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 10:18 PM Reply

I generally pass the stage object as a parameter of the constructor. Just give the objects that you add to the stage a name that you can reference with getChildByName(). You can do that or pass the actual object to the class. Either one should work well enough.
First Method:
Class to which the stage is passed:

//File: Ref.as

package
{
	import flash.display.Stage;
	
	public class Ref
	{
		private var stage:Stage;
		
		public function Ref(s:Stage)
		{
			stage = s;
			stage.getChildByName("Test").x = 55;
		}
	}
}

My main class (or document class if you are using CS3):

//File Main.as

package
{
	import flash.display.Sprite;
	
	public class Main extends Sprite
	{
		public function Main():void
		{
			var g:Sprite = new Sprite();
			with (g.graphics)
			{
				beginFill(0x000000);
				drawRect(0, 0, 25, 25);
				endFill();
			}
			stage.addChild(g);
			g.name = "Test";
			var r:Ref = new Ref(stage);
		}
	}
}

BBS Signature
ryratt
ryratt
  • Member since: Feb. 13, 2008
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 14th, 2008 @ 11:17 PM Reply

I think if you're new it's best to start with AS3. AS3 seems to work more logically. I seem to remember in AS2 a few things that worked totally logically except under certain circumstances and it drove me batty because I didn't know about the exception.

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 02:46 AM Reply

At 2/15/08 12:20 AM, LCurtis wrote: Also, you could tone back your attitude. Especially when someone is trying to help you and clearly only jokingly commenting on your AS skills. Gust can be an extreme help with AS3 problems (_probably more so than anyone on this forum_) and that just means you gotta learn to take his shit. :D

Gust was hardly helpful in his first post. I had to force the answer from him. Had nothing to do with taking a joke, there was no joke to take. He's just a dueche.

But thanks for your help. Never did think about the error codes. And i'll check back tommorow to give an update on my second problem. Wont be able to test it until then.


BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 09:10 AM Reply

If I'd be serious about this I wouldn't argue so extensively. So yes that is a joke. I know better because I made it. You wanted me to judge you on the scale of competent programmers, no? It's all your fault!


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 11:03 AM Reply

You're funny :)


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 03:06 PM Reply

At 2/15/08 09:10 AM, GustTheASGuy wrote: If I'd be serious about this I wouldn't argue so extensively. So yes that is a joke. I know better because I made it. You wanted me to judge you on the scale of competent programmers, no? It's all your fault!

Still dont understand the mentality behind your first post. Whatev's. Questions answered. Lets all go home.


BBS Signature
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 03:44 PM Reply

Hmmm... I'm going to take the liberty of asking an AS3 question here :)

What is faster/more efficient, a named function or an anonymous one?

Thanks in advance :P

BBS Signature
Moonkey
Moonkey
  • Member since: May. 11, 2007
  • Offline.
Forum Stats
Member
Level 07
Programmer
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 03:46 PM Reply

Named

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 03:59 PM Reply

Mentality? Ninja chicken.

I second the named thing, such definitions have a clearer context aswell. What difference does it make in what you're doing? You pretty much only use anonymous functions for functional arguments or dynamic objects (like, an event property).


BBS Signature
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Event Functions in AS3 classes??? Feb. 15th, 2008 @ 04:01 PM Reply

Thanks for the feedback :)

Named it is then.


BBS Signature