-
[RESOLVED] Class files...can one make another?
Is it possible to create a movieclip and apply it to a class file that you could make? I mean...When you have a movieclip in the library in flash and have it be exported for actionscript...is it possible to do this for movieclip made in actionscript?? just have it generate a blank class file on start or something??
-
you can define a new class like this:
var newclass:Class;
http://livedocs.adobe.com/flash/9.0/...riptLangRefV3/
-
Total Universe Mod
Fixed your link flosculus.
http://livedocs.adobe.com/flash/9.0/...fV3/Class.html
What's your reasoning for doing this shabasky?
-
rabid_Delineator
yes thats possible. Essentially , you are talking about prototyping objects, but not good to do in the web world. Its memory expensive and will affect performance. Plus its messy. If you need to create dynamic object at run time , that may or may not contain certain elements that will be defined at run time , you should still be writing a base class that is robust , yet abstract enough to handle it. Read up on polymorphism and encapsulation. And keep in mind , this is actionscript , designed to be run and rendered in a browser. It does not have the benefit a virtual engine like .NET or Java. It also not a strict O.O.P. as java or C.
-
That is...an interesting post AttackRabbit. But really all im needing to do is reference a class file linked to a movieclip so i can sync it to a little physics engine thing.
-
rabid_Delineator
someMovieclip.addPhysics( physics );
where addPhysics is a public method of the class in which your movieclip is linked to.
-
Total Universe Mod
Could it be that all you're trying to do is extend MovieClip so that you can instantiate a customized MovieClip?
That would be
in a file named PhysicsClip.as
PHP Code:
package{ import flash.display.MovieClip; public class PhysicsClip extends MovieClip{ public function PhysicsClip(){ //constructor (default) actions } public function doStuff():void{ trace("hello from PhysicsClip"); } }
}
and then usage in your main class
PHP Code:
public var myPhysicsClip:PhysicsClip; myPhysicsClip = new PhysicsClip(); myPhysicsClip.doStuff();
-
Hmm ok. I'm using a physics engine called box2d. It does all the math and everything and all i need to do to actually see it is link a movlecip to it. you do that by accessing the boxDef's userdata. you link the userdata to a movieclip that you exported from actionscript
now, what im asking is, is it possibe to link a movieclip you made in code to a new class file and give that file a name and make it function the way any other exported movieclip would.
PHP Code:
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(75 / m_ratio, 50 / m_ratio);
boxDef.friction = 0.5;
boxDef.restitution = 0.3;
boxDef.userdata = //--A class file. A class file that is linked to a movieclip.
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(randomF(0, 1200), randomF(0, 800));
var theBox:b2Body = m_world.CreateBody(bodyDef);
theBox.CreateShape(boxDef);
theBox.SetMassFromShapes();
-
Wow. The question you wanted to ask and the question you actually asked are very very different.
Yes, you can set a variable to an instance created through code and use that with box2d.
Simply define your variable, instantiate it as an instance of whatever class you like, and add it to the stage. Then it's just like any instance added through the IDE.
Code:
var someClip:MyClass = new MyClass();
addChild(someClip);
//... later...
boxDef.userdata = someClip;
-
nooo.....im bad at explaining things..
1. create a class file in actionscript.
2. link the movieclip to the class file.
3. use it in box2d.
create a class file as in......
MyClass. The one you used. That would be created in actionscript by calling some sort of function.
"This class was not found so one will be automatically generated for you". Thats what it says when you export a movieclip for actionscript in flash while there is no class file with that name.
I want to do exactly what this does but in Actionscript! I want to create a movieclip and have it be "exported for actionscript"
-
Create a file called MyClass.as which defines your MovieClip. Something like this:
Code:
package{
import flash.display.MovieClip;
public class MyClass extends MovieClip {
//add properties here as variables.
public function MyClass(){
super();
//do other initialization stuff if necessary.
}
//add other methods, etc.
}
}
Now, you can associate that class with a clip in the library or not, your choice. If you do not, then you should add code which draws stuff so that the clip has a visible presence.
Then, you can do exactly the code I posted before to get an instance of MyClass and use it in box2d.
-
rabid_Delineator
yeah this whole thing makes no sense. If you create a movieclip at run time , meaning you say someClip = new MovieClip() , its already exported for actionsript, cus you are using it in actionscript. When you say new something() , you are calling the constructor of that object. So it is generating that code in the actionscript. If i have a movieclip in the library , that has no class file associated with it , but its base class , is some sort of display object , like sprite or movieclip , because flash auto generates a class , its linkage name becomes its , type. If i have a clip in the library called , myLIBCLIP. And i export for actionscript , but associate it with nothing else but the base class movieclip, then it becomes available to me in my main class. I could say
private var myNewInstance : MovieClip;
myNewInstance = new mYLIBCLIP();
addChild( myNewInstance );
This works because library objects extend display object. Youre not going to be able to custom define a class inside of your main class , and add it as a child to anything , because it is not display object. Now you can define a 'Helper' class outside of your package block , like
class helpfunc{
function helpfunc() {
}
}
but the only thing thats really gonna have reference to that class is the main class defined within the package block.
Its like whatever you are trying to do , i promise there is a ten times easier way to do it.
-
Originally Posted by 5TonsOfFlax
Create a file called MyClass.as which defines your MovieClip. Something like this:
Code:
package{
import flash.display.MovieClip;
public class MyClass extends MovieClip {
//add properties here as variables.
public function MyClass(){
super();
//do other initialization stuff if necessary.
}
//add other methods, etc.
}
}
Now, you can associate that class with a clip in the library or not, your choice. If you do not, then you should add code which draws stuff so that the clip has a visible presence.
Then, you can do exactly the code I posted before to get an instance of MyClass and use it in box2d.
ok. now how would you do all of this but inside a for loop inside a MAIN class file.
-
So you want to dynamically define a new class at runtime. AttackRabbit is right. Whatever it is you think you need to do this for, there's a much easier way to achieve it.
That said, it is possible to define a class at runtime by jumping through a few hoops. Hoops that are too high. And too small. And on fire. And moving. In other words, this is the hard way.
You can use a compiler such as the one at http://eval.hurlant.com/ to compile a string representation to a class. This works by writing a new swf into a bytearray then loading that bytearray as a swf.
Seriously though, why do you think you need to do that?
-
Its part of box2d's physics engine thing...To sync movieclips with the big square in the b2world...But what im probably going to is get the b2box's x and y and adding syncing the cordinates every frame....the thing is...thats not working...well, time to go to the box2d's forum...
-
You should not need a dynamically created class to use as a box2d userData. In fact, you should only need an instance. It does not matter how that instance gets on the stage. It could be placed there from the IDE, it could be a class associated with a library symbol which you create an instance of, or it can be an instance created from a class which is pure code.
-
rabid_Delineator
yeah i probably would have started a thread in the box2d forum first. if that truly is the case , and you need to dynamically write a class at run time to use box2d ill donate to a charity of your choice.
-
Total Universe Mod
It's a rather common box2d task. Each Box2d shape can have a display child associated with as a property of that shape. Each iteration, you run a method that returns all of the worlds shapes in an array that you loop over and update x,y, rotation etc.
-
rabid_Delineator
right but updating x and y , existing properties of an object , is not writing a dynamic class at run time, which for some reason , is what this guy wants to do. badly.
-
Originally Posted by jAQUAN
It's a rather common box2d task. Each Box2d shape can have a display child associated with as a property of that shape. Each iteration, you run a method that returns all of the worlds shapes in an array that you loop over and update x,y, rotation etc.
How exactly would you go about doing this? I assume you know a bit about box2d so would you tell me why you think this is not working?
Inside main, I altered it so that it would create a new class file instead of a movieclip...
PHP Code:
private function createProjects():void
{
for (var i:int = 0; i < numberOfProjects; i++)
{
var movieclip:PElement = new PElement(); //Creates the movieclip
movieclip.name = "card" + projectNumber; //Gives a name card + the current project number
var loader:Loader = new Loader();
var urlreq:URLRequest = new URLRequest("http://www.waldmark.com/mainpix/deer.png"); //A random test dumby image
loader.load(urlreq);
movieclip.addChild(loader);
movieclip.addEventListener(MouseEvent.CLICK, displayDataF); //Adds an event listener
addChild(movieclip); //Adds the movieclip to a movieclip already on stage
projectNumber++;
}
}
The PElement class files is heavily commented.
PHP Code:
package
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
public class PElement extends MovieClip
{
public function PElement()
{
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(75 / 30, 50 / 30);
boxDef.friction = 0.5;
boxDef.restitution = 0.3;
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(randomF(0, 1200) / 30, randomF(0, 800) / 30);
var theBox:b2Body = Main.m_world.CreateBody(bodyDef); //Main.m_world is a public static var...does it being static have anything to do with it?
//Also, in Main.m_world, I do a .step every frame...When i trace theBox's position..Its the same every frame
theBox.CreateShape(boxDef);
theBox.SetMassFromShapes();
//Code that positions it (The one that actually works...but only once
this.x = bodyPosition.x * 30;
this.y = bodyPosition.y * 30;
this.rotation = bodyRotation * (180/Math.PI) % 360;
//But when I add an enter frame event...It doesnt work. It just positions the movieclip in flash's defult x and y
this.addEventListener(Event.ENTER_FRAME, updateF);
function updateF(e:Event):void
{
var bodyPosition:b2Vec2 = theBox.GetPosition();
var bodyRotation:Number = theBox.GetAngle();
this.x = bodyPosition.x * 30; //30 is the set ratio between pixles and meters I made.
this.y = bodyPosition.y * 30;
this.rotation = bodyRotation * (180 / Math.PI) % 360;
trace("The body position is " + bodyPosition.x * 30 + " by " + bodyPosition.y * 30); //Traces the same over and over again...
}
}
private function randomF(lowVal:int, highVal:int):int
{
if (lowVal <= highVal)
{
return(lowVal + Math.floor(Math.random() * (highVal - lowVal + 1 )));
} else {
throw(new Error("Fail. Wrong values passed to randomF"));
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|