Forum Topic: document class newbie

(104 views • 13 replies)

This topic is 1 page long.

<< < > >>
Happy

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 11:00 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

so ive started to embrace AS3 and im trying to learn the OOP aspects abt it which is DOCUMENT CLASS.

i went thru this tutorial:
http://www.gotoandlearn.com/play?id=43

so erm well u basically make stars with random scale and coordinates every 10 secs on timer event.
And i think i got the hang of private public and all that crap.

So i was thinking is it possible to store variables in that star as private so i dun have to like make an array to store all those stars variables in public? And erm so yeah i think the only way to do that is to make a Star class. and i was liek WOOHOO ok lets go. and then there was 30seconds of awkward silence between AS3 and me. =(

so basically how do i make classes of the star and stuffs o_O im not quite sure how to summarise what im doing but yeah can someone help plox.

Jus in case u want teh code:

package
{
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	public class DocumentClass extends MovieClip{
		private var timer:Timer = new Timer(100);
		
		public function DocumentClass():void{
			timer.addEventListener(TimerEvent.TIMER,makeStar);
			timer.start();
		}
		
		private function makeStar(e:TimerEvent):void{
			var star:Star=  new Star();
			star.scaleX = star.scaleY=Math.random();
			star.x = Math.random()*this.stage.stageWidth
			star.y = Math.random()*this.stage.stageHeight
			star.rotation=Math.random()*360
			this.addChild(star);
		}
	}
}

None

henke37

Reply To Post Reply & Quote

Posted at: 10/5/09 11:06 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,670

What was the question again?

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 11:09 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

erm how to make a class of the objects ive created ...

the stars that were randomly generated currently are just instances. I want them to be classes. Cuz i wanna make them move and fade out according to their own private vars

=( sry if i was going a lil confusingk up there


None

hdxmike

Reply To Post Reply & Quote

Posted at: 10/5/09 11:12 AM

hdxmike LIGHT LEVEL 09

Sign-Up: 09/11/09

Posts: 1,824

im at a loss for words

'jus in case u want teh codes ' = LOL

OOP AS3 || Flash 8,CS3,CS4 || *sigh* || The new forum on the block : FLASH SEED !

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 11:13 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

=( ok i noez i are teh funniest but can some 1 pls help meghhh

=(((((


None

UnknownFury

Reply To Post Reply & Quote

Posted at: 10/5/09 11:17 AM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

Eh? I sitll don't have a clue what you're on about. You make a seperate class for your stars :/

package
{

import flash.display.MovieClip;

public class Star extends MovieClip
{
    // stuff here

}
}

Something along those lines. Was that even what you were asking?

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 11:19 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

to be honest im nt sure either =( lol

erm ok the main code has

addChild(star);

rite? thats making and instance of a star... instead of that how can i make a new class that contains the star object and private variables like fading speed and _x,_y velocities inside that class?


None

hdxmike

Reply To Post Reply & Quote

Posted at: 10/5/09 11:44 AM

hdxmike LIGHT LEVEL 09

Sign-Up: 09/11/09

Posts: 1,824

At 10/5/09 11:13 AM, iluvAS wrote: =( ok i noez i are teh funniest but can some 1 pls help meghhh

=(((((

OMFG ive ever LOLED so hard !! :D

OOP AS3 || Flash 8,CS3,CS4 || *sigh* || The new forum on the block : FLASH SEED !

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 10/5/09 12:00 PM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,585

Use UnknownFury's code as a template for your star class. Put that in it's own .as file and name it Star.

Then, open up the linkage properties of the star movieclip and in the area where it says "class" Type "Star"

That will link the 2 together.

Now, anything you put in that class will affect the star.

All sites currently down. Deal with it. <3

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 12:03 PM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

went to trusty google tried to find a way to create class of a star heres what i came up with 2 as files DocumentClass: Main Code
StarClass: Class for each Star

//DocumentClass codes
package
{
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import StarClass;
	
	public class DocumentClass extends MovieClip{
		private var timer:Timer = new Timer(100);
		
		public function DocumentClass():void{
			timer.addEventListener(TimerEvent.TIMER,makeStar);
			timer.start();
		}
		
		private function makeStar(e:TimerEvent):void{
			var star:StarClass=  new StarClass();
		}
	}
}

//StarClass Code
package
{
	
   public class StarClass
   {
	   private var color:Number = Math.random()*10;
	   public function StarClass(){
		   var star:Star=new Star();
		   star.scaleX = star.scaleY=0.25+Math.random()*0.75;
		   star.x = Math.random()*550
		   star.y = Math.random()*400
		   star.rotation=Math.random()*360
		   this.addChild(star);//smth wrong here
	   } 
   }
}

I guess smth went wrong at the "this" im suppose to direct or refer or wateva u call it to the document class? ithink.... how do i do dat? cuz i onli got the error

1061: Call to a possibly undefined method addChild through a refrence with static type StarClass.

None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 12:07 PM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

At 10/5/09 12:00 PM, Johnny wrote: Use UnknownFury's code as a template for your star class. Put that in it's own .as file and name it Star.

Then, open up the linkage properties of the star movieclip and in the area where it says "class" Type "Star"

That will link the 2 together.

Now, anything you put in that class will affect the star.

woah okay so wat u mean is dat the as file is named star and the star movieclip has class name Star as well? if dat so anything done in the star as file will happen to the star instance? liek

x+=10

will move the star? erm if dat is so jus to clarify to make the star instance it is still

var star:Star=new Star();
addhild(star);

am i right?


None

iluvAS

Reply To Post Reply & Quote

Posted at: 10/5/09 12:22 PM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

im trying out the linked as file and star class thing. jus to clarify one more thing. Do i create the instancec of the star inside the DocumentClass class or inside the Star class. cuz when i want to set the stars coordinates and scale i think it mistook me trying to change the as file Star class coordinates and scale and came up with errors cuz u cant change those things as its static watever crap the errror thing says.

So can someone show me an example =( and yeah im indirectly asking for free code so dat i can learn from a working source


None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 10/5/09 02:12 PM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 942

Dude, you have no clue what you are doing. Look at the code that UnknownFury posted.


None

UnknownFury

Reply To Post Reply & Quote

Posted at: 10/5/09 03:33 PM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

The reason the addChild didn't work is because your class wasn't a display type, not to mention you're doing it all wrong. If you want a class with code and the image of MovieClip from the library. Right click your MC in the library and click 'Linkage'. Then click export for actionscript and put the class name as Star. Then your code would look something like this.

// Document class
package
{
	import flash.display.MovieClip;

	public class DocClass extends MovieClip
	{

		function DocClass()
		{
			var star : Star = new Star();
			addChild(star);

			addEventListener(Event.ENTER_FRAME, enterframe);
		}

		function enterframe(e : Event) : void
		{
			star.move();
		}
	}
}

// Star.as
package
{
	import flash.display.MovieClip;

	public class Star extends MovieClip
	{

		function Star()
		{
			x = y = 50;
		}

		function move() : void
		{
			x ++;
			y ++;
		}
	}
}

This should, haven't tested, put an instance of your star on the stage at (50, 50) and slowly move it towards the bottom right. Hopefully you can see what I'm doing. Rather than having a class with the code in which then controls a class in your library you do it all in one class.

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


All times are Eastern Standard Time (GMT -5) | Current Time: 02:14 PM

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